【问题标题】:the RecyclerView I am injecting does not appear?我正在注入的 RecyclerView 没有出现?
【发布时间】:2016-05-05 16:55:10
【问题描述】:

这是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:orientation="horizontal">


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:dividerHeight="10dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/content_main">


        <android.support.v4.widget.DrawerLayout

            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/DrawerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:elevation="7dp">


            <!-- Framelayout to display Fragments -->
            <FrameLayout
                android:id="@+id/frame_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


                <include
                    android:id="@+id/tool_bar"
                    layout="@layout/tool_bar"></include>
            </LinearLayout>


            <android.support.v7.widget.RecyclerView
                android:id="@+id/RecyclerView"
                android:layout_width="320dp"
                android:layout_height="match_parent"
                android:layout_gravity="left"

                android:background="#ffffff"
                android:scrollbars="vertical">


            </android.support.v7.widget.RecyclerView>


        </android.support.v4.widget.DrawerLayout>


    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

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


    </RelativeLayout>
</LinearLayout>

我做这个设计的主要原因是试图展示

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

正确。但是,当它根本不显示时。在添加两个相对布局之前,我有一个相对布局并且 recyclerview 确实显示了,但它match_parent 用于列表的每个项目并且无法修复它。如何正确显示我的视图?

【问题讨论】:

  • 首先用正确的解释更新您的问题。现在,尝试将 DrawerLayout 作为父布局。然后实现它。

标签: android xml view android-recyclerview android-relativelayout


【解决方案1】:

问题可能与您的RecyclerView 上的layout_height="wrap_content" 有关。您需要将setAutoMeasureEnabled(true) 用于wrap_content 才能与RecyclerView 正常工作。

这是一个例子:

RecyclerView threadRecyclerView = (RecyclerView)findViewById(R.id.threadRecyclerView);
threadRecyclerView.getLayoutManager().setAutoMeasureEnabled(true);

您的 XML 应该如下所示:

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

希望对您有所帮助。 :)

请注意,setAutoMeasureEnabled 仅适用于 Android Support Library +23.2

【讨论】:

    猜你喜欢
    • 2017-09-26
    • 1970-01-01
    • 2015-12-09
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    相关资源
    最近更新 更多