【问题标题】:Not able to add empty view below Recyclerview无法在 Recyclerview 下方添加空视图
【发布时间】:2015-01-04 17:49:39
【问题描述】:

我正在尝试使用 recyclerview 和应用程序的浮动操作按钮。我遇到的问题是浮动操作按钮阻碍了recyclerview中的按钮。我查看了 Android 应用程序,如手机应用程序(您在 gridvew 中显示联系人的应用程序)是如何设计来处理这个问题的,我看到底部有空白空间,在 recyclerview 和浮动操作按钮之间留出了空间,用户只需要滚动到底部以避免重叠。 我试图在 recyclerview 之后添加一个视图,但它没有显示出来,这个视图也应该与 recyclerview 行一起移动。在回收站视图的末尾显示视图我缺少什么。 我的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#6fbababa">

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

    <ImageButton
        android:id="@+id/button_floating_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:src="@drawable/ic_fab"
        android:background="@null"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="12dp"
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp"
        android:elevation="2dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_below="@id/my_recycler_view"
        android:background="@color/white"/>

</RelativeLayout>

更新:修改了布局代码以使其工作。

<android.support.v7.widget.RecyclerView 
android:id="@+id/my_recycler_view" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:clipToPadding="false" 
android:paddingBottom="30dp" 
android:scrollbars="vertical"/>

【问题讨论】:

  • 所有你需要的是让 RecyclerView 最后绘制。检查我的答案中的示例。 HTH

标签: android android-layout android-recyclerview


【解决方案1】:

为 RecyclerView 添加底部填充。 此外,除非您在布局管理器中覆盖了onMeasure,否则不要使用android:layout_height="wrap_content"。当前的布局管理器还不支持换行内容。

添加属性android:clipToPadding="false" 以实现目标。 如 cmets 中所述。

【讨论】:

  • 向 recyclerview 添加填充会导致屏幕的一部分被占用,从而切断屏幕末尾行的视图。它没有显示在最后一行下方的空白区域,而是显示在最后一行
  • 刚刚解决了这个问题...我们还需要在 recyclerview 中添加 android:clipToPadding="false" ..这是对我有用的最终结果 跨度>
  • @ɥʇᴉɾuɐɹ 太棒了,你让我开心:-)
  • 其实它支持wrap_content。问题是在屏幕上绘制新元素后它不会重新计算。但它适用于以前的元素。因此,您需要做的就是将 RecyclerView 留在布局文件的最后。您可以使用RelativeLayout 设置元素放置。检查我的答案。
  • 此解决方案使我的底部视图在向上滚动时消失。有谁知道为什么?
【解决方案2】:

这里有一个示例(在本例中是一个 listView,但可以是任何东西)位于一个回收器视图下方,它占据了父级的其余高度。注意 layout_alignParentBottom 和 layout_above 属性。

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none"
    **android:layout_alignParentBottom="true"**
    />

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:layout_above="@+id/list_view"
    />

【讨论】:

    【解决方案3】:

    我为此创建了一个实用方法,因此您可以轻松添加填充

    public void addLastChildPadding(RecyclerView.ViewHolder holder,int padding,int recyclerListSize,int position){
    
    if(recyclerListSize-1==position){
    holder.itemView.setPadding(0,0,0,padding);
    }
    }
    

    【讨论】:

      【解决方案4】:

      立即尝试

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">
      
      
      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent">
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
              <android.support.v7.widget.RecyclerView
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:clipToPadding="false">
      
              </android.support.v7.widget.RecyclerView>
          </RelativeLayout>
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:layout_marginBottom="15dp">
      
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="20dp"
                  android:layout_marginRight="20dp"
                  android:orientation="horizontal">
      
                  <android.support.v7.widget.AppCompatButton
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:layout_marginRight="15dp"
                      android:layout_weight="1"
                      android:background="@drawable/button_primary_color_border"
                      android:text="70%"
                      android:textColor="@android:color/darker_gray"
                      android:textSize="20sp" />
      
                  <android.support.v7.widget.AppCompatButton
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:layout_marginRight="15dp"
                      android:layout_weight="1"
                      android:background="@drawable/button_primary_color_border"
                      android:text="60%"
                      android:textColor="@android:color/darker_gray"
                      android:textSize="20sp" />
              </LinearLayout>
          </RelativeLayout>
      
      </RelativeLayout>
      </LinearLayout>
      
      </LinearLayout>
      

      根据您的用途删除图像和其他字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-31
        • 2016-01-09
        • 1970-01-01
        相关资源
        最近更新 更多