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

时间:2015-05-15 04:39:45

标签: android android-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

这来自于reinlerView方法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;
    }

查看v高度:338
查看v宽度:480

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

3 个答案:

答案 0 :(得分:2)

在正确定位和/或其子视图时,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的高度和高度子视图中的宽度。 对于您的特定情况,您只需设置宽度。

答案 1 :(得分: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 将 LayoutParams 设置为您的 ViewHolder 视图。就我而言,它看起来像

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 :(得分:-2)

我认为问题来自

  

vg.getContext()

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

相关问题