简体   繁体   English

CoordinatorLayout和工具栏以及TabLayout有时无法左右移动

[英]CoordinatorLayout and toolbar and TabLayout sometimes cannot work move right and left scroll

I make the collapse toolbar activity use the CoordinatorLayout. 我使折叠工具栏活动使用CoordinatorLayout。

1. main_activity 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. code 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);

This code is very simple and basic code. 这段代码是非常简单和基本的代码。 I have one question the scroll action please see the image below. 我对滚动动作有一个疑问,请参见下图。

在此处输入图片说明

在此处输入图片说明

Question: why cannot scroll move right? 问题:为什么滚动不能向右移动? this is bug or my mistake? 这是错误还是我的错误? please let me know. 请告诉我。 thanks 谢谢

please add below lines in your tablayout : 请在您的布局中添加以下几行:

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

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

tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 滚动时隐藏工具栏无法正常工作[CoordinatorLayout] - Hide Toolbar on scroll wont work properly [CoordinatorLayout] 工具栏不响应CoordinatorLayout中的滚动 - Toolbar not responding to scroll in CoordinatorLayout 工具栏在CoordinatorLayout中不会在滚动时折叠 - Toolbar not collapsing on scroll in CoordinatorLayout 使用CoordinatorLayout滚动隐藏工具栏 - Hide Toolbar on scroll with CoordinatorLayout 在CoordinatorLayout中的ViewPager然后在屏幕外滚动tablayout不起作用 - ViewPager in CoordinatorLayout then scroll tablayout offscreen not working 我向上/向下/向左或向右移动/滚动谷歌地图,因为它向后移动当前位置 - I cannot move/scroll google maps up/down/left or right as it move back tomy current location 折叠工具栏并在滚动条上隐藏TabLayout - Collapse Toolbar and hide TabLayout on scroll 除非工具栏具有layout_scrollflags:scroll,否则CoordinatorLayout滚动将无法正常工作 - CoordinatorLayout scroll doesn't work unless Toolbar has layout_scrollflags:scroll 如何通过CoordinatorLayout将TextView移动到工具栏中? - How to move a TextView into the Toolbar through CoordinatorLayout? 如何在CoordinatorLayout内部的工具栏上触发滚动行为 - How to Trigger a Scroll Behavior on a Toolbar inside a CoordinatorLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM