草庐IT

android - RecyclerView 不尊重 height=wrap_content?

coder 2023-12-10 原文

我有一个包含两个 View 的聊天屏幕:一个用于显示以前聊天的回收器 View 和一个封装编辑文本的自定义 View 、一个发送按钮和另一个回收器 View ,让用户选择要发送的其他内容,如照片等. 这个自定义 View 叫做“Panel”。

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

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

我的“面板” View 具有以下布局:

<?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 中的回收器 View 占用了整个屏幕空间。所以我所看到的只是来自 Panel 类的 Mr. problemed recycler View 。

这两个回收器 View 都分配了 LinearLayoutManager还没有适配器。

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

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

并查看最新的 android 开发人员博客文章:

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:

我向 Mr. problemed recycler view 添加了一个适配器,其中包含 0 个项目。仍然没有解决办法。

更新2:
截图:

最佳答案

取自 23.2 RecyclerView onMeasure() 调用的代码 fragment :

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 才能触发自动测量。

关于android - RecyclerView 不尊重 height=wrap_content?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35726839/

有关android - RecyclerView 不尊重 height=wrap_content?的更多相关文章

  1. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  2. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  3. ruby-on-rails - rails 不需要 Content-Type "application/json" - 2

    我在rails中有一个API端点,默认情况下,如果您没有设置任何Content-Typeheader,则会处理中的参数application/x-www-form-urlencoded有没有办法在不指定header中的内容类型的情况下处理来自POST请求的rails中的json字符串? 最佳答案 在您的routes.rb文件中,您可以将POST路由放置在命名空间中,并像这样定义预期的格式:namespace:api,defaults:{format::json}dopost'example'=>'controller#action'

  4. ruby - 如何在 ruby​​ 中设置 header ['content-type' ] ='application/json' - 2

    require'net/http'require'rubygems'require'json'url=URI.parse('http://www.xyxx/abc/pqr')resp=Net::HTTP.get_response(url)#get_responsetakesanURIobjectdata=resp.bodyputsdata这是我在ruby​​中的代码,resp.data以xml形式提供给我数据。restapi默认返回xml数据,如果headercontent-type是application/json,则返回json。但我想要json格式的数据。为此我必须设置heade

  5. ruby - 如何在尊重隐私的情况下动态调用方法 - 2

    使用动态方法调用(#send或#method),方法的可见性将被忽略。有没有一种简单的方法可以动态调用调用私有(private)方法失败的方法? 最佳答案 据我所知-你需要public_send方法:-----------------------------------------------------Object#public_sendobj.public_send(symbol[,args...])=>objFromRuby1.9.1-----------------------------------------------

  6. ruby-on-rails - 如何在 RoR 中使用 content_tag 嵌入标签? - 2

    我有这个可以为我生成一个超链接:我希望它显示在td标签中,所以我想使用这个content_tag来帮助我:"example")%>我想要我的td中的超链接,所以我有这样的东西:,:class=>"example")%>但是我收到语法错误,我该怎么办? 最佳答案 内联:'example')%>或block形式:'example')do%> 关于ruby-on-rails-如何在RoR中使用content_tag嵌入标签?,我们在StackOverflow上找到一个类似的问题:

  7. ruby-on-rails - wicked_pdf : footer height/styling - 2

    我正在使用很棒的wicked_pdfgem生成PDF,但我不知道如何更改页脚中的某些样式。我有一个用于页脚的HAML模板,大致如下所示:!!!%html%head%meta{:charset=>"utf-8"}=wicked_pdf_stylesheet_link_tag"pdf"%body.footer%pLine1%pLine2%pLine3还有一些样式:.footer{padding-top:1em;border-top:1pxsolid#ccc;}样式应用得很好,但由于页脚高度较小,只有第一行可见。我试过通过CSS设置高度,但到目前为止还没有。如果我使用例如center、att

  8. c - Data_wrap_struct 和标记函数 - 2

    我正在编写一个Ruby扩展,我正在使用函数Data_wrap_struct。为了参与Ruby的标记和清除垃圾收集过程,我需要定义一个例程来释放我的结构,以及一个例程来标记从我的结构到其他结构的任何引用。我通过经典的free函数来释放内存,但我不知道如何使用标记函数。我的结构听起来像这样typedefstruct{intx;inty;}A;typedefstruct{Acollection[10];intcurrent;}B;我认为我需要一个标记函数来标记结构B的collection中的引用。谁能给我看一个例子,看看标记函数是如何工作的? 最佳答案

  9. Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信) - 2

    运行有问题或需要源码请点赞关注收藏后评论区留言一、利用ContentResolver读写联系人在实际开发中,普通App很少会开放数据接口给其他应用访问。内容组件能够派上用场的情况往往是App想要访问系统应用的通讯数据,比如查看联系人,短信,通话记录等等,以及对这些通讯数据及逆行增删改查。首先要给AndroidMaifest.xml中添加响应的权限配置 下面是往手机通讯录添加联系人信息的例子效果如下分成三个步骤先查出联系人的基本信息,然后查询联系人号码,再查询联系人邮箱代码 ContactAddActivity类packagecom.example.chapter07;importandroid

  10. Android 10.0 设置默认launcher后安装另外launcher后默认Launcher失效的功能修复 - 2

    1.前言 在10.0的系统rom定制化开发中,在系统中有多个launcher的时候,会在开机进入launcher的时候弹窗launcher列表,让用户选择进入哪个launcher,这样显得特别的不方便所以产品开发中,要求用RoleManager的相关api来设置默认Launcher,但是在设置完默认Launcher以后,在安装一款Launcher的时候,默认Launcher就会失效,在系统设置的默认应用中Launcher选项就为空,点击home键的时候会弹出默认Launcher列表,让选择进入哪个默认Launcher.所以需要从安装Launcher的流程来分析相关的设置。来解决问题设置默认La

随机推荐