在TabView中,视图分页器不会删除所选的标签片段,而是会删除最后一个标签片段

时间:2018-09-19 10:58:41

标签: java android

在我的活动中,我具有TabLayout,查看寻呼机,添加标签和删除标签的功能。一切正常,但是当我单击“删除”选项卡按钮时,我通过了选定的选项卡位置,但最后一个选项卡被删除。关于选项卡名称,所选的选项卡名称将被删除。

removeExpenseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tabs.getTabCount() != 1) {
                    int i = tabs.getSelectedTabPosition();
                    modeAdapter.removeFragment(i);
                    viewPager.setAdapter(modeAdapter);
                    modeAdapter.notifyDataSetChanged();
                    tabNameChanger();
                } else {
                    onBackPressed();
                }
            }
        });

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

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

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

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

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

    public void removeFragment(int position) {
        mFragmentTitleList.remove(position);
        mFragmentList.remove(position);
    }

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



private void setupViewPager(ViewPager viewPager) {

        modeAdapter = new  ReqCreationActivity.PayModeAdapter(getSupportFragmentManager());
        modeAdapter.addFragment(new ReqDetailFragment(), "Expense");
        viewPager.setAdapter(modeAdapter);
        modeAdapter.notifyDataSetChanged();
    }

0 个答案:

没有答案