Android Recyclerview滚动问题

时间:2016-12-29 17:33:33

标签: android android-recyclerview android-coordinatorlayout

我是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>

3 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

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

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

显示/说:<!-- Your scrolling content -->

<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 =&#34; @ string / appbar_scrolling_view_behavior&#34;到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到底是什么?使用Slider?那么也许你需要将它与CollapsingToolbarLayout一起放在一起!

答案 2 :(得分:0)

您应该做的是将recyclerView包装到ScrollviewHorizontalscrollView