【问题标题】:Android Recyclerview scrolling issueAndroid Recyclerview 滚动问题
【发布时间】:2016-12-29 17:33:33
【问题描述】:

我是 Android 开发的新手。我正在使用回收站视图来显示列表。我认为它具有滚动的默认行为,但它没有发生。谁能给我一些建议。以下是我正在使用的布局。

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@android:color/transparent"
    tools:context="com.team.sidhesh.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <ImageView
            android:id="@+id/sliderTempImageView"
            android:layout_width="match_parent"
            android:layout_height="@dimen/slider_image_height"
            android:src="@drawable/homescreen_logo"
            android:scaleType="fitCenter"
            android:background="@android:color/transparent"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:background="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            android:paddingBottom="10dp"/>

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

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

【问题讨论】:

  • 有时这是由于您在图像视图中使用的可绘制对象而发生的。绘图不应该太大

标签: android android-recyclerview android-coordinatorlayout


【解决方案1】:

试试这个:

    <?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@android:color/transparent"
    tools:context="com.team.sidhesh.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <ImageView
            android:id="@+id/sliderTempImageView"
            android:layout_width="match_parent"
            android:layout_height="@dimen/slider_image_height"
            android:src="@drawable/homescreen_logo"
            android:scaleType="fitCenter"
            android:background="@android:color/transparent"/>

      <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="470dp"
            android:layout_weight="1"
            android:background="@android:color/darker_gray"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:background="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            android:paddingBottom="10dp"/>
        </android.support.v4.widget.NestedScrollView>
    </android.support.design.widget.AppBarLayout>

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

还要确保在 gradle 文件中添加依赖项

compile 'com.android.support:recyclerview-v7:22.2.0'

【讨论】:

    【解决方案2】:

    为什么在AppBarLayout 中使用RecyclerView?它不应该在CoordinatorLayout 之下和AppBarLayout 之外吗?

    要达到您的期望,请查看此链接:https://developer.android.com/reference/android/support/design/widget/AppBarLayout.html

    显示/说:&lt;!-- Your scrolling content --&gt;

    <android.support.v4.widget.NestedScrollView
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
             <!-- Your scrolling content -->
    
         </android.support.v4.widget.NestedScrollView>
    

    因此,您需要将RecyclerView 放在AppBarLayout 之外,然后将:app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到RecyclerView。我也将AppBarLayout layout_height 设置为wrap_content

    <?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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true"
        tools:context="com.team.sidhesh.MainActivity">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/background"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    
            <ImageView
                android:id="@+id/sliderTempImageView"
                android:layout_width="match_parent"
                android:layout_height="@dimen/slider_image_height"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:src="@drawable/homescreen_logo" />
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:paddingBottom="10dp"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    ImageView 到底是怎么回事?使用滑块?那么也许您还需要将它们与CollapsingToolbarLayout 放在一起!

    【讨论】:

      【解决方案3】:

      您应该将 recyclerView 包装到 ScrollviewHorizontalscrollView

      【讨论】:

      • 您可以在您添加的链接中添加更多信息吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 2016-05-07
      • 2018-09-14
      • 1970-01-01
      相关资源
      最近更新 更多