Search in sources :

Example 1 with MainFragmentAdapter

use of com.litingzhe.justandroid.main.adapter.MainFragmentAdapter 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);
}
Also used : ShopFragment(com.litingzhe.justandroid.main.fragment.ShopFragment) OtherUtilsFragment(com.litingzhe.justandroid.main.fragment.OtherUtilsFragment) DesignPatternFragment(com.litingzhe.justandroid.main.fragment.DesignPatternFragment) HomeBaseUIFragment(com.litingzhe.justandroid.main.fragment.HomeBaseUIFragment) NetAndDBDataFragment(com.litingzhe.justandroid.main.fragment.NetAndDBDataFragment) ShopFragment(com.litingzhe.justandroid.main.fragment.ShopFragment) Fragment(android.support.v4.app.Fragment) OtherUtilsFragment(com.litingzhe.justandroid.main.fragment.OtherUtilsFragment) HomeBaseUIFragment(com.litingzhe.justandroid.main.fragment.HomeBaseUIFragment) ViewPager(android.support.v4.view.ViewPager) AbViewPager(com.ningcui.mylibrary.viewLib.sample.AbViewPager) ImageView(android.widget.ImageView) BindView(butterknife.BindView) View(android.view.View) TextView(android.widget.TextView) DesignPatternFragment(com.litingzhe.justandroid.main.fragment.DesignPatternFragment) NetAndDBDataFragment(com.litingzhe.justandroid.main.fragment.NetAndDBDataFragment) MainFragmentAdapter(com.litingzhe.justandroid.main.adapter.MainFragmentAdapter) TabLayout(android.support.design.widget.TabLayout) TextView(android.widget.TextView) ImageView(android.widget.ImageView)

Aggregations

TabLayout (android.support.design.widget.TabLayout)1 Fragment (android.support.v4.app.Fragment)1 ViewPager (android.support.v4.view.ViewPager)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 BindView (butterknife.BindView)1 MainFragmentAdapter (com.litingzhe.justandroid.main.adapter.MainFragmentAdapter)1 DesignPatternFragment (com.litingzhe.justandroid.main.fragment.DesignPatternFragment)1 HomeBaseUIFragment (com.litingzhe.justandroid.main.fragment.HomeBaseUIFragment)1 NetAndDBDataFragment (com.litingzhe.justandroid.main.fragment.NetAndDBDataFragment)1 OtherUtilsFragment (com.litingzhe.justandroid.main.fragment.OtherUtilsFragment)1 ShopFragment (com.litingzhe.justandroid.main.fragment.ShopFragment)1 AbViewPager (com.ningcui.mylibrary.viewLib.sample.AbViewPager)1