click on tab layout doesn't work at all
Asked Answered
T

3

6

I have implemented this example.

but not able to click on 2nd tab.

my xml file looks like

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/header"
    android:orientation="horizontal">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:layout_below="@+id/header"
        android:background="@color/colorPrimary"
        app:tabSelectedTextColor="#ffffff" />

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_marginTop="@dimen/padding"
        android:layout_height="wrap_content"
        android:layout_below="@id/tab_layout" />

</LinearLayout>

and Main activity

 pager= (ViewPager) findViewById(R.id.view_pager);
    tabLayout= (TabLayout) findViewById(R.id.tab_layout);

    FragmentManager manager=getSupportFragmentManager();
    BuyCurrencyPagerAdapter adapter=new BuyCurrencyPagerAdapter(manager);

    //set Adapter to view pager
    pager.setAdapter(adapter);
    tabLayout.setupWithViewPager(pager);
    pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.setTabsFromPagerAdapter(adapter);

please give any suggestions.

Trotskyism answered 21/12, 2015 at 13:56 Comment(1)
Look at this question because it has each code what you're tying to do https://mcmap.net/q/579597/-android-viewpager-and-tablayout-are-not-working-fast/2404262Tildi
P
9

Sometimes I believe the problem might be that the ViewPager is positioned above the TabLayout. You want to place it below like this:

<AppBarLayout>
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"/>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"/>
Plaintive answered 5/5, 2016 at 11:52 Comment(2)
I had this issue where the ViewPager and AppBarLayout were placed in a RelativeLayout. Changing it to LinearLayout solve the issue.Serapis
I wasted hours to resolve this. Replaced module many times and searched a lot too but could not find solution but this simple fix of correcting position of viewpager and tablayout fixed it. I was using relativelayout and it was just overlapping which was not visible by looking at it as fragment loaded was blank.Boigie
S
5

UPDATE: setOnTabSelectedListener deprecated, use addOnTabSelectedListener

Try this:

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
    @Override
        public void onTabSelected(TabLayout.Tab tab) {
        pager.setCurrentItem(tab.getPosition());
    }

});

If it's not working in your case, check your layout. May be there is a view overlapping your TabLayout.

Spinet answered 21/12, 2015 at 14:1 Comment(1)
worked for me with addOnTabSelectedListener() version of this.Huntingdonshire
D
0

As this method is drepecatad now, you can use the new method

addOnTabSelectedListener(OnTabSelectedListener)

Danonorwegian answered 30/8, 2016 at 11:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.