使用ViewPager后TabItem图标消失
问题描述:
我的TabLayout是完美的,显示图标,然后我不得不添加一个ViewPagerOnTabSelectedListener,并且我的图标丢失。
有人可以帮我吗?使用ViewPager后TabItem图标消失
我试图解决方案,如在我的MainActivity加入这一点,但什么都没有改变:
tabLayout.getTabAt(0).setIcon(R.drawable.home);
这是我的代码:
public class MainActivity extends AppCompatActivity {
private ViewPager pager;
private BottomNavigationView navigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.pager);
navigation = (BottomNavigationView) findViewById(R.id.navigation);
BottomNavigationViewHelper.disableShiftMode(navigation);
TabLayout tabLayout = new TabLayout(this);
tabLayout = (TabLayout) findViewById(R.id.toolbar);
tabLayout.setupWithViewPager(pager);
tabLayout.addOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(pager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
pager.setCurrentItem(tab.getPosition());
Log.i("TAG", "onTabSelected: " + tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
final PagerAdapter mAdapter = new MainPagerAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_adresse:
pager.setCurrentItem(0);
return true;
case R.id.navigation_cognitif:
pager.setCurrentItem(1);
return true;
case R.id.navigation_deplacement:
pager.setCurrentItem(2);
return true;
case R.id.navigation_yeux:
pager.setCurrentItem(3);
return true;
}
return false;
}
});
}
class MainPagerAdapter extends FragmentPagerAdapter {
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MeFragment();
case 1:
return new MeFragment();
case 2:
return new MeFragment();
case 3:
default:
return new MeFragment();
}
}
@Override
public int getCount() {
return 4;
}
}
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.blabla.bloublou.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="?attr/colorPrimary">
<android.support.design.widget.TabItem
android:id="@+id/home_button"
android:icon="@drawable/home"
android:title="home"/>
<android.support.design.widget.TabItem
android:id="@+id/me_button"
android:icon="@drawable/me"
android:title="moi" />
<android.support.design.widget.TabItem
android:id="@+id/configuration_button"
android:icon="@drawable/tools"
android:title="configuration"
/>
<android.support.design.widget.TabItem
android:id="@+id/help_button"
android:icon="@drawable/help"
android:title="aide"/>
</android.support.design.widget.TabLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="@color/colorPrimary"
app:itemIconTint="@color/icon"
app:itemTextColor="@color/icon"
app:menu="@menu/navigation">
</android.support.design.widget.BottomNavigationView>
<com.umanit.emfsc.NonSwipeableViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/navigation"
android:layout_below="@+id/toolbar"
/>
<include layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
android:layout_above="@id/navigation"
/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
答
tabLayout.setupWithViewPager(pager);
tabLayout.addOnTabSelectedListener(
new TabLayout.ViewPagerOnTabSelectedListener(pager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
pager.setCurrentItem(tab.getPosition());
Log.i("TAG", "onTabSelected: " + tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
final PagerAdapter mAdapter = new MainPagerAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
tabLayout.setupWithViewPager(pager);
作出以上更改,问题我如果您在寻呼机设置了适配器之前设置了viewpager,请告诉我这是否无效。
谢谢你的帮助,但它仍然不起作用:( – Racine2Ju
确定我改变了代码,就像你说的那样,然后添加这个,它的工作! tabLayout.getTabAt(0).setIcon(R.drawable.home) ; tabLayout.getTabAt(1).setIcon(R.drawable.me); tabLayout.getTabAt(2).setIcon(R.drawable.tools); tabLayout.getTabAt(3).setIcon(R.drawable.help ); – Racine2Ju
如果它可以帮助你将它标记为接受,谢谢你,快乐的编码 –