当从android中的另一个片段返回时,如何在TabLayout中保持选中的选项卡保持选中状态?

时间:2017-03-11 18:46:16

标签: android android-fragments android-viewpager android-tablayout

以下是我的应用的截屏

enter image description here

该应用包含一个活动,其中包含一个FrameLayout和底部的四个按钮。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <LinearLayout
        android:id="@+id/layoutParent"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="bottom"
        android:orientation="horizontal">
        <Button
            android:id="@+id/fragmentOne"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:clickable="true"
            android:gravity="center"
            android:textColor="@color/selector_task_bar_text"
            android:background="@drawable/selector_tab_layout_bg"
            android:layout_weight="1"
            android:text="One"/>
        <Button
            android:id="@+id/fragmentTwo"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:clickable="true"
            android:gravity="center"
            android:textColor="@color/selector_task_bar_text"
            android:background="@drawable/selector_tab_layout_bg"
            android:layout_weight="1"
            android:text="Two"/>
        <Button
            android:id="@+id/fragmentThree"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:clickable="true"
            android:gravity="center"
            android:textColor="@color/selector_task_bar_text"
            android:background="@drawable/selector_tab_layout_bg"
            android:layout_weight="1"
            android:text="Three"/>
        <Button
            android:id="@+id/fragmentFour"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:clickable="true"
            android:gravity="center"
            android:textColor="@color/selector_task_bar_text"
            android:background="@drawable/selector_tab_layout_bg"
            android:layout_weight="1"
            android:text="Four"/>
    </LinearLayout>

</LinearLayout>

在每个按钮单击时,活动会向FrameLayout添加一个片段(暂时我只使用一个名为ContentFragment的片段类)。

片段包含TabLayout和ViewPager,如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs_sub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/CustomTabLayout" />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

ViewPager有三个项目,用于在TabLayout中的选项卡上滑动或选择时显示不同的项目。目前,我已为ViewPager

中的所有页面添加了此布局
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textInfo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/app_name"
android:textSize="20sp"/>

现在我需要做的是当用户选择底部的特定按钮(一,二,三,四)和顶部的特定标签,然后切换到任何其他按钮,然后当他回去对于之前选择的按钮,他应该会发现TabLayout选项卡保持为该按钮选中。

例如:

  1. 用户选择按钮ONE和ViewPager选项卡1
  2. 用户转到按钮三并选择标签2.
  3. 用户返回按钮ONE,应该会发现ViewPager选项卡1仍处于选中状态。
  4. 转到按钮三,应该找到选项卡2保持选中状态。
  5. 如何做到这一点?

    这是我的MainActivity

        public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private static final String TAG = MainActivity.class.getSimpleName();
        private TextView fragmentOneButton;
        private TextView fragmentTwoButton;
        private TextView fragmentThreeButton;
        private TextView fragmentFourButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initLayout();
        }
    
        private void initLayout() {
            fragmentOneButton = (TextView) findViewById(R.id.fragmentOne);
            fragmentTwoButton = (TextView) findViewById(R.id.fragmentTwo);
            fragmentThreeButton = (TextView) findViewById(R.id.fragmentThree);
            fragmentFourButton = (TextView) findViewById(R.id.fragmentFour);
    
            fragmentOneButton.setOnClickListener(this);
            fragmentTwoButton.setOnClickListener(this);
            fragmentThreeButton.setOnClickListener(this);
            fragmentFourButton.setOnClickListener(this);
    
            if (getSupportFragmentManager().findFragmentById(R.id.fragment_container) == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.fragment_container,
                ContentFragment.newInstance(1,"TabOne")).commit();
            }
        }
    
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.fragmentOne:
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fragment_container, ContentFragment.newInstance(1,"TabOne")).commit();
                    break;
                case R.id.fragmentTwo:
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fragment_container, ContentFragment.newInstance(2,"TabTwo")).commit();
                    break;
                case R.id.fragmentThree:
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fragment_container, ContentFragment.newInstance(3,"TabThree")).commit();
                    break;
                case R.id.fragmentFour:
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.fragment_container, ContentFragment.newInstance(4,"TabFour")).commit();
                    break;
                default:
                    break;
            }
        }
    }
    

    ContentFragment

        public class ContentFragment extends android.support.v4.app.Fragment {
        private static final String TAG = ContentFragment.class.getSimpleName();
        private int mName = 0;
        private ViewPager mFragmentsPager;
        private TabLayout       mFragmentsTab;
        private PagerAdapter pagerAdapter;
        private String tabName = "";
    
        public static ContentFragment newInstance(int position,String tabName) {
            ContentFragment contentFragment = new ContentFragment();
            Bundle bundle = new Bundle();
            bundle.putString("TAB_NAME",tabName);
            bundle.putInt("KEY_POSITION",position);
            contentFragment.setArguments(bundle);
            return contentFragment;
        }
    
        public ContentFragment(){}
    
        @Override
        public void onAttachFragment(Fragment childFragment) {
            super.onAttachFragment(childFragment);
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.main_frag,container,false);
    
            final int position = getArguments().getInt("KEY_POSITION");
            tabName = getArguments().getString("TAB_NAME");
    
            initViews(view,position);
    
            return view;
        }
    
        private void initViews(View view, int position) {
            mName = position;
    
            mFragmentsPager = (ViewPager) view.findViewById(R.id.viewPager);
            mFragmentsPager.setOffscreenPageLimit(1);
    
            pagerAdapter = new FragmentStateAdapter(getChildFragmentManager());
    
            mFragmentsPager.setOffscreenPageLimit(3);
            mFragmentsPager.setAdapter(pagerAdapter);
    
            mFragmentsTab = (TabLayout) view.findViewById(R.id.tabs_sub);
            mFragmentsTab.setupWithViewPager(mFragmentsPager,false);
    
        }
    
        private class FragmentStateAdapter extends FragmentStatePagerAdapter {
            public FragmentStateAdapter(FragmentManager fragmentManager) {
                super(fragmentManager);
            }
    
            @Override
            public Fragment instantiateFragment(int position) {
                if (getFragment(position)!=null) {
                    return getFragment(position);
                } else {
                    return ViewPagerContentFragment.newInstance(position,tabName);
                }
    
            }
    
            @Override
            public int getCount() {
                return 3;
            }
    
            public CharSequence getPageTitle(int position) {
                return String.valueOf(position);
            }
    
    
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                super.destroyItem(container, position, object);
            }
        }
    }
    

    ViewPagerContentFragment

        public class ViewPagerContentFragment extends 
    android.support.v4.app.Fragment {
        private static final String TAG = ViewPagerContentFragment.class.getSimpleName();
        private TextView showOtherButton;
    
        public static ViewPagerContentFragment newInstance(int position,String tabName) {
            ViewPagerContentFragment contentFragment = new ViewPagerContentFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("KEY_POSITION",position);
            bundle.putString("TAB_NAME",tabName);
            contentFragment.setArguments(bundle);
            return contentFragment;
        }
    
        public ViewPagerContentFragment(){}
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View view = inflater.inflate(R.layout.pager_content,container,false);
    
            final int position = getArguments().getInt("KEY_POSITION");
            final String tabName = getArguments().getString("TAB_NAME");
    
            showOtherButton = (TextView) view.findViewById(R.id.textInfo);
            showOtherButton.setText(tabName+"\n"+position);
            showOtherButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
            return view;
        }
    }
    

    我无法用任何其他布局替换底栏。

1 个答案:

答案 0 :(得分:1)

这就是我解决它的方法:

使用字段创建一个Constant类,以便在单击底栏中的按钮时添加的每个片段中存储当前选定的选项卡

    public class Constant {
    public static final Map<String,Integer> fragmentTabs = new HashMap<>();
}

然后在ContentFragment类中,在initViews方法中添加了这些代码。

    final Bundle bundle = getFragmentManager().findFragmentById(R.id.fragment_container).getArguments();
try {
    if (Constant.fragmentTabs.containsKey(bundle.getString("TAB_NAME"))) {
        mFragmentsTab.getTabAt(Constant.fragmentTabs.get(bundle.getString("TAB_NAME"))).select();
    }
} catch (NullPointerException e) {
    e.printStackTrace();
}


mFragmentsPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        Constant.fragmentTabs.put(bundle.getString("TAB_NAME"),position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
});