使用View Pager选择了错误的片段

时间:2016-07-28 10:21:40

标签: android android-fragments view

我使用View Pager显示三个不同的片段。我已将片段添加到适配器,如下所示。显示的标题是正确的,但点击它选择错误的片段

 private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        adapter.addFragment(new AcceptsFragment(), "Accepts");
        adapter.addFragment(new AskedFragment(), "Asked");
        adapter.addFragment(new HelpedFragment(), "Helped");
        viewPager.setAdapter(adapter);
    } 

这是我的适配器实现。

class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            Log.d("Fragment Clicked ",String.valueOf(position));
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            //return mFragmentList.size();
            return mFragmentist.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

0 个答案:

没有答案