1

I have a question about RecyclerView. I was trying to learn RecyclerView but I stuck with a problem. I am creating all views dynamically. So, Let say I have a ContentLinearLayout. My problem starts with "refreshContent" method. I am calling this several times. But, even on the first call, the inside views of RecyclerView stays in their background image size (I guess there is some implicit WRAP_CONTENT somewhere). I couldn't find a way to equalize the heights and widths of the buttons to the height of the RecyclerView. I tried iterating over RecyclerView and setting layout params for every child, but it didn't work. Also, I tried to set layout params inside "onBindViewHolder" but that didn't work, too.

Any help will be greatly appreciated.

public class ContentLinearLayout extends LinearLayout {

    private RecyclerView rvCon;
    private LinearLayout llCon;

    public ContentLinearLayout(Context context) {
        super(context);
        this.init(context);
    }

    public ContentLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init(context);
    }

    private void init(Context c){
        this.setOrientation(LinearLayout.VERTICAL);
        this.setBaselineAligned(false);

        rvCon = new RecyclerView(c);
        LinearLayout.LayoutParams recyclerPrms = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f);
        recyclerPrms.setMargins(0, 0, 0, 0);
        rvCon.setLayoutParams(recyclerPrms);
        LinearLayoutManager manager = new LinearLayoutManager(c, LinearLayoutManager.HORIZONTAL, false);
        rvCon.setLayoutManager(manager);
        addView(rvCon);

        rvCon.setPadding(0, 0, 0, 0);

        LinearLayout llCon = new LinearLayout(c);
        llCon.setOrientation(LinearLayout.HORIZONTAL);
        llCon.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 3f));
        llCon.setBaselineAligned(false);
        addView(container);

        ...
    }

    private void refreshContent(Context ctx, Content content){
        rvCon.setAdapter(new ContentAdapter(ctx, content));
    }
}

public class ContentAdapter extends RecyclerView.Adapter<ImageButtonHolder>{

    private Context mContext;
    private Content mContent;

    public ContentAdapter(Context c, Content content){
        mContext = c;
        mContent = content
    }

    @Override
    public int getItemCount() {
        return content.size();
    }

    @Override
    public void onBindViewHolder(ImageButtonHolder buttonHolder, int pos) {

    }

    @Override
    public ImageButtonHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
        ImageButton b = new ImageButton(mContext);
        b.setBackgroundColor(Color.RED);
        ImageButtonHolder holder = new ImageButtonHolder(b);
        return holder;
    }
}

public class ImageButtonHolder extends RecyclerView.ViewHolder {

    public final ImageButton imageButton;

    public ImageButtonHolder(ImageButton button) {
        super(button);
        imageButton = button;
    }

}
Murat ARI
  • 11
  • 1
  • 3
  • sth is very wrong in your ContentAdapter. Every time RV needs a new view, you increment `num` and when `getItemCount()` is called you are returning `num.get()`. These are two unrelated things. `onCreateViewHolder(ViewGroup parent, int type)`, the second parameter here is the type of the view. Creating a new view should not change the adapter size. You can check [here](https://developer.android.com/training/material/lists-cards.html) for example RV usage. – yigit Dec 23 '14 at 21:34
  • Ok, I didn't thought it would be relevant at first place. So, I edited. – Murat ARI Dec 24 '14 at 22:31

1 Answers1

4

I had the same problem: View in RecyclerView does not match parent width

Try setting the width according to the device's width:

Adapter Code:

private Activity activity;
private int imageWidth = 0;

public RecommendedVendorsAdapter(Activity activity) {
    recommendedVendors = VendorListingManager.getInstance().getRecommended();
    this.activity = activity;
    imageWidth = activity.getWindow().getDecorView().getWidth();
}

@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=imageWidth;
    v.setLayoutParams(params);

    RecommendedVendorsViewHolder vh = new RecommendedVendorsViewHolder(v);
    return vh;
}

I hope it helps you.

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