Skip to content
Advertisement

RecyclerVIew item width doesn’t fill screen width

I’m filling a RecyclerView with custom view as a list item, ListItem, creating it from the onCreateViewHolder function.

The problem is, the list item view’s width isn’t filling the recyclerview (the parent) width, but in reality it wraps the width of the content (it’s set to match_parent not wrap_content).

I’m guessing that since I’m creating the view programmatically, it has no knowledge of it’s parent.

Here’s where I create the ListItem in the adapter:

@NonNull
@Override
public ListItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new ListItemHolder(new ListItem(parent.getContext()));
}

And the code for ListItem

public class ListItem extends LinearLayout {

    private ListItemBinding binding;
    private final Context mContext;

    public ListItem(Context context) {
        super(context);
        mContext = context;
        setUp();
    }

    public ListItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setUp();

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ListItem, 0, 0);
        setBigText(a.getString(R.styleable.ListItem_bigText));
        setSmallText(a.getString(R.styleable.ListItem_smallText));
        setImageDrawable(a.getDrawable(R.styleable.ListItem_src));
        setHeaderText(a.getString(R.styleable.ListItem_header));
        showHeader(binding.listItemHeaderText.getText() != null);
        a.recycle();
    }

    public void setUp() {
        binding = ListItemBinding.inflate(LayoutInflater.from(mContext), this, true);
        setClickable(true);
        setFocusable(true);
    }

    public void setBigText(@Nullable String text) {
        binding.listItemBigText.setText(text == null ? "" : text);
    }

    .
    .
    .
    .
    .
}

I tried passing the parent as a parameter to the ListItem constructor and inflating with that but it raised an error relating to the context.

How can I inflate the ListItem while keeping it as a seperate class and making it fill it’s parent width?

I could create a layout of the list item and inflate from a layout in onCreateViewHolder instead of inflating from a custom view class but I want to have it separated and reusable.

SOLUTION

Thanks to @Zain, hes solution was just to set the width to MATCH_PARENT through the view class, apparently it ignores the xml.

The change I did:

public class ListItem extends LinearLayout {
    .
    .
    .

    public void setUp() {
        binding = ListItemBinding.inflate(LayoutInflater.from(mContext), this, true);
        setClickable(true);
        setFocusable(true);
        *setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));*
    }

    .
    .
    .
}

Advertisement

Answer

Try to set it while instantiating it in onCreateViewHolder():

@NonNull
@Override
public ListItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    ListItem listItem = new ListItem(parent.getContext());
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                                     MATCH_PARENT, WRAP_CONTENT);
    listItem.setLayoutParams(params);
    return new ListItemHolder(listItem);
    
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement