【问题标题】:CoordinatorLayout and toolbar and TabLayout sometimes cannot work move right and left scrollCoordinatorLayout 和工具栏和 TabLayout 有时不能工作 左右滚动
【发布时间】:2017-11-15 07:58:26
【问题描述】:

我使折叠工具栏活动使用 CoordinatorLayout。

1。 main_activity

<?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"
    android:id="@+id/coordinatorLayout"
    android:background="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:background="?attr/colorPrimary"
            android:paddingLeft="@dimen/layout_margin"
            android:theme="@style/ThemeToolbar"
            app:layout_scrollFlags="scroll|enterAlways"
            app:layout_collapseMode="pin"/>
        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabTextColor="@color/text_space"
            app:tabSelectedTextColor="@android:color/white"
            app:tabIndicatorColor="@android:color/white"
            app:tabIndicatorHeight="2dp"/>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipe_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </android.support.v4.widget.SwipeRefreshLayout>
    <android.support.design.widget.FloatingActionButton
        android:visibility="gone"
        android:id="@+id/fabButton_menu"
        android:src="@drawable/plus"
        app:fabSize="normal"
        app:backgroundTint="@color/color_family"
        app:borderWidth="0dp"
        style="@style/FabStyle_Normal"/>
</android.support.design.widget.CoordinatorLayout>

2。代码

pagerAdapter = new PagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(new TabFragment_Home(), "T1");
pagerAdapter.addFragment(new TabFragment_Pharm(),"T2");
pagerAdapter.addFragment(new TabFragment_Family(), "T3");
pagerAdapter.addFragment(new TabFragment_Board(), "T4");

viewPager.setOffscreenPageLimit(4);
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager, false);

这段代码是非常简单和基本的代码。 我有一个关于滚动动作的问题,请看下图。

问题:为什么滚动不能向右移动?这是错误还是我的错误?请告诉我。谢谢

【问题讨论】:

  • 你想滚动标签右侧并使用滚动移动到第二个标签吗?
  • 啊......我的错误问题......使用左滚动移动到第二个标签
  • 你试过我的答案了吗?
  • 是的,输入代码这个 tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);但还是一样..我使用示例代码here
  • 请找到我的另一个答案并尝试一下。

标签: android android-toolbar android-coordinatorlayout


【解决方案1】:

请在您的标签布局中添加以下行:

<android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:background="@android:color/transparent"
            app:tabGravity="fill"
            app:tabMode="scrollable"
            app:tabTextAppearance="@style/TextAppearance.Design.Tab"
            app:tabSelectedTextColor="@color/myPrimaryColor"
            app:tabIndicatorColor="@color/myPrimaryColor"
            android:overScrollMode="never"
            />

【讨论】:

    【解决方案2】:

    您还可以通过编程方式设置可滚动模式:

    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    

    【讨论】:

      【解决方案3】:

      在tablayout中添加这一行: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:layout_scrollFlags="scroll|enterAlways"
                      app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
      

      【讨论】:

      • 同样的问题.. : (
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 2019-01-28
      • 2015-10-09
      相关资源
      最近更新 更多