RecyclerView 不尊重 height=wrap_content?
RecyclerView doesn't respect height=wrap_content?
我有一个包含两个视图的聊天屏幕:一个用于显示以前聊天的回收器视图和一个封装编辑文本的自定义视图、一个发送按钮和另一个回收器视图让用户 select 其他东西发送喜欢的照片等。此自定义视图称为 "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>
我的 "Panel" 视图具有以下布局:
<?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
的回收器视图吞噬了整个屏幕 space。所以我所看到的只是 Panel class 先生的问题回收者观点。
这两个回收站视图都分配了 LinearLayoutManager
。 还没有适配器。
我的 build.gradle
中有最新的支持库:
compile 'com.android.support:recyclerview-v7:23.2.0'
并查看最新的 android 开发者博客 post:
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()
调用的代码片段:
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
。
我有一个包含两个视图的聊天屏幕:一个用于显示以前聊天的回收器视图和一个封装编辑文本的自定义视图、一个发送按钮和另一个回收器视图让用户 select 其他东西发送喜欢的照片等。此自定义视图称为 "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>
我的 "Panel" 视图具有以下布局:
<?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
的回收器视图吞噬了整个屏幕 space。所以我所看到的只是 Panel class 先生的问题回收者观点。
这两个回收站视图都分配了 LinearLayoutManager
。 还没有适配器。
我的 build.gradle
中有最新的支持库:
compile 'com.android.support:recyclerview-v7:23.2.0'
并查看最新的 android 开发者博客 post:
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()
调用的代码片段:
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
。