Tabview 不会在触摸时更改标签 [英] Tabview does not change tab on touch

查看:27
本文介绍了Tabview 不会在触摸时更改标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾了谷歌和堆栈溢出以找到解决方案,但我似乎无法理解为什么我无法让它工作,我遵循我所看到的一切.我的问题是,每当我触摸选项卡时,它都不会更改为我选择的选项卡.看看我正在按照 [This Stackoverflow post][1] 中的指示进行操作的那一刻,请注意标签在完美滑动时可以正常工作

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

这是 tabview 的 XML.

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);
}

也在主要活动中,我有这个,但它似乎在点击时不起作用.虽然我检查了 Log.d,但它在滑动时有效.

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) {

          }
      });

推荐答案

   <?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>

TabLayout 移动到 ViewPager

这篇关于Tabview 不会在触摸时更改标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆