Dom*_*kTV 5 android android-tabs android-design-library
我有一个简单的东西AppCompatActivity,一个简单的东西Toolbar(android.support.v7.widget.Toolbar),一个设计库TabLayout和一个ViewPager。
现在,在这种情况下,我有4个片段,它们位于viewpager内部,可以从侧面滑动。很好 但是,一旦我使用mTabLayout.setupWithViewPager(mViewPager);,图标就会消失,但是我可以在空白处进行切换,效果很好。如果我删除该行,图标将再次可见,“滑动”将起作用,但是如果我使用Tab键选择图标,则不会发生任何事情。
我的密码
主要活动
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
private TabLayout mTabLayout;
private ViewPager mViewPager;
private ViewPagerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initToolbar();
initTabLayout();
initViewPager();
mTabLayout.setupWithViewPager(mViewPager);//Here is the code from above
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
}
private void initToolbar() {
mToolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
private void initTabLayout() {
mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.ic_fire_white));
mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.ic_apps_white));
mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.ic_account_plus_white));
mTabLayout.addTab(mTabLayout.newTab().setIcon(R.drawable.ic_help_white));
}
private void initViewPager() {
mAdapter = new ViewPagerAdapter(getSupportFragmentManager(), MainActivity.this);
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mViewPager.setAdapter(mAdapter);
}
//MENUE STUFF
}
//ViewPagerAdapter
class ViewPagerAdapter extends FragmentStatePagerAdapter {
public static final int TABS_COUNT = 4;
private Context context;
public ViewPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: return new HomeFragment1();
case 1: return new HomeFragment2();
case 2: return new HomeFragment3();
case 3: return new HomeFragment4();
}
return null;
}
@Override
public int getCount() {
return TABS_COUNT;
}
}
Run Code Online (Sandbox Code Playgroud)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary"
app:tabGravity="fill"
app:tabMode="fixed"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
setupWithViewPager()实例化Tab仅具有从 提供的标题ViewPager.Adapter。当然,你可以重写这个方法,如下所示:
public class IconicTabLayout extends TabLayout {
private TabViewProvider mTabViewProvider;
public interface TabViewProvider {
Tab newTabInstance(int tabPosition);
}
public IconicTabLayout(Context context) {
super(context);
}
public IconicTabLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public IconicTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setTabProvider(TabViewProvider provider){
mTabViewProvider = provider;
}
@Override
public void setupWithViewPager(ViewPager viewPager) {
PagerAdapter adapter = viewPager.getAdapter();
if (adapter == null) {
throw new IllegalArgumentException("ViewPager does not have a PagerAdapter set");
} else {
this.setTabsFromPagerAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(this));
this.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
}
}
@Override
public void setTabsFromPagerAdapter(PagerAdapter adapter) {
removeAllTabs();
mTabViewProvider = mTabViewProvider == null && adapter instanceof TabViewProvider? (TabViewProvider)adapter : null;
if(mTabViewProvider == null){
super.setTabsFromPagerAdapter(adapter);
}else{
for (int i = 0, count = adapter.getCount(); i < count; ++i) {
this.addTab(mTabViewProvider.newTabInstance(i));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
mIconicTabLayout.setTabProvider(new TabViewProvider() {
@Override
public Tab newTabInstance(int tabPosition) {
return mIconicTabLayout.newTab().setIcon(icons[position]);
}
});
mIconicTabLayout.setupWithViewPager(mViewPager);
Run Code Online (Sandbox Code Playgroud)
或者
class ViewPagerAdapter extends FragmentStatePagerAdapter implements TabViewProvider{
public void setTabLayout(TabLayout t){
mTabLayout = t;
}
//...
@Override
public Tab newTabInstance(int position) {
return mTabLayout.newTab().setIcon(icons[position]);
}
}
Run Code Online (Sandbox Code Playgroud)
注意:确保您首先调用setTabProvider方法或您的PagerAdapter实现TabViewProvider,否则它将调用默认实现。