我正在使用TabLayout,它根据在ViewPager中选择的片段显示选项卡图标红色。但我已经面临这个问题2周了,我一直在寻找解决这个问题的方法。
问题是如果我在片段用户帐户,当我关闭应用程序时,我的用户选项卡图标被选中。现在,当我重新打开应用程序片段时,我的标签餐馆已加载,但仍然选择了用户帐户标签图标,但应选择餐馆标签图标。
我附上了截图供参考:
restaurant image after re open
public class PagerMainActivity extends Fragment {
//This is our tablayout
public TabLayout tabLayout;
public static CustomViewPager viewPager;
AdapterPager adapter;
public int tabIconColor_Selected;
public int tabIconColor_DeSelected;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// tabSelection();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_pager, container, false);
tabLayout = (TabLayout) v.findViewById(R.id.tab_layout);
viewPager = v.findViewById(R.id.pager);
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.restaurent).setText("Restaurant"));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.order).setText("Order"));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.deals).setText("Deals"));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.account).setText("Account"));
viewPager = v.findViewById(R.id.pager);
adapter = new AdapterPager(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
adapter.getRegisteredFragment(viewPager.getCurrentItem());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
viewPager.setAllowedSwipeDirection(SwipeDirection.none);
// setupTabIcons();
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
@Override
public void onTabSelected(TabLayout.Tab tabSelected)
{
viewPager.setCurrentItem(tabSelected.getPosition());
if(UserAccountFragment.LOG_OUT_FLAG || UserAccountFragment.LOG_IN_FLAG){
tabIconColor_Selected = ContextCompat.getColor(getContext(), R.color.colorDeal);
tabSelected.getIcon().setColorFilter(tabIconColor_Selected, PorterDuff.Mode.SRC_IN);
UserAccountFragment.LOG_OUT_FLAG=false;
UserAccountFragment.LOG_IN_FLAG=false;
}
tabIconColor_Selected = ContextCompat.getColor(getContext(), R.color.colorRed);
tabSelected.getIcon().setColorFilter(tabIconColor_Selected, PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabUnselected(TabLayout.Tab tabSelected){
viewPager.setCurrentItem(tabSelected.getPosition());
tabIconColor_DeSelected = ContextCompat.getColor(getContext(), R.color.colorDeal);
tabSelected.getIcon().setColorFilter(tabIconColor_DeSelected, PorterDuff.Mode.SRC_IN);
}
@Override
public void onTabReselected(TabLayout.Tab tabSelected){
}
});
return v;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
适配器
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
int mNumOfTabs;
public AdapterPager(FragmentManager fragmentManager, int tabCount) {
super(fragmentManager);
this.mNumOfTabs=tabCount;
}
@Override
public Fragment getItem(int position) {
Fragment fm=null;
switch (position) {
case 0:
fm = new RestaurantsFragment();
break;
case 1:
fm = new OrdersFragment();
break;
case 2:
fm = new DealsFragment();
break;
case 3:
fm = new UserAccountFragment();
break;
}
return fm;
}
@Override
public int getCount() {
return mNumOfTabs;
}
/* @Override
public Parcelable saveState() {
// Do Nothing
return saveState();
}*/
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
您的意见和解决方案将不胜感激。提前致谢