Reputation: 127
I use RecyclerView with two columns and I want children views have height sized based on its content,
but by default children views are resized to the longest height of row. I have listview inside recycler view which is resized to height of its children.
ItemAdapter adapter = (ItemAdapter) holder.mItemList.getAdapter();
if(adapter == null){
adapter = new ItemAdapter(mItems.get(position));
holder.mItemList.setAdapter(adapter);
}
else
adapter.setItems(mItems.get(position));
adapter.notifyDataSetChanged();
holder.mItemList.invalidateViews();
setListViewHeightBasedOnItems(holder.mItemList);
Is there any easy way to make views fits or I have to create my own two column layout, similar to collection view in iOS ?
Upvotes: 1
Views: 960
Reputation: 2725
In my case I wanted view two images in two columns with the same size i did it using below code.
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imgViewIcon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
int colSpan = 2;
imgViewIcon.getLayoutParams().height = (int) (displayMetrics.widthPixels / colSpan);
imgViewIcon.getLayoutParams().width = (int) (displayMetrics.widthPixels / colSpan);
}
}
Instead of imgViewIcon you can put itemLayoutView and then set the layoutParams(width and height).
Upvotes: 1