private ViewPager viewPager;
private LinearLayout linearLayout;
public SimplePagerIndicator(Context context) {
super(context);
}
public SimplePagerIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SimplePagerIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* 对外提供一个方法,设置viewPager
*/
public void setViewPager(ViewPager viewPager){
this.viewPager = viewPager;
linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
this.addView(linearLayout);
//给linearLayout添加条目
for (int i =0;i<viewPager.getAdapter().getCount();i++){
View view = View.inflate(getContext(), R.layout.indicator_layout, null);
//设置点击事件
view.setOnClickListener(this);
//找到控件
TextView text_title = view.findViewById(R.id.text_title);
TextView text_indicator = view.findViewById(R.id.text_indicator);
text_title.setText(viewPager.getAdapter().getPageTitle(i));
if (i == 0){
text_indicator.setVisibility(View.VISIBLE);
//设置标题的颜色
text_title.setTextColor(Color.RED);
}else {
text_indicator.setVisibility(View.INVISIBLE);
text_title.setTextColor(Color.BLACK);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,5,10,5);
//把条目添加到linearLayout中
linearLayout.addView(view,layoutParams);
}
}
@Override
public void onClick(View view) {//view代表点击的控件
//遍历linaearLyaout里面的子条目
for (int i = 0;i<linearLayout.getChildCount();i++){
if (view == linearLayout.getChildAt(i)){
//选中viewPager那一页
viewPager.setCurrentItem(i,false);//
}
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0;i<linearLayout.getChildCount();i++){
View view = linearLayout.getChildAt(i);
TextView text_title = view.findViewById(R.id.text_title);
TextView text_indicator = view.findViewById(R.id.text_indicator);
if (i == position){
text_indicator.setVisibility(View.VISIBLE);
//设置标题的颜色
text_title.setTextColor(Color.RED);
//控制scrollView滚动到某个位置
this.smoothScrollTo((int) linearLayout.getChildAt(i).getX()-linearLayout.getChildAt(i).getWidth(),0);
}else {
text_indicator.setVisibility(View.INVISIBLE);
//设置标题的颜色
text_title.setTextColor(Color.BLACK);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
实现一个frement
public class NewsFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText(getArguments().getString("title",""));
return textView;
}
public static NewsFragment getInstance(String title){
NewsFragment newsFragment = new NewsFragment();
Bundle bundle = new Bundle();
bundle.putString("title",title);
newsFragment.setArguments(bundle);
return newsFragment;
}
}
-------------------------------------------------------------------------------
public class MainActivity extends AppCompatActivity {
private ProgressView progressView;
private ViewPager viewPager;
private List<String> list;
private SimplePagerIndicator simplePagerIndicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.view_pager);
simplePagerIndicator = findViewById(R.id.simple_indicator);
list = new ArrayList<>();
list.add("推荐");
list.add("推荐新闻");
list.add("推荐娱乐");
list.add("推荐歌");
list.add("推荐视频");
list.add("局势");
list.add("军事");
list.add("国际");
list.add("推荐");
list.add("推荐新闻");
list.add("推荐娱乐");
list.add("推荐歌");
list.add("推荐视频");
list.add("局势");
list.add("军事");
list.add("国际");
list.add("推荐");
list.add("推荐新闻");
list.add("推荐娱乐");
list.add("推荐歌");
list.add("推荐视频");
list.add("局势");
list.add("军事");
list.add("国际");
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public Fragment getItem(int position) {
return NewsFragment.getInstance(list.get(position));
}
@Override
public int getCount() {
return list.size();
}
});
simplePagerIndicator.setViewPager(viewPager);
//添加监听事件
viewPager.addOnPageChangeListener(simplePagerIndicator);