【问题标题】:Views above and below RecyclerViewRecyclerView 上方和下方的视图
【发布时间】:2016-01-09 06:53:22
【问题描述】:

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

View
RecyclerView
View

但是,无论我尝试什么似乎都不起作用,并且 RecyclerView 似乎占据了整个空间。我相信问题出在wrap_content issue,虽然我尝试了一些建议的解决方案,例如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 更好?

【问题讨论】:

    标签: android android-recyclerview android-design-library


    【解决方案1】:

    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 的已知错误,所以简单地添加 layout_weight 不起作用。但是,我决定使用 ListView 而不是 RecyclerView,向 ListView 添加 layout_weight 确实给了我想要的效果。
    【解决方案2】:

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

    如何使用 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>
    

    【讨论】:

    • 不幸的是,这对我来说不是一个可行的解决方案,因为我的顶部和底部视图会动态改变大小。
    • 为什么不动态重置填充?
    • 动态处理填充对我不起作用。结果我改为使用 ListView 并在其上添加了 layout_weight 属性,这产生了我想要的效果。谢谢你的帮助。
    【解决方案3】:

    为 RecyclerView 使用页眉和页脚,在 Recyclerview Adapter 中使用 viewtype 并添加页眉和页脚视图。您面临的问题是因为一个布局中有多个 scollview,最好的解决方案是使用页眉和页脚,这将使它作为单个滚动视图。有关更多信息,请参阅此链接https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type?rq=1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多