RecyclerView 中的视图与父级宽度不匹配

View in RecyclerView does not match parent width

我有一个水平回收视图:

<android.support.v7.widget.RecyclerView
        android:id="@+id/rvRecommendedVendors"
        android:scrollbars="none"
        android:layout_width="match_parent"
        android:layout_height="225dp"
        android:visibility="visible"
        android:layout_below="@+id/llRecommendedVendors"
        android:overScrollMode="never"
        android:background="@color/dark_blue"
        />

它有一个项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="225dp"
        android:id="@+id/ibRecommendedCoverPhoto"
        android:src="@drawable/sample_cover_image"
        android:background="@android:color/darker_gray"
        android:scaleType="centerCrop"/>
</LinearLayout>

两个控件的宽度都设置为 match_parent 但是,图像按钮不会 "match" 其父控件的宽度。

我记录了宽度:

recyclerView 高度:338
recyclerView 宽度:480

这是来自recyclerview方法onCreateViewHolder:

  @Override
    public RecommendedVendorsViewHolder onCreateViewHolder(ViewGroup vg, int viewType) {
    View v = LayoutInflater.from(vg.getContext()).inflate(R.layout.item_recommended_list, vg, false);

我记录了视图的高度和宽度:

视角高度:338
视图宽度:327

我尝试以编程方式设置宽度:

@Override
    public RecommendedVendorsViewHolder onCreateViewHolder(ViewGroup vg, int viewType) {
        View v = LayoutInflater.from(vg.getContext()).inflate(R.layout.item_recommended_list, vg, false);
        RecyclerView.LayoutParams params= (RecyclerView.LayoutParams)v.getLayoutParams();
        params.width=480;
        v.setLayoutParams(params);

        return vh;
    }

视图 v 高度:338
视图宽度:480

它可以工作,但我不想每次都调整视图的大小。

我认为问题来自

vg.getContext()

让更改为 applicationContext 或将上下文传递给 CustomAdapter

RecyclerView 在正确定位它 and/or 它的子视图时非常糟糕。 尝试

@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
    final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    final View v = inflater.inflate(R.layout.item, parent, false);

    // match_parent won't work for RecyclerView (sigh)
    final ViewGroup.LayoutParams lp = v.getLayoutParams();
    lp.width = parent.getWidth();
    lp.height = parent.getHeight();
    v.setLayoutParams(lp);

    return new ViewHolder(v);
}

基本上替换了子视图中高度和宽度的 match_parent。 对于您的特定情况,您只需设置宽度即可。

我使用androidx.recyclerview.widget.ListAdapter作为适配器, 我有同样的问题:我的 view_holder_item 已经设置了宽度 的 match_parent 属性,但是它 出现在屏幕上就像 wrap_content.

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="8dp">
...

只需让您的 onCreateViewHolder 将 LayoutParams 设置为您的 ViewHolder View。就我而言,它看起来像

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StopWatchViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding = StopWatchItemBinding.inflate(layoutInflater)
        val lp = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        binding.root.layoutParams = lp
        return StopWatchViewHolder(binding)
    }