Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking on tabs does not switch current tab when using Android TabLayout

Tags:

Am I supposed to set onClicks myself, and if so, where should I do that?

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  <android.support.design.widget.TabLayout     android:id="@+id/sliding_tabs"     android:layout_width="match_parent"     android:layout_height="wrap_content"     app:tabMode="scrollable" />  <android.support.v4.view.ViewPager     android:id="@+id/vpPager"     android:layout_width="match_parent"     android:layout_height="wrap_content"> </android.support.v4.view.ViewPager> 

like image 508
Felipe Calderon Avatar asked Aug 15 '15 03:08

Felipe Calderon


People also ask

Can we use TabLayout without ViewPager in Android?

It is possible to use a TabLayout without a ViewPager by using a TabLayout. OnTabSelectedListener . For navigation within an Activity , manually populate the UI based on the tab selected.

How do I disable TabLayout click?

the method has slightly different on the time of its invocation, you just have to setup your tabitem to disable its click after all viewpager fragment already added.


2 Answers

The main problem is, the ViewPager overlays the TabLayout. So, you should place it below TabLayout:

<androidx.viewpager.widget.ViewPager     android:id="@+id/viewpager"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_below="@+id/sliding_tabs"/> 

And from the Activity's onCreate method, call:

TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs); tabLayout.setupWithViewPager(viewPager); 

For more details, check this example:

https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/java/com/support/android/designlibdemo/MainActivity.java

https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/res/layout/include_list_viewpager.xml

like image 105
Anggrayudi H Avatar answered Nov 07 '22 12:11

Anggrayudi H


Try setting the viewpagers current item using the tab layouts tab selected listener.

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()     {         @Override         public void onTabSelected(TabLayout.Tab tab)         {             viewPager.setCurrentItem(tab.getPosition());         }          @Override         public void onTabUnselected(TabLayout.Tab tab)         {          }          @Override         public void onTabReselected(TabLayout.Tab tab)         {          }     }); 
like image 35
Dom Avatar answered Nov 07 '22 13:11

Dom