选项卡中的 setCurrentItem 不起作用
setCurrentItem in tab not working
我的 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)
。但它不起作用。我做错了什么?
如果您的纵向和横向布局相同,那么您可以在 MainActivity 的清单文件中使用这行代码。
android:configChanges="orientation|keyboardHidden|screenSize"
您甚至不需要跟踪选项卡的当前位置。希望这有帮助。
我的 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)
。但它不起作用。我做错了什么?
如果您的纵向和横向布局相同,那么您可以在 MainActivity 的清单文件中使用这行代码。
android:configChanges="orientation|keyboardHidden|screenSize"
您甚至不需要跟踪选项卡的当前位置。希望这有帮助。