RecyclerView不尊重height = wrap_content吗? [英] RecyclerView doesn't respect height=wrap_content?

查看:87
本文介绍了RecyclerView不尊重height = wrap_content吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个聊天屏幕,其中包含两个视图:用于显示以前的聊天的回收站视图和用于封装编辑文本,发送按钮的自定义视图,以及另一个回收站视图,以允许用户选择要发送的其他内容(例如照片)等这个自定义视图称为面板".

I have a chat screen which contains two views : a recycler view for displaying previous chat and a custom view which encapsulates an edit text, a send button, and another recycler view to let user select other things to send like photos, etc. This custom view is called "Panel".

因此,我的聊天活动具有以下布局:

So, my chat activity has the following layout:

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

    <chat.example.app.widget.Panel
        style="@style/PanelStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:id="@+id/panel" />

    <android.support.v7.widget.RecyclerView
        android:background="@android:color/holo_red_dark"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:scrollbars="vertical"
        android:id="@+id/previous_chat"
        android:layout_above="@id/panel"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

和我的面板"视图具有以下布局:

and my "Panel" view has the following layout:

<?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="wrap_content"
    android:background="@android:color/white">
    <RelativeLayout
        android:id="@+id/staging_area_root"
        android:layout_gravity="center"
        android:gravity="center"
        android:paddingLeft="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-- Chat box and send -->
    </RelativeLayout>
    <!-- THIS GUY IS PROBLEMATIC -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/card_set"
        android:layout_margin="15dp"
        android:layout_below="@id/staging_area_root"
        android:background="@android:color/holo_blue_bright"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>  

问题在于,Panel中的回收站视图吞噬了整个屏幕空间.因此,我所看到的只是Panel类中有问题的回收商视图.

The problem is that the recycler view from Panel gobbles up entire screen space. So all I see is Mr. problematic recycler view from Panel class.

这两个回收者视图均分配了LinearLayoutManager. 尚无适配器.

Both these recycler views have LinearLayoutManagers assigned to them. No adapters yet.

我的build.gradle中具有最新的支持库:

I have the latest support library in my build.gradle:

compile 'com.android.support:recyclerview-v7:23.2.0'  

并浏览最新的android开发人员博客文章:

and going over the latest android developers blog post:

此版本为LayoutManager API带来了令人兴奋的新功能: 自动测量!这使得RecyclerView可以根据以下内容自行调整大小 其内容的大小.这意味着以前不可用 场景,例如将WRAP_CONTENT用于 现在可以使用RecyclerView.您会发现所有内置的 LayoutManager现在支持自动测量.

This release brings an exciting new feature to the LayoutManager API: auto-measurement! This allows a RecyclerView to size itself based on the size of its contents. This means that previously unavailable scenarios, such as using WRAP_CONTENT for a dimension of the RecyclerView, are now possible. You’ll find all built in LayoutManagers now support auto-measurement.

我错过了什么吗?

更新1:

我向有问题的回收商视图中添加了一个适配器,其中包含0项.仍然没有解决方案.

I added an adapter to Mr. problematic recycler view with 0 items in it. Still no solution.

Update2:
截图:

推荐答案

从23.2 RecyclerView onMeasure()调用中摘录的代码段:

Code snippet taken from the 23.2 RecyclerView onMeasure() call:

if (mLayout.mAutoMeasure) {
        final int widthMode = MeasureSpec.getMode(widthSpec);
        final int heightMode = MeasureSpec.getMode(heightSpec);
        final boolean skipMeasure = widthMode == MeasureSpec.EXACTLY
                && heightMode == MeasureSpec.EXACTLY;
        mLayout.onMeasure(mRecycler, mState, widthSpec, heightSpec);
        if (skipMeasure || mAdapter == null) {
            return;
        }
        if (mState.mLayoutStep == State.STEP_START) {
            dispatchLayoutStep1();
        }
        // set dimensions in 2nd step. Pre-layout should happen with old dimensions for
        // consistency
        mLayout.setMeasureSpecs(widthSpec, heightSpec);
        mState.mIsMeasuring = true;
        dispatchLayoutStep2();

        // now we can get the width and height from the children.
        mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec);

        // if RecyclerView has non-exact width and height and if there is at least one child
        // which also has non-exact width & height, we have to re-measure.
        if (mLayout.shouldMeasureTwice()) {
            mLayout.setMeasureSpecs(
                    MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
            mState.mIsMeasuring = true;
            dispatchLayoutStep2();
            // now we can get the width and height from the children.
            mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec);
        }
    }

mLayout.setMeasuredDimensionFromChildren()调用将根据RecyclerView子代的大小进行自动测量.意思是,要触发自动测量,需要具有1个或多个孩子的有效RecyclerView.Adapter.

The mLayout.setMeasuredDimensionFromChildren() call will do the auto-measuring based off of the size of the children of the RecyclerView. Meaning, a valid RecyclerView.Adapter with 1 or more children is needed for the auto-measurement to trigger.

这篇关于RecyclerView不尊重height = wrap_content吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆