Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当从android中的另一个片段返回时,如何使TabLayout中的选定选项卡保持选中状态?_Android_Android Fragments_Android Viewpager_Android Tablayout - Fatal编程技术网

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

当从android中的另一个片段返回时,如何使TabLayout中的选定选项卡保持选中状态?,android,android-fragments,android-viewpager,android-tablayout,Android,Android Fragments,Android Viewpager,Android Tablayout,这是我的应用程序的屏幕截图 该应用程序包含一个活动,其中包含一个框架布局和底部的四个按钮 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <

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

该应用程序包含一个活动,其中包含一个框架布局和底部的四个按钮

<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>
内容片段

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

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

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

我就是这样解决的:

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

    public class Constant {
    public static final Map<String,Integer> fragmentTabs = new HashMap<>();
}
    public class Constant {
    public static final Map<String,Integer> fragmentTabs = new HashMap<>();
}
    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) {
    }
});