android-view-pager-back函数在前两个选项卡中不起作用

e5nszbig  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(174)

我在活动中使用了带有表格布局的viewpager。它有3个选项卡,问题是当选中的选项卡1和2“后退”按钮不起作用时,它无法转到上一个活动。在第三个选项卡中,后退按钮正在工作,我可以访问上一个活动。
我在onback函数中给出了finish代码。然后它也不会进入上一个活动。
下面的代码描述了如何在活动中设置viewpager和tablayout。告诉我如何设置标签1和标签2的后退按钮。

class MainActivity
{

    TabLayout.Tab firsttab = tabs.newTab();
    firsttab.setText("Personal Details");
    tabs.addTab(firsttab);

    TabLayout.Tab secondtab = tabs.newTab();
    secondtab.setText("Employment");
    tabs.addTab(secondtab);

    TabLayout.Tab thirdtab = tabs.newTab();
    thirdtab.setText("Documents");
    tabs.addTab(thirdtab);

    Servicepageadapter adapter = new Servicepageadapter(getSupportFragmentManager(),tabs.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs));
    tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            tapposition = tab.getPosition();

            viewPager.setCurrentItem(tab.getPosition());
        }

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

        }

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

        }
    });

    private class Servicepageadapter extends FragmentPagerAdapter {
        int mNumOfTabs ;
        public Servicepageadapter(FragmentManager fragmentManager, int tabCount) {
            super(fragmentManager);
            this.mNumOfTabs = tabCount;
        }

        @Override
        public Fragment getItem(int position) {

            switch (position){
                case 0:
                    boolean  isbackstack = true;
                    PersonalFragment tab1 = new PersonalFragment().newInstance("", "","");
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

                    if (!isbackstack) {
                        transaction.addToBackStack(null);
                    }
                    transaction.commit();
                   return tab1;

                case 1:

                    boolean  isbackstack1 = true;
                    EmploymentFragment tab2 = new EmploymentFragment().newInstance("", "","");
                    FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();

                    if (!isbackstack1) {
                        transaction1.addToBackStack(null);
                    }
                    transaction1.commit();
                   return tab2;

                case 2:
                    boolean isbackstack2 = true;
                    DocumentsFragment tab3 = new DocumentsFragment().newInstance("", "","");
                    FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
                    if (!isbackstack2) {
                        transaction2.addToBackStack(null);
                    }
                    transaction2.commit();
                    return tab3;
                default:
                    return null;
            }

        }

        @Override
        public int getCount() {
            return mNumOfTabs;
        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题