android MagicIndicator 指示器简单使用

时间:2022-01-29 10:29:40

布局文件内容

<net.lucode.hackware.magicindicator.MagicIndicator
android:id="@+id/view_home_main_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#d43d3d" />

java代码中设置

//初始化指示器
private void initMagicIndicator() {

CommonNavigator commonNavigator = new CommonNavigator(context);
commonNavigator.setAdapter(new CommonNavigatorAdapter() {
@Override
public int getCount() {
return mDataList == null ? 0 : mDataList.size();
}

@Override
public IPagerTitleView getTitleView(Context context, final int index) {
ClipPagerTitleView clipPagerTitleView = new ClipPagerTitleView(context);

clipPagerTitleView.setText(mDataList.get(index));
clipPagerTitleView.setTextColor(Color.parseColor("#f2c4c4"));
clipPagerTitleView.setClipColor(Color.WHITE);

clipPagerTitleView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewHomeMainViewpager.setCurrentItem(index);
}
});

return clipPagerTitleView;
}

@Override
public IPagerIndicator getIndicator(Context context) {
LinePagerIndicator indicator = new LinePagerIndicator(context);
// indicator.setLineColor(Color.parseColor("#ffffff"));
indicator.setColors(Color.parseColor("#ffffff"));
return null;
}
});
viewHomeMainIndicator.setNavigator(commonNavigator);
//关联viewpager
viewHomeMainViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
viewHomeMainIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
@Override
public void onPageSelected(int position) {
viewHomeMainIndicator.onPageSelected(position);
}
@Override
public void onPageScrollStateChanged(int state) {
viewHomeMainIndicator.onPageScrollStateChanged(state);
}
});
viewHomeMainViewpager.setCurrentItem(0);
}