java - RecyclerView 占用所有屏幕空间

标签 java android xml android-layout android-recyclerview

所以我在使用布局文件中的 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 和内部的 View 不可见...我尝试了很多方法,但到目前为止没有任何效果。

你们中的任何人都可以指出我正确的方向吗?

非常感谢!

最佳答案

当 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>

关于java - RecyclerView 占用所有屏幕空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27464073/

相关文章:

Javascript - 解析 XML 文档以创建数组

java - 返回值不变-java

android - Javascript 接口(interface),错误 "Uncaught Error: Error calling method on NPObject"

java - Android - RecyclerView 从中间而不是顶部开始

java - 在 TextView 中让一个词始终高于另一个词

android - 使用 Andengine 的游戏中出现意外的 arrayIndexOutofBound 异常

.net - 是否可以在 .net 中执行 "mixed"XML 序列化?

java - 什么是CompoundCallable?

java - 不推荐使用 QueryBuilder 类型中的方法 andProperty(String) - Javers

java - 是否有可能在 Spring 制作@Aspect 请求范围