在RecyclerView中,视图宽度不匹配父布局

7

我有一个水平的RecyclerView

<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,但是imagebutton无法“匹配”其父控件的宽度。

我记录了宽度:

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);

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

视图v高度:338
视图v宽度: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;
    }

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

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


1
你解决了这个问题吗?我遇到了类似的情况,但不知道该如何解决 :( - Gintama
1
你找到解决方案了吗? - prasunnair
1
我还没有重新审视这个问题。我记得使用了一个viewpager。请点赞该问题,以便更多的人对它感兴趣。 - Kim Montano
3个回答

3

在正确定位RecyclerView及其子视图方面,RecyclerView非常难以处理。

尝试:

@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,包括高度和宽度。 对于您的特定情况,您只需要设置宽度。


0

我使用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设置为ViewHolder View的LayoutParams即可。在我的情况下,它看起来像这样

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)
    }

-2

我认为问题来自于

vg.getContext()

让我们改为使用applicationContext或将context传递给CustomAdapter


1
我试过了,不起作用。而且在Android文档中显示应该使用vg.getContext。请查看:https://developer.android.com/training/material/lists-cards.html无论如何,谢谢! - Kim Montano

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接