java - Can't get swipe working for swipeable tabs -
i following http://www.android4devs.com/2015/01/how-to-make-material-design-sliding-tabs.html , have same code. can't tabs swipe each other , can't view content in tabs. code below. help!
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:fab="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="activity.mainactivity"> <include android:id="@+id/toolbar" layout="@layout/tool_bar" /> <slidingmodel.slidingtablayout android:layout_below="@+id/toolbar" android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:elevation="4dp" android:background="@color/primary_color" /> <android.support.v4.view.viewpager android:layout_below="@id/tabs" android:id="@+id/pager" android:layout_height="0dp" android:layout_width="match_parent" /> <com.melnykov.fab.floatingactionbutton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="16dp" fab:fab_colornormal="@color/fab_normal" fab:fab_colorpressed="@color/fab_pressed" fab:fab_colorripple="@color/fab_ripple" android:layout_marginleft="16dp" android:layout_marginright="16dp" android:layout_alignparentbottom="true" android:layout_alignparentend="true" /> </relativelayout>
mainactivity.java
package activity; import android.support.v4.view.viewpager; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.toolbar; import android.view.menu; import android.view.menuitem; import com.gursimran.bei.forte.r; import adapter.viewpageradapter; import slidingmodel.slidingtablayout; public class mainactivity extends appcompatactivity { toolbar mtoolbar; viewpager pager; viewpageradapter adapter; slidingtablayout tabs; charsequence titles[] = {"factorial", "permutation", "random"}; int numboftabs = 3; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtoolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(mtoolbar); // creating viewpageradapter , passing fragment manager, titles fot tabs , number of tabs. adapter = new viewpageradapter(getsupportfragmentmanager(),titles,numboftabs); // assigning viewpager view , setting adapter pager = (viewpager) findviewbyid(r.id.pager); pager.setadapter(adapter); // assiging sliding tab layout view tabs = (slidingtablayout) findviewbyid(r.id.tabs); tabs.setdistributeevenly(true); // make tabs fixed set true, makes tabs space evenly in available width // setting custom color scroll bar indicator of tab view tabs.setcustomtabcolorizer(new slidingtablayout.tabcolorizer() { @override public int getindicatorcolor(int position) { return getresources().getcolor(r.color.fab_pressed); } }); // setting viewpager slidingtabslayout tabs.setviewpager(pager); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
viewpageradapter.java
package adapter; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentstatepageradapter; import fragment.factorialtab; import fragment.permutationtab; import fragment.randomtab; public class viewpageradapter extends fragmentstatepageradapter { charsequence titles[]; // store titles of tabs going passed when viewpageradapter created int numboftabs; // store number of tabs, passed when viewpageradapter created // build constructor , assign passed values appropriate values in class public viewpageradapter(fragmentmanager fm,charsequence mtitles[], int mnumboftabsumb) { super(fm); this.titles = mtitles; this.numboftabs = mnumboftabsumb; } //this method return fragment every position in view pager @override public fragment getitem(int position) { if(position == 0) // if position 0 returning first tab { factorialtab factorialtab = new factorialtab(); return factorialtab; } else if(position == 1) // having 2 tabs if position 0 must 1 returning second tab { permutationtab permutationtab = new permutationtab(); return permutationtab; } else { randomtab randomtab = new randomtab(); return randomtab; } } // method return titles tabs in tab strip @override public charsequence getpagetitle(int position) { return titles[position]; } // method return number of tabs tabs strip @override public int getcount() { return numboftabs; } }
swipe function control viewpager
.your viewpager layout_height 0dp in here that's why have problem .
so set way
android:layout_width="match_parent" android:layout_height="wrap_content"
Comments
Post a Comment