【问题标题】:RecyclerView takes up all screenspaceRecyclerView 占用所有屏幕空间
【发布时间】:2015-02-12 08:53:00
【问题描述】:

所以我在布局文件中的 RecyclerView 遇到了一些问题

这里是:

<android.support.v7.widget.RecyclerView
    android:id="@+id/chat_listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingRight="@dimen/abc_action_bar_default_padding_material"
    android:paddingLeft="@dimen/abc_action_bar_default_padding_material"
    />


<LinearLayout
    android:layout_below="@id/chat_listview"
    android:layout_width="match_parent"
    android:layout_height="@dimen/bottom_bar_height"
    android:orientation="horizontal"
    >

    <EditText
        android:id="@+id/chat_input_edittext"
        android:layout_weight="7"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:inputType="textAutoCorrect"
        />

    <Button
        android:id="@+id/chat_send_button"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/ic_action_send_now"
        />

    </LinearLayout>

RecyclerView 是可见且可滚动的,但 LinearLayout 和里面的 Views 不可见......我尝试了很多东西,但到目前为止没有任何效果。

请你们指点我正确的方向吗?

非常感谢!

【问题讨论】:

标签: java android xml android-layout android-recyclerview


【解决方案1】:

当RecyclerView被绘制时,它会在绘制下一个元素之前计算屏幕上所有剩余的大小,并且在绘制其他元素之后不重新计算,将它们留在屏幕之外。

诀窍是先绘制所有其他元素,然后将 RecyclerView 放在最后。 FrameLayout 不再起作用,因此使用相对布局并将 RecyclerView 放在 XML 布局文件的最后。

在 RecyclerView 下方添加带有按钮的栏的示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    >

    <LinearLayout
        android:id="@+id/pagination_btns"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"> //HERE YOU ALIGN THIS ELEMENT TO THE BOTTOM OF THE PARENT
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/previous_btn_label"/>
        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/next_btn_label"/>
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/items_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layout_above="@id/pagination_btns"/> //HERE YOU ALIGN THE RECYCLERVIEW ABOVE THE PAGINATION BAR 


</RelativeLayout>

【讨论】:

  • 应该是选择的答案! FrameLayout 处理起来很糟糕,你给出了解释。谢谢!
  • 经过大约 3 小时的搜索和测试,结果如下:简单且解释清楚的答案!谢谢!
  • 你很幸运。我花了几天时间才弄明白。我很高兴它有所帮助。
【解决方案2】:

将根视图更改为 FrameLayout。将 LinearLayout 的重力设置为底部

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


<android.support.v7.widget.RecyclerView
    android:id="@+id/chat_listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingRight="@dimen/abc_action_bar_default_padding_material"
    android:paddingLeft="@dimen/abc_action_bar_default_padding_material"
/>


<LinearLayout
    android:layout_below="@id/chat_listview"
    android:layout_width="match_parent"
    android:layout_height="@dimen/bottom_bar_height"
    android:layout_gravity="bottom"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/chat_input_edittext"
        android:layout_weight="7"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:inputType="textAutoCorrect"/>

    <Button
        android:id="@+id/chat_send_button"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/ic_action_send_now"/>

    </LinearLayout>
</FrameLayout>

或者,您可以将线性布局包装在 FrameLayout 并将其设置为 align_parentBottom = true 如果它是根处的相对布局或线性布局并且您的视图中有其他内容。

【讨论】:

  • 非常感谢!我还在 RecyclerView 的底部添加了一个填充,因此 LinearLayout 后面不会有文本。
  • FrameLayout 没有 android:layout_below 属性。添加填充是一种我真的不建议的解决方法。 RelativeLayout 和 LinearLayout 应该可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
  • 2016-11-01
相关资源
最近更新 更多