RecyclerView项目宽度layout_width =“match_parent”与父项不匹配

我正在使用RecyclerView,我正在尝试使RecyclerView match_parent的宽度项目,因为我正在使用

LinearLayoutManager.HORIZONTAL

运行后的RecyclerView项目

<android.support.v7.widget.RecyclerView android:id="@+id/listOffersHits" android:layout_width="match_parent" android:layout_height="match_parent" /> 

custom_layout.xml

 <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="1" android:layout_margin="2dp" card_view:cardCornerRadius="2dp"> <ImageView android:id="@+id/offerImage" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/item_selector" android:scaleType="fitXY" android:src="@drawable/offer" /> </android.support.v7.widget.CardView> 

布局pipe理

  LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL, false); listOffersHits.setLayoutManager(linearLayoutManager); 

ViewHolder

 View view= LayoutInflater.from(parent.getContext()) .inflate(R.layout.custom_layout, parent, false); ViewOffersHolder viewHolder=new ViewOffersHolder(view); return viewHolder; 

Solutions Collecting From Web of "RecyclerView项目宽度layout_width =“match_parent”与父项不匹配"

我解决了这个问题:

1-获取屏幕大小(宽度)。

2-使ViewHolder宽度与屏幕大小相同。

下面我的代码,如果任何人需要它。

 WindowManager windowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); int width = windowManager.getDefaultDisplay().getWidth(); int height = windowManager.getDefaultDisplay().getHeight(); view.setLayoutParams(new RecyclerView.LayoutParams(width, RecyclerView.LayoutParams.MATCH_PARENT)); 

注意:请参阅本文最后的更新。


老答案:

以下为我工作:

 view.getLayoutParams ().width = parentViewGroup.getWidth (); 

,在膨胀视图“视图”之后。

它基本上采用父宽度,并用父宽度replace新视图的layout_width。

您还可以添加对MATCH_PARENT的检查,以确保它在以后将XML修改为不同的宽度时正确显示。 所以像这样:

  public ViewHolder onCreateViewHolder (ViewGroup parent, int type) { View vItem = LayoutInflater.from (parent.getContext ()) .inflate (R.layout.bookmark_card, parent, false); if (vItem.getLayoutParams ().width == RecyclerView.LayoutParams.MATCH_PARENT) vItem.getLayoutParams ().width = parent.getWidth (); return new ViewHolder (vItem); } 

如果在此之前宽度为MATCH_PARENT,则只会将其设为父宽度。

编辑:

请注意过度使用此修复程序,因为它的一个变种导致了我的布局错误。 我做到了这一点:

  public ViewHolder onCreateViewHolder (ViewGroup parent, int type) { View vItem = LayoutInflater.from (parent.getContext ()) .inflate (R.layout.page_tile, parent, false); if (vItem.getLayoutParams ().width == RecyclerView.LayoutParams.MATCH_PARENT) vItem.getLayoutParams ().width = parent.getWidth (); if (vItem.getLayoutParams ().height == RecyclerView.LayoutParams.MATCH_PARENT) vItem.getLayoutParams ().height = parent.getHeight (); return new ViewHolder (vItem); } 

这工作,但它没有更新的宽度和高度RecyclerView的大小调整。 因此,可以应用此修复程序的两个修复程序之一:

  1. 检查RecyclerView适配器的方向,只对这个parent.getHeight()等进行滚动的尺寸。
  2. 或者,在ViewHolder中的某处,保存原始的“宽度”和“高度”,然后每次在onBindViewHolder()方法中重做此步骤。

下面的修复似乎工作:

  public ViewHolder onCreateViewHolder (ViewGroup parent, int type) { View vItem = LayoutInflater.from (parent.getContext ()) .inflate (R.layout.page_tile, parent, false); ViewHolder holder = new ViewHolder (vItem); // Create view holder. holder.vItem = vItem; // Save top-level View. holder.vParent = parent; // Save its parent. ViewGroup.LayoutParams lp = vItem.getLayoutParams (); if (lp != null) { // Save the original width and height from lp: holder.originalWidth = lp.width; holder.originalHeight = lp.height; refreshLayoutParams (holder); // Check for MATCH_PARENT. } else holder.originalHeight = holder.originalWidth = 0; return holder; } public void onBindViewHolder (ViewHolder holder, int position) { ... do work here ... refreshLayoutParams (holder); // Check AGAIN for MATCH_PARENT. } private void refreshLayoutParams (ViewHolder holder) { ViewGroup.LayoutParams lp = holder.vItem.getLayoutParams (); View parent = holder.vParent; if (parent == null) return; // Is there a parent to check against? if (lp == null) holder.vItem.setLayoutParams (lp = new RecyclerView.LayoutParams (holder.originalWidth, holder.originalHeight)); if (holder.originalWidth == RecyclerView.LayoutParams.MATCH_PARENT) lp.width = parent.getWidth (); // Check width. if (holder.originalHeight == RecyclerView.LayoutParams.MATCH_PARENT) lp.height = parent.getHeight (); // Check height. } 

另请注意:如果在onBindViewHolder()方法中使用这种resize的方法,则还需要在Activity.onSizeChanged()中调用getAdapter().notifyDataSetChanged ()

简而言之,主要思想是:检查每个绑定上的MATCH_PARENT,而不仅仅是创build视图持有者。 这样调整RecyclerView的作用。


更新:至lessRecyclerView v7:23.2.1,似乎他们已经修复它,所以现在这些自定义修复不再是必要的。 RecyclerView本身现在应该识别WRAP_CONTENT和MATCH_PARENT设置。

在布局中删除这个。

 android:layout_weight="1" 

并再次在适配器中设置项目的布局参数。

 View view= LayoutInflater.from(parent.getContext()) .inflate(R.layout.custom_layout, parent, false); view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT)); ViewOffersHolder viewHolder=new ViewOffersHolder(view); return viewHolder;