Android 匹配\父宽度在RecyclerView中不起作用
My RecyclerView和item具有匹配的父宽度,但结果是:Android 匹配\父宽度在RecyclerView中不起作用,android,android-recyclerview,Android,Android Recyclerview,My RecyclerView和item具有匹配的父宽度,但结果是: 看不到完整的代码,但可以猜测,LinearLayout中的某些视图是“wrap\u content”。您需要使用“android:layout\u weight=“1””使其中一个或一些扩展到全宽 更新: 您有很多冗余的布局权重。将它们全部“wrap\u content”并仅为其中一个添加最后一个CustomTextView的layout\u weight=1。这样,它就占据了所有的空白空间。在您的适配器中,您在代码中膨胀的
看不到完整的代码,但可以猜测,LinearLayout中的某些视图是“wrap\u content
”。您需要使用“android:layout\u weight=“1”
”使其中一个或一些扩展到全宽
更新:
您有很多冗余的布局权重。将它们全部“wrap\u content
”并仅为其中一个添加最后一个CustomTextView的layout\u weight=1
。这样,它就占据了所有的空白空间。在您的适配器中,您在代码中膨胀的项目> OnCuraTeVIEWHOLD ,是第二个参数<膨胀> <代码>调用<代码> null < /代码> .< /P>
如果是,则将其更改为parent
,这是onCreateViewHolder
函数签名中的第一个参数
View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, parent, false);
如果需要将第二个参数设置为null
,则在获取充气时的视图参考时,请执行以下操作
View rootView = LayoutInflater.from(context).inflate(R.layout.itemLayout, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(lp);
return new RecyclerViewHolder(rootView);
在适配器的onCreateViewHolder(…)方法中,在该方法中,您正在膨胀视图。。您必须将ViewGroup定义为父级。这将从onCreateViewHolder(…)方法的第一个参数获得
请参见我传递给ViewGroup的第二个参数中的下一行。这将自动将视图与其父视图匹配:
rowView=inflater.inflate(R.layout.home_custom_list, parent,false);
///完整的代码如下
public View onCreateViewHolder(ViewGroup parent, int position) {
// TODO Auto-generated method stub
View rowView;
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView=inflater.inflate(R.layout.home_custom_list, parent,false);
在适配器中为项目设置布局参数时,请尝试此操作
View viewHolder= LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
viewHolder.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
ViewOffersHolder viewOffersHolder = new ViewOffersHolder(viewHolder);
return viewOffersHolder;
我用以下方法解决了这个问题:
myInflatedRowLayout.getLayoutParams().width = vg.getWidth();
它将MATCH\u父对象
替换为RecyclerView的实际宽度我使用了FrameLayout
和MATCH\u父对象
作为宽度,并且看到了与RecyclerView
+LinearLayoutManager
相同的行为。在我在onCreateViewHolder
回调中执行以下操作之前,上述更改都不适用于我:
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.note_layout, parent, false);
v.setLayoutParams(new RecyclerView.LayoutParams(
((RecyclerView) parent).getLayoutManager().getWidth(),
context.getResources()
.getDimensionPixelSize(R.dimen.note_item_height)));
return new ViewHolder(v);
}
显然,在(我猜)RecyclerView实现中看起来像一个bug。我已经做了这样的修复。在我的例子中,活动布局文件存在问题,因为我使用ConstraintLayout作为根活动布局。您可能也是这样
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolBar"
layout="@layout/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/accent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolBar" />
</android.support.constraint.ConstraintLayout>
这对我很有效
替换这个
View view = View.inflate(parent.getContext(), R.layout.row_timeline, null);
return new TimeLineViewHolder(view, viewType);
由此
View rootView = LayoutInflater.from(context).inflate(R.layout.row_timeline, null, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(lp);
return new TimeLineViewHolder(rootView, viewType);
在我的例子中,问题出在RecyclerView
XML声明中,layout\u width
是0dp,这意味着匹配约束,当我将其更改为match\u parent
时,项目开始填充所有RecyclerView
宽度:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp" <-- changed this to "match_parent"
android:layout_height="0dp"
android:layout_marginBottom="45dp"
android:background="@android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="360dp"
app:layout_constraintHeight_min="60dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/header"/>
这个问题困扰了我一段时间,对我有效的解决方案是将两个视图放置在match_parent中,一个视图在另一个视图中
如果我在列表项的布局上执行此操作:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#F00"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
</RelativeLayout>
尽管它是一个相对布局,正如其他人所提到的,我正在传递父视图,但在充气机中为false,它根本不会显示(要检查的红色背景)
但是,当我这样做的时候:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:background="#0F0"
android:layout_height="match_parent"/>
</RelativeLayout>
绿色布局出现了,占据了整个空间
因此,只需在主视图中添加一个子视图并匹配父视图就可以解决问题,不知道为什么。只需在根视图中添加一个高度为0、全宽为0的虚拟视图,对我来说就行了。
对我不起作用:如果我将项目的rootView设置为width=“match\u parent”,但是如果我将其设置为500dp,它就会“起作用”。有什么想法吗?好的,它不适用于线性布局,但适用于项目的相对布局。。。wtf?Sry,由于它与relativeLayout一起工作,我只是将LinearLayout更改为relativeLayout。但RecyclerView是match_父级,而item rootView(因此LinearLayout)也是match_父级,因此无法工作。我第一次面对这个问题。哦,太好了,这很有效。在拉扯我的头发一个小时后。是的,正如前面提到的,出于某种原因,这不适用于LinearLayout并将其切换到RelativeLayout。使用此修复似乎可以解决问题。这不适用于LinearLayout或Constraint Layout。我不得不切换到Relativelayout,这很难让人相信,但用线性布局替换我周围的ConstraintLayout也解决了这个问题……match_parent不应该用于ConstraintLayout的直接子项。相反,将0dp与constraintLeft/Right/Bottom/Top属性一起使用。重写layoutParams是唯一对我有帮助的解决方案。有人知道这是否是RecyclerView中的一个bug吗?如果您能链接到Google bug tracker,我们将不胜感激。什么是ViewOffersHolder?:)您的代码不完整重量可能不是原因。但公平地说,它被称为match_parent。这对我来说很有效,谢谢。
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp" <-- changed this to "match_parent"
android:layout_height="0dp"
android:layout_marginBottom="45dp"
android:background="@android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="360dp"
app:layout_constraintHeight_min="60dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/header"/>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#F00"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:background="#0F0"
android:layout_height="match_parent"/>
</RelativeLayout>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".screen.gallery.GalleryFragment">
<!----DUMMY VIEW----->
<View
android:layout_width="match_parent"
android:layout_height="0dp"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/navigationList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>