use of com.litingzhe.justandroid.main.fragment.DesignPatternFragment in project JustAndroid by chinaltz.
the class MainActivity method InitView.
//初始化View
private void InitView() {
viewPager.setOffscreenPageLimit(4);
fragmentList = new ArrayList<Fragment>();
homeBaseUIFragment = new HomeBaseUIFragment();
netAndDbDataFragment = new NetAndDBDataFragment();
designPatternFragment = new DesignPatternFragment();
shopFragment = new ShopFragment();
otherUtilsFragment = new OtherUtilsFragment();
fragmentList.add(homeBaseUIFragment);
fragmentList.add(netAndDbDataFragment);
fragmentList.add(designPatternFragment);
fragmentList.add(shopFragment);
fragmentList.add(otherUtilsFragment);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
titleList = new String[] { "UI", "Net&DB", "设计模式", "购物车", "其他" };
MainFragmentAdapter adapter = new MainFragmentAdapter(getSupportFragmentManager(), titleList, fragmentList);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
//设置ViewPager 是否可以滑动
viewPager.setPagingEnabled(true);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < titleList.length; i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
View view = tab.getCustomView();
ImageView img = (ImageView) view.findViewById(R.id.tab_icon);
TextView title = (TextView) view.findViewById(R.id.tab_title);
if (position == i) {
img.setImageResource(icons_press[i]);
title.setTextColor(getResources().getColor(R.color.mainColor));
} else {
img.setImageResource(icons[i]);
title.setTextColor(getResources().getColor(R.color.gray));
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
//为TabLayout添加tab名称
for (int i = 0; i < titleList.length; i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(getTabView(i));
}
viewPager.setCurrentItem(0);
}
Aggregations