Tabview does not change tab on touch

大城市里の小女人 提交于 2021-02-11 14:57:22

问题


I have looked around google and stack overflow to find a solution to this but I can't seem to understand why I can't get it to work, I follow everything I see. my problem here is that whenever I touch on a tab it does not change to the tab I have chosen. looking at the moment I am following directions from [This Stackoverflow post][1] Please note that the tabs work when swiping perfectly

Here is the XML for the tabview.

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget" />
    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tabs"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

I have added this as well in my main activity. (container is for my recycler view)

  TabLayout tabLayout = findViewById(R.id.tabs);
        viewPager = findViewById(R.id.container);  
        setupViewPager(viewPager);
        tabLayout.setupWithViewPager(viewPager);

also in my main activity, I have this Method to create my tabs. (the tabnames and fragment names where edited in this post)

    private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
    adapter.addFragment(new 1_Fragment(),"tab1");
    adapter.addFragment(new 2_Fragment(),"tab2");
    adapter.addFragment(new 3_Fragment(),"tab3");
    adapter.addFragment(new 4_Fragment(),"tab4");
    adapter.addFragment(new 5_Fragment(),"tab5");
    adapter.addFragment(new 6_Fragment(),"tab6");
    adapter.addFragment(new 7_Fragment(),"tab7");
    viewPager.setAdapter(adapter);
}

also In the main activity, I have this but it does not seem to work on click. though with a Log.d I checked and it works when swiping.

   tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
          @Override
          public void onTabSelected(TabLayout.Tab tab) {
              viewPager.setCurrentItem(tab.getPosition());
              Log.d("Tab", String.valueOf(tab.getPosition()));
          }

          @Override
          public void onTabUnselected(TabLayout.Tab tab) {

          }

          @Override
          public void onTabReselected(TabLayout.Tab tab) {

          }
      });

回答1:


Use Relative layout in parent or CoordinatorLayout as below code.

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/color_white"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

    </android.support.v7.widget.Toolbar>


    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="5dp"
        android:background="@color/color_light_blue_1"
        android:paddingRight="2dp"
        android:paddingTop="2dp"
        app:tabBackground="@drawable/tab_selector_products"
        app:tabIndicatorHeight="0dp"
        app:tabTextAppearance="@style/MineCustomTabText" />

</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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />




回答2:


   <?xml version="1.0" encoding="utf-8"?>
   <android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tabs"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget" />

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

Move TabLayout to bottom of ViewPager



来源:https://stackoverflow.com/questions/51629468/tabview-does-not-change-tab-on-touch

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