click on tab layout doesn't work at all

房东的猫 提交于 2019-12-07 05:23:24

问题


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.


回答1:


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"/>



回答2:


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.




回答3:


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

addOnTabSelectedListener(OnTabSelectedListener)



来源:https://stackoverflow.com/questions/34397381/click-on-tab-layout-doesnt-work-at-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!