选项卡中的setCurrentItem不起作用

时间:2016-01-12 12:29:51

标签: android android-tablayout

我的MainActivity中有一个使用TabLayout的片段。我使用了三个以上的片段来表示3个标签。我想保存instant state,以便当前标签在更改方向时保持选择状态。 这是我的主片段TabLayout

public class HomeFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    CharSequence titles[];
    int numOfTabs;

    private static final String CURRENT_SELECTED_TAB_INDEX = "selectedTabIndex";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ViewPagerAdaptar pagerAdaptar;

    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        titles = getResources().getStringArray(R.array.tabs);
        numOfTabs = titles.length;

        pagerAdaptar = new ViewPagerAdaptar(getFragmentManager(), titles, numOfTabs);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (viewPager != null) {
            int i = viewPager.getCurrentItem();
            outState.putInt(CURRENT_SELECTED_TAB_INDEX, i);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View homeFragment = inflater.inflate(R.layout.fragment_home, container, false);

        viewPager = (ViewPager) homeFragment.findViewById(R.id.viewpager);
        viewPager.setAdapter(pagerAdaptar);

        tabLayout = (TabLayout) homeFragment.findViewById(R.id.tablayout);
        tabLayout.setupWithViewPager(viewPager);

        if (savedInstanceState != null) {
            int selectedTab = savedInstanceState.getInt(CURRENT_SELECTED_TAB_INDEX, 0);
            viewPager.setCurrentItem(selectedTab);
        }

        return homeFragment;
    }

}

这是我的适配器

public class ViewPagerAdaptar extends FragmentStatePagerAdapter {
    CharSequence titles[];
    int numberOfTabs;

    public ViewPagerAdaptar(FragmentManager fm, CharSequence mTitles[], int mNumberOfTabs) {
        super(fm);

        this.titles = mTitles;
        this.numberOfTabs = mNumberOfTabs;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch (position){
            case 0:
                fragment = new First();
                break;
            case 1:
                fragment = new Second();
                break;
            case 2:
                fragment = new Third();
                break;
        }
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

我使用savedInstantState获取最后选择的位置并将索引传递给setCurrentItem(index)。但它没有用。我做错了什么?

1 个答案:

答案 0 :(得分:2)

如果您的纵向和横向布局相同,那么您可以在清单文件中使用这行代码作为MainActivity。

 android:configChanges="orientation|keyboardHidden|screenSize"

您甚至无需跟踪标签当前位置。希望这会有所帮助。