Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android - View in RecyclerView does not match parent width

I have a horizontal 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"
        />

It has an item:

<?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>

Both controls' widths are set to match_parent however, the imagebutton won't "match" its parent's width.

I logged the width:

recyclerView Height: 338
recyclerView Width: 480

This is from the recyclerview method onCreateViewHolder:

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

I logged the view's height and width:

View v Height: 338
View v Width: 327

I tried setting the width programmatically:

@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;
    }

View v Height: 338
View v Width: 480

It works but I don't want to resize the view everytime.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

RecyclerView is very crappy when it comes to properly positioning it and/or its child views. Try

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

which basically replaces match_parent for both height & width within the child view. For your particular case you would only set the width then.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...