package com.wzq.scrollviewindicater;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.wzq.scrollviewindicater.fragment.MFragment;
import java.util.ArrayList;
public class MainActivity extends FragmentActivity implements OnClickListener {
private LinearLayout ll_head;
private ViewPager viewPager;
private String[] titles;
private ArrayList<TextView> textViewList;
private HorizontalScrollView horizontalScrollView;
private ArrayList<TextView> titlesView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll_head = (LinearLayout) findViewById(R.id.ll_head);
horizontalScrollView = (HorizontalScrollView) findViewById(R.id.hsv);
viewPager = (ViewPager) findViewById(R.id.viewpager);
titles = new String[]{"头条", "娱乐", "科技", "信息", "八卦", "北京", "上海", "天津",
"重庆", "大大燕网"};
//动态添加顶部textview
initTitle();
viewPager.setAdapter(new MPageAdapter(getSupportFragmentManager()));
initListener();
}
/**
* 设置滑动viewpager监听
*/
private void initListener() {
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
//滑动的时候, 改变文字颜色,
for (int i = 0; i < titles.length; i++) {
if (i == position) {//1
titlesView.get(i).setTextColor(Color.RED);
}else {
titlesView.get(i).setTextColor(Color.GRAY);
}
}
//处理滑动顶部标题
int width = titlesView.get(0).getWidth();//textview的宽度,
horizontalScrollView.scrollTo((width+20)*position,0);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override
public void onPageScrollStateChanged(int state) { }
});
}
/**
* 动态添加标题;
*/
private void initTitle() {
titlesView = new ArrayList<>();
// 我
for (int i = 0; i < titles.length; i++) {
TextView textView = new TextView(MainActivity.this);
textView.setText(titles[i]);
textView.setTextSize(26);
if (i == 0) {
textView.setTextColor(Color.RED);//处理第一次进入第一个标题为红色
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//设置外边距,左上右下;
params.setMargins(10, 10, 10, 10);
textView.setId(i);
textView.setOnClickListener(this);//点击文字的时候,会进入onClick方法;
titlesView.add(textView);
ll_head.addView(textView, params);
}
}
/**
* 处理顶部文字的点击事件;
* @param v
*/
@Override
public void onClick(View v) {
int id = v.getId();//id就是标题的下标;
//ID是哪个Textview,我将来让viewpager滑动到哪个页面;
viewPager.setCurrentItem(id);
}
private class MPageAdapter extends FragmentPagerAdapter {
public MPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
//动态返回Fragment;
return MFragment.getInstance(titles[position]);
}
@Override
public int getCount() {
return titles.length;
}
}