欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TabLayout+ViewPager+Fragment

程序员文章站 2024-03-24 20:20:46
...

这个是MainActivity

enter code herepublic class MainActivity extends AppCompatActivity {
    private TabLayout homeTl;
    private ViewPager homeVp;
    private ArrayList<Fragment> listHomeFragment;
    private HomeFragmentAdapter homeFragmentAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        assignViews();//找到控件
        initTbVp();//tab和vp关联
    }
    private void assignViews() {
        homeTl = (TabLayout) findViewById(R.id.home_tl);
        homeVp = (ViewPager) findViewById(R.id.home_vp);
    }
    private void initTbVp() {
        listHomeFragment = new ArrayList<Fragment>();
        listHomeFragment.add(new FirstFragment());
        listHomeFragment.add(new FirstFragment());
        listHomeFragment.add(new FirstFragment());
        listHomeFragment.add(new FirstFragment());
        listHomeFragment.add(new FirstFragment());
        listHomeFragment.add(new FirstFragment());
        listHomeFragment.add(new FirstFragment());
        //如果是在fragment中写TAb和Vp
        // homeFragmentAdapter = new HomeFragmentAdapter(getChildFragmentManager(), listHomeFragment);
        //如果是在activity中写TAb和Vp
        homeFragmentAdapter = new HomeFragmentAdapter(getSupportFragmentManager(), listHomeFragment);
        homeVp.setAdapter(homeFragmentAdapter);
        homeVp.addOnAdapterChangeListener(new ViewPager.OnAdapterChangeListener() {
            @Override
            public void onAdapterChanged(@NonNull ViewPager viewPager, @Nullable PagerAdapter oldAdapter, @Nullable PagerAdapter newAdapter) {
            }
        });
//      homeVp.getCurrentItem()
        homeTl.setupWithViewPager(homeVp);
        homeTl.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                System.out.print("HomeFragment....onTabSelected" + tab);
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                System.out.print("HomeFragment....onTabUnselected" + tab);
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                System.out.print("HomeFragment....onTabReselected" + tab);
            }
        });
    }
}

MainActivity的布局文件,垂直布局

<android.support.design.widget.TabLayout
    android:id="@+id/home_tl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabIndicatorColor="@color/colorPrimary"
    />
<android.support.v4.view.ViewPager
    android:layout_below="@id/home_tl"
    android:id="@+id/home_vp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

^[Viewpage的适配器]

enter code herepublic class HomeFragmentAdapter extends FragmentPagerAdapter {
    private ArrayList<Fragment> fragments;
    String[] title = {"全部", "计划", "在途", "到达", "回单", "审核", "完成"};
    private FragmentManager fm;

    public HomeFragmentAdapter(FragmentManager fm, ArrayList listHomeFragment) {
        super(fm);
        this.fm = fm;
        this.fragments = listHomeFragment;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {

        return title[position];
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        container.removeView((View) object);
        super.destroyItem(container, position, object);
    }

    //方法类似于listview的notifyDataSetChanged,刷新用
    public void setFragments(ArrayList fragments) {
        if (this.fragments != null) {
            FragmentTransaction ft = fm.beginTransaction();
            for (Fragment f : this.fragments) {
                ft.remove(f);
            }
            ft.commit();
            ft = null;
            fm.executePendingTransactions();
        }
        this.fragments = fragments;
        notifyDataSetChanged();
    }


}

viewpage中的fragment

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_first,container,false);
    }
}
相关标签: 新手