RecyclerView标头或顶视图

时间:2018-11-12 16:49:48

标签: android view android-recyclerview

请帮助我!

如何在RecyclerView顶部添加视图。

此图片:https://blog.iamsuleiman.com/wp-content/uploads/2017/08/pagination-4-recyclerview-multiple-viewtypes-header.png

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/pattern"
    android:fillViewport="true"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

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

        <ImageView
            android:id="@+id/main_image_slider"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/line" />

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/main_swipe"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rc"
                android:layout_width="match_parent"
                android:layout_height="500dp"
                android:scrollbars="none" />
        </android.support.v4.widget.SwipeRefreshLayout>
    </LinearLayout>

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

1 个答案:

答案 0 :(得分:0)

欢迎使用Stackoverflow。

我觉得很难理解你的问题。从我的角度来看,您想在recyclerview上方(顶部)添加另一个图像。确定您不需要NestedScrollView。请尝试以下操作:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="@drawable/pattern"
android:fillViewport="true"
android:orientation="vertical"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">
<ImageView
            android:id="@+id/main_image_slider"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/line" />
<ImageView
            android:id="@+id/ivNew1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/line" />
<ImageView
            android:id="@+id/ivNew2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/line" />

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/main_swipe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rc"
            android:layout_width="match_parent"
            android:layout_height="500dp"
            android:scrollbars="none" />
    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

如果这不是您问题的答案,请告诉我!您也可能想在Recyclerview中添加另一个图像。如果可以发布layout.xml文件,那将非常有帮助。

很好的问候