RecyclerView子对象match_parent [英] RecyclerView children match_parent
问题描述
我的问题与这个问题非常相似,但是,与OP不同,我将parent
传递给LayoutInflater
.我正在使用支持库25.3.1.这是我要夸大的布局(将ConstraintLayout
更改为LinearLayout
并没有帮助):
My problem is very similar to this question, however unlike it's OP I'm passing the parent
to the LayoutInflater
. I'm using support library 25.3.1. Here's the layout that I'm inflating (Changing the ConstraintLayout
to LinearLayout
doesn't help):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="72dp"
android:background="?android:selectableItemBackground"
android:clickable="true"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/list_text_primary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.ListPrimary"
app:layout_constraintBottom_toTopOf="@+id/list_text_secondary"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed"
tools:ignore="Deprecated"
tools:text="list_text_primary"/>
<TextView
android:id="@+id/list_text_secondary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:gravity="center_vertical"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.ListSecondary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/list_text_primary"
tools:ignore="Deprecated"
tools:text="list_text_secondary"/>
</android.support.constraint.ConstraintLayout>
我还尝试按照根据此答案,RecyclerView
应该已经由我测量了onCreateViewHolder
被调用.
I've also tried to set the LayoutParams
manually in onCreateViewHolder
as described According to this answer the RecyclerView
should already me measured by the time onCreateViewHolder
is called.
推荐答案
如果粘贴适配器代码,效果会更好,但是可以尝试一下.
If you paste your adapter code, that would have been better, but you can try this.
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
windowManager.getDefaultDisplay().getSize(size);
holder.yourItemBinding.getRoot().setLayoutParams(new RecyclerView.LayoutParams(size.x,
RecyclerView.LayoutParams.WRAP_CONTENT));
替换
holder.yourItemBinding.getRoot()
holder.yourItemBinding.getRoot()
如果不使用布局绑定,请使用
与根视图对象一起使用.
with your root view object if you are not using layout binding.
这篇关于RecyclerView子对象match_parent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!