7

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.

Kim Montano
  • 2,185
  • 4
  • 26
  • 39

3 Answers3

3

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.

foo
  • 574
  • 8
  • 13
0

I use androidx.recyclerview.widget.ListAdapter for adapter, I have the same problem: my view_holder_item has setted match_parent properties of width, but it appears on screen like 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">
...

Just make your onCreateViewHolder set LayoutParams to your ViewHolder View. In my case it looks like

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)
    }
amsuredev
  • 21
  • 1
-2

i think the problem is from

vg.getContext()

Let change to applicationContext or pass context to CustomAdapter

famfamfam
  • 396
  • 3
  • 8
  • 31
  • 1
    I tried it, it's not working. Also in the android documentation it shows that you should use vg.getContext. Check it out: https://developer.android.com/training/material/lists-cards.html Thanks, anyway! – Kim Montano May 15 '15 at 06:44