1.build
[html]view plaincopy
compile 'com.shizhefei:ViewPagerIndicator:1.1.3'
2.xml
[html]view plaincopy
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grayE">
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:overScrollMode="never">
android:layout_width="match_parent"
android:layout_height="0.6dip"
android:background="@color/grayE"/>
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="54dip">
3.java
[java]view plaincopy
publicclassMainActivityextendsFragmentActivity {
privateLayoutInflater inflater;
privateIndicatorViewPager indicatorViewPager;
@Override
protectedvoidonCreate(@NullableBundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupLayout();
}
privatevoidsetupLayout() {
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setOffscreenPageLimit(5);
finalIndicator indicator = (Indicator) findViewById(R.id.indicator);
indicatorViewPager =newIndicatorViewPager(indicator, viewPager);
inflater = LayoutInflater.from(getApplicationContext());
indicatorViewPager.setAdapter(adapter);
indicatorViewPager.setOnIndicatorPageChangeListener(newIndicatorViewPager.OnIndicatorPageChangeListener() {
@Override
publicvoidonIndicatorPageChange(intpreItem,intcurrentItem) {
if(preItem != -1) {
indicatorViewPager.getIndicatorView().getItemView(preItem).findViewById(R.id.imageView).setSelected(false);
((TextView) indicatorViewPager.getIndicatorView().getItemView(preItem).findViewById(R.id.textView)).setTextColor(getResources().getColor(R.color.black2));
}
indicatorViewPager.getIndicatorView().getItemView(currentItem).findViewById(R.id.imageView).setPressed(true);
((TextView) indicatorViewPager.getIndicatorView().getItemView(currentItem).findViewById(R.id.textView)).setTextColor(getResources().getColor(R.color.text_red));
}
});
((TextView) indicatorViewPager.getIndicatorView().getItemView(0).findViewById(R.id.textView)).setTextColor(getResources().getColor(R.color.text_red));
indicatorViewPager.setCurrentItem(0,true);
}
privateIndicatorViewPager.IndicatorFragmentPagerAdapter adapter =newIndicatorViewPager.IndicatorFragmentPagerAdapter(getSupportFragmentManager()) {
privateint[] images = {R.drawable.main0_select, R.drawable.main1_select, R.drawable.main2_select, R.drawable.main3_select, R.drawable.main4_select};
privateint[] texts = {R.string.main0, R.string.main1, R.string.main2, R.string.main3, R.string.main4};
privateFragment[] fragments = {MainFragment.newInstance(), ScenicFragment.newInstance(), ServiceFragment.newInstance(), ShopFragment.newInstance(), MineFragment.newInstance()};
@Override
publicintgetCount() {
returnimages.length;
}
@Override
publicView getViewForTab(intposition, View convertView, ViewGroup container) {
if(convertView ==null) {
convertView = inflater.inflate(R.layout.main_bottom_item, container,false);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
imageView.setImageResource(images[position]);
TextView textView = (TextView) convertView.findViewById(R.id.textView);
textView.setText(texts[position]);
}
returnconvertView;
}
@Override
publicFragment getFragmentForPage(intposition) {
returnfragments[position];
}
};
}
网友评论