android - Icons in TabLayout -
i have simple appcompatactivity
1 simple toolbar
(android.support.v7.widget.toolbar), 1 design-library tablayout
, 1 viewpager
.
now have in case 4 fragments inside viewpager , can swiped sides. works fine. use mtablayout.setupwithviewpager(mviewpager);
, icons disappear can tab on empty spaces works fine. if remove line, icons visible again, "swipe" works if tab icon, nothing happens.
my code
mainactivity
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 code 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; } }
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>
setupwithviewpager()
instantiate tab
have title provided viewpager.adapter
. of course can override method this:
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 not have 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{ (int = 0, count = adapter.getcount(); < count; ++i) { this.addtab(mtabviewprovider.newtabinstance(i)); } } } }
usage:
miconictablayout.settabprovider(new tabviewprovider() { @override public tab newtabinstance(int tabposition) { return miconictablayout.newtab().seticon(icons[position]); } }); miconictablayout.setupwithviewpager(mviewpager);
or
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]); } }
note: make sure first call settabprovider
method or pageradapter
implement tabviewprovider
, otherwise call default implementation.
Comments
Post a Comment