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

How to keep selected tab in TabLayout remain selected when returned from another fragment in android?

这是我的应用程序的屏幕截图

该应用程序包含一个 activity,其中包含一个 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>

在每个按钮上单击 activity 向 FrameLayout 添加一个片段(目前我只使用一个名为 ContentFragment 的片段 class)。

片段包含一个 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. 用户转到按钮 3 并选择选项卡 2。
  3. 用户返回按钮一,应该会发现 ViewPager 选项卡 1 保持选中状态。
  4. 转到按钮三,应该会发现选项卡 2 保持选中状态。

如何做到这一点?

这是我的 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;
        }
    }
}

内容片段

    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;
    }
}

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

我是这样解决的:

创建了一个常量 class,其中包含用于在单击底部栏中的按钮时添加的每个片段中存储当前选定选项卡的字段

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

然后在 ContentFragment 中 class 在 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) {
    }
});