RecyclerView 上方和下方的视图

Views above and below RecyclerView

我有一个显示动态内容的 RecyclerView。我会在其他视图之间显示此 RecyclerView。比如RecyclerView上面显示一个View,下面显示一个View:

View
RecyclerView
View

但是,无论我尝试什么,似乎都不起作用,而且 RecyclerView 似乎占据了整个 space。我认为问题出在 wrap_content issue, though I've tried some suggested solutions, such as this custom LinearLayoutManager,但它似乎并没有解决我的问题。

尝试:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

        <!-- AppBar works fine; no issue here -->
        <android.support.design.widget.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:id="@+id/app_bar_layout">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin"/>

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

        <!-- Not displaying; Tried different views -->
        <com.chrynan.taginput.view.TextInputWrapper
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit_text_container">
            <AutoCompleteTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/edit_text"/>
        </com.chrynan.taginput.view.TextInputWrapper>

        <!-- Takes up entire screen space -->
        <android.support.v4.widget.SwipeRefreshLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/swipe_refresh">

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/recycler_view"/>

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

        <!-- Not displaying -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/text_view"/>

    </LinearLayout>

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

问题:有没有办法在RecyclerView的上方放置一个View,在下方显示一个View?如果是这样,如何?还是使用 ListView 更好?

这可能不是您的典型解决方案,但您仍然可以尝试一下。

如何使用 RelativeLayout 而不是 LinearLayout,并简单地将 SwipeRefreshLayout 的顶部和底部填充设置为视图的高度,如下所示。

<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:fitsSystemWindows="true">

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

    <!-- your app bar -->

    <!-- Not displaying; Tried different views -->
    <com.chrynan.taginput.view.TextInputWrapper
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:id="@+id/edit_text_container"/>

    <!-- Takes up entire screen space -->
    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="150dp"
        android:paddingBottom="250dp"
        android:id="@+id/swipe_refresh">

        <android.support.v7.widget.RecyclerView

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/recycler_view"/>

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

    <!-- bottom view -->
    <TextView
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:id="@+id/text_view"/>

</RelativeLayout>

weight的用法呢?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://..."
    android:layout_width="match_parent"
    android:layout_height="match_parent"        
    android:orientation="vertical"
    >

    <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

为 RecyclerView 使用页眉和页脚,在 Recyclerview 适配器中使用视图类型并添加页眉和页脚 view.The 您面临的问题是因为一个布局中有多个 scollview,最好的解决方案是使用页眉和页脚,这将使它成为单个滚动视图。请参阅此 link 了解更多信息