【问题标题】:Android NestedScrollView is Lagging when scrolling up and downAndroid NestedScrollView 在上下滚动时滞后
【发布时间】:2017-02-21 12:03:35
【问题描述】:

我在 Coordinatorlayout 中创建了一个带有 3 个片段的 viewpager。
在每个 Viewpager 中,您都可以找到包含一些内容的 NestedScrollView。
但是,如果我上下滚动内容,它就会滞后。
我测试并用普通的 ScrollView 替换了 NestedScrollView,它很流畅。问题在于嵌套滚动视图。
我希望能得到一些帮助。谢谢。

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

        <android.support.design.widget.TabLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/black"
            app:tabIndicatorHeight="1dp"
            app:tabMaxWidth="0dp"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/white"
      app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
            app:tabTextColor="@color/black"/>
    </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"/>

    <include
        android:id="@+id/floating_action_button"
        layout="@layout/floating_action_button_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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

在viewpager里面你可以找到:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    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/fragment_first"
    style="@style/NestedScrollBarStyle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:layout_scrollFlags="scroll|enterAlways"
    tools:context="com.amirkhan.template1.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="111111111111111111111111111"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="111111111111111111111111111"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="111111111111111111111111111"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="111111111111111111111111111"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="111111111111111111111111111"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sadsff"/>
    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

after adding android:nestedScrollingEnabled="false"

【问题讨论】:

    标签: java android xml nestedscrollview


    【解决方案1】:

    H 阿米尔,

    请在你的代码中加入下面一行

    recyclerView.setNestedScrollingEnabled(false);
    

    【讨论】:

    • 没有recyclerview。
    • 抱歉,如果回收视图使用您的布局
    【解决方案2】:

    使用此代码

    <?xml version="1.0" encoding="utf-8"?>
        <android.support.v4.widget.NestedScrollView
            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/fragment_first"
            style="@style/NestedScrollBarStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_scrollFlags="scroll|enterAlways"
            android:nestedScrollingEnabled="false"
            tools:context="com.amirkhan.template1.MainActivity">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="16dp">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="111111111111111111111111111"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="111111111111111111111111111"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="111111111111111111111111111"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="111111111111111111111111111"/>
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:text="111111111111111111111111111"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="sadsff"/>
            </LinearLayout>
    
        </android.support.v4.widget.NestedScrollView>
    

    【讨论】:

    • 我明白了:无法实例化以下类:- android.support.v4.widget.NestedScrollView
    • 还是同样的消息
    • 没有办法得到错误。我们只在 xml 中添加一个滚动视图属性对吗?请确保它在我提到的编辑之前工作。
    • 请看图片
    • 分享图片
    【解决方案3】:

    这是我做的,效果很好。

    你最需要的部分是:

    <android.support.v4.widget.NestedScrollView
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="true"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
    

    我为你做了这个,所以你可以试试。

    <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/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
     <android.support.v4.widget.NestedScrollView
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="true"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
    <LinearLayout>
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="111111111111111111111111111"/>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="111111111111111111111111111"/>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="111111111111111111111111111"/>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="111111111111111111111111111"/>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:text="111111111111111111111111111"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sadsff"/>
    </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      【解决方案4】:

      在您的 java 代码中的 LinearLayout 对象上使用它:

      linearLayout.setNestedScrollingEnabled(false);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-17
        • 1970-01-01
        • 2014-11-11
        • 1970-01-01
        • 2020-05-17
        • 1970-01-01
        • 1970-01-01
        • 2019-05-08
        相关资源
        最近更新 更多