在RecyclerView下方显示视图(不重叠)

时间:2017-11-17 13:23:10

标签: android

有很多解决方案,其中最后一个视图是底部对齐的,但我需要它具有灵活性。想象一个带有列表和正/负按钮的对话框。如果我希望它只有少数几个元素:enter image description here

当有很多项目时,这个对话框应该扩展到底部,如下所示:

enter image description here

LinearLayout内的布局结构是:

title layout
RecyclerView
buttons layout

我尝试将RecyclerView包裹在LinearLayout内而没有成功,只是重叠我的按钮

3 个答案:

答案 0 :(得分:1)

最好的方法是使用NestedScrollView。使用方式如下:

<NestedScrollView>
  <LinearLayout>
   <YourTitleLayout>
   <RecyclerView>
   <YourLayoutBelowRecyclerView>
 </LinearLayout>
</NestedScrollView>

这个嵌套的scrollView确保项目按名称建议一个接一个地滚动。希望这会有所帮助。

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/splash"
android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/card_listing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_add_card"
        android:layout_below="@+id/card_line"
        android:layout_margin="10dp"
        android:dividerHeight="2dp"
        android:visibility="visible" />

    <TextView
        android:id="@+id/error_message"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/card_listing"
        android:layout_toRightOf="@+id/card_listing"
        android:gravity="center"
        android:text="@string/internet_toast"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:visibility="gone" />

    <Button
        android:id="@+id/btn_add"
        style="@style/ButtonStyle"
        android:layout_above="@+id/pay_now"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="Button" />
</RelativeLayout>

答案 2 :(得分:0)

尝试使用 recyclerview 的 layout_weight 属性。并为下面的标题和按钮提供固定高度。 Layout_weight 确保 recyclerview 不会掩盖其下方的视图。