Section Page Adapter getCurrentItem title

时间:2016-03-31 12:28:59

标签: java android

我有tab layoutinstances fragment listcurrent page中有不同的数据。我希望获得data,以便我可以使用标题更改mViewPager.getCurrentItem())。问题是:method oncreateview并不总是被调用。

jours2 = new ArrayList<>(); rv = (RecyclerView) rootView.findViewById(R.id.recyclerView); rv.setLayoutManager(new LinearLayoutManager(getContext())); adapter = new MyAdapter(jours2); rv.setAdapter(adapter); String test= mSectionsPagerAdapter.getPageTitle(mViewPager.getCurrentItem()).toString(); for (Cours c : jours) { if (c.jour==test) { jours2.add(c); } } adapter.notifyDataSetChanged(); 我有:

current

所以我可以更改我的代码,以便始终获得titlepublic class SectionsPagerAdapter extends FragmentPagerAdapter { ArrayList<Fragment> fragments = new ArrayList<>(); ArrayList<String> titles = new ArrayList<>(); public SectionsPagerAdapter(FragmentManager fm) { super(fm); } public void AddFragment(Fragment fragment,String title) { fragments.add(fragment); titles.add(title); } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class below). return fragments.get(position); } @Override public int getCount() { // Show 3 total pages. return fragments.size(); } @Override public CharSequence getPageTitle(int position) { return titles.get(position); } }

SectionPageAdapter:

octdect($number)

1 个答案:

答案 0 :(得分:0)

你可以使用它来获得位置

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
              tab.getPosition();     
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

或者您只需使用tabLayout.getSelectedTabPosition()