I'm trying to set the height of a ViewHolder in onBindViewHolder after changing its content but it only works at the beginning. When it gets recycled, the heights are wrong. I guess it keeps the height of the previous content. Why does it not adjust it again? What am I doing wrong?
My code (simplified):
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mMyView.setText(holder.mItem.getValue());
holder.mView.measure(
View.MeasureSpec.makeMeasureSpec(Resources.getSystem().getDisplayMetrics().widthPixels, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
);
int layoutHeight = holder.mView.getMeasuredHeight();
holder.mMyView.getLayoutParams().height = layoutHeight;
//holder.mView.requestLayout();
}
public class ViewHolder extends RecyclerView.ViewHolder {
final View mView;
final TextView mMyView;
ViewHolder(View view) {
super(view);
mView = view;
mMyView= view.findViewById(R.id.myView);
}
}
Edit: My original code is a bit more complicated than this. I need to adjust the size of a view to match the size of another view. The code above is simplified to make it easier to read.
Edit 2: As requested, here is a simplified version of my XML to understand why I want to adjust the height. My goal is to adjust the ImageView height to match the height of the TextViews.
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/imageView"
app:layout_constraintStart_toStartOf="parent"
/>
<ImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Edit 3: I just set the height to a fixed value now. Calculating the height had a big impact on the performance anyway. Ridiculous how difficult it is to make layouts responsive in Android.
Thanks in advance.
Simply set height of the TextView to wrap_content in its XML file. Then change the onBindViewHolder as following:
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mMyView.setText(holder.mItem.getValue());
}
Height of the TextView will be change accordingly to its content.
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
//this is called when you scroll items
//this will reset to origin xml default items
//here heightChanged is the status , set true if changed
if(heightChanged){
//height according to logic
}else{
//do nothing
}
}
It seems like the height of items are different from each other, try adding status for height that has been changed (write logs to find out the height of each view you will get an idea how to solve this)
you have to set the height of Viewholder dynamically.just use,
public class ViewHolder extends RecyclerView.ViewHolder {
final View mView;
final TextView mMyView;
ViewHolder(View view) {
super(view);
mView = view;
mMyView= view.findViewById(R.id.myView);
view.setMinimumHeight(600); // 600 you can type you height that you want...
}
}
Related
how to control visibility in android?
if the user does not upload a photo, the ImageView will gone. In contrast, if the user has upload the photo the ImageView will visible.
so how can I do?
this is my xml code.
<LinearLayout
android:id="#+id/infophoto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/post_pic"
android:layout_width="match_parent"
android:layout_height="300dp"
android:scaleType="centerCrop"
android:src="#drawable/expic"
android:transitionName="sharedView"
android:visibility="invisible"
/>
</LinearLayout>
and this is my adapter code.
public void onBindViewHolder(final ViewHolder holder, final int position) {
glide.load(newFeedModels.get(position).getFeedimageURL()).into(holder.uploader_pro_pic);
glide.load(newFeedModels.get(position).getFeedPostImageURL()).into(holder.post_pic);
}
class ViewHolder extends RecyclerView.ViewHolder{
post_pic = (ImageView) itemView.findViewById(R.id.post_pic);
}
this is homefragment code.
String TAG_FeedMediaPhoto_URL = "FeedPostPhoto";
newFeedModel.setFeedimageURL(json.getString(TAG_FeedMediaPhoto_URL));
I need to modify where code?
At the First step is to check whether this particular item having pic or not
public void onBindViewHolder(final ViewHolder holder, final int position) {
if(!TextUtils.isEmpty(newFeedModels.get(position).getFeedPostImageURL())){
holder.post_pic.setVisibility(View.VISIBLE)
glide.load(newFeedModels.get(position).getFeedPostImageURL()).into(holder.post_pic);
}else{
holder.post_pic.setVisibility(View.GONE)
}
}
The android:visibility="invisible" command controls the visibility of the imageview section.
By using:imageview.setVisibility(View.visible); you can change the visibility of the imageview.
I am facing a strange error where recyclerview is showing only a single item. Below is code for my recyclerview adapter :
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<chat> chats;
String username;
final int My=0,Their=1;
public ChatAdapter(List<chat> chats) {
this.chats = chats;
this.username = PushApp.getApp().getSPF().getStringData(Constants.ANAME);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case My:
View v1 = inflater.inflate(R.layout.my_chat_child, parent, false);
viewHolder = new MyChatHolder(v1);
break;
case Their:
View v2 = inflater.inflate(R.layout.their_chat_child, parent, false);
viewHolder = new TheirMessageHolder(v2);
break;
default:
View v = inflater.inflate(R.layout.my_chat_child, parent, false);
viewHolder = new MyChatHolder(v);
break;
}
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case My:
MyChatHolder myChatHolder = (MyChatHolder) holder;
myChatHolder.setMyMessage(chats.get(position).getMessage());
break;
case Their:
TheirMessageHolder theirMessageHolder = (TheirMessageHolder) holder;
theirMessageHolder.setTheirChat(chats.get(position).getFrom(), chats.get(position).getMessage());
break;
}
}
#Override
public int getItemViewType(int position) {
if(username.equalsIgnoreCase(chats.get(position).getFrom()))
return My;
return Their;
}
#Override
public int getItemCount() {
return chats.size();
}}
I have already used this code in other app and its working perfectly. I have checked the chats data which is also perfect.
Here's link to git repo layout files:
layout files
Don't use match_parent for height for your item view. One item fills whole screen vertically so you don't see another.
when you are creating row.xml for recyclerview should follow these things:
Always use "wrap_content" for the height of the row otherwise in "match_parent" it will occupy the whole screen for a single row.
You can also take the height in dp.
My mistake was I accidentally used:
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
instead of:
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
content_main.xml as follows
android:layout_width="match_parent"
android:layout_height="match_parent"
IN row_list.xml file make following changes
android:layout_width="match_parent"
android:layout_height="wrap_content"
I make above changes it runs.
Try changing the layout used in your item view to FrameLayout. Here is an example.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item"
android:layout_width="match_parent"
android:layout_height="?listPreferredItemHeight"
android:clickable="true"
android:focusable="true"
android:foreground="?selectableItemBackground">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"/>
</FrameLayout>
After checking the log make sure multiple items are added in the list but in UI its showing only 1 item means check this properties.
In item_xml file change property to
Correct Approach
android:layout_width="match_parent"
android:layout_height="wrap_content"
wrong Approach
android:layout_width="match_parent"
android:layout_height="match_parent"
I have this issue today, and after 1001 minutes, I find out this come from this line inside RecyclerView:
android:layout_marginTop="10dp"
My RecyclerView was put inside NestedScrollView, I think that is the indirect cause.
So I put here for anyone else meet the same issue. here the image
1) if your recyclerview is vertical then set height of recyclerview match_parent and row_item.xml height also match_parent
2) if your recyclerview is horizontal then set Width of recyclerview match_parent and row_item.xml Width also match_parent
for ex:-
Horizontal RecyclerView
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp" />
row_item.xml
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/rect"
android:gravity="center"
android:maxLines="2"
android:padding="10dp"
android:textColor="#color/white"
android:textSize="25sp" />
There Fragment. Is it RecyclerView attached below. The bottom line is that by design is the first element should take a little more space than the others. His I increase in ViewHolder of the adapter when rendering as follows:
public class myAdapter extends RecyclerView.Adapter ...
...
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
WeatherForDay weather = mWeathersList.get(position);
holder.mDayTextView.setText(weather.getDay());
//todo update iconManager
//need icon manager, with input -> String, output ->(R.drawable.icon) int
holder.mIconImageView.setImageResource(R.drawable.testicon1);
holder.mTempTextView.setText(weather.getTmp());
if (position == 0) {
if (isFirstBind) {
Log.d(DEBUG_TAG, "is first bind and first position");
holder.setBig();
isFirstBind = false;
}
}
}
...
public void setBig() {
LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) mRoundLayout.getLayoutParams();
int newHeight = (int) (param.height * 1.2f);
int newWidth = (int) (param.height * 1.2f);
param.height = newHeight;
param.width = newWidth;
mRoundLayout.setLayoutParams(param);
mRoundLayout.setBackgroundDrawable(createBigShape(newHeight));
}
private Drawable createBigShape(int newHW) {
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setCornerRadii(new float[]{newHW, newHW, newHW, newHW, newHW, newHW, newHW, newHW});
shape.setColor(mContext.getResources().getColor(R.color.weryDark));
shape.setStroke(1, mContext.getResources().getColor(R.color.weryDark));
return shape;
}
...
}
How to make elements are aligned along the upper edge and the lower?
P.S. The same problem arises in that the N-element also somehow rendered "large" (on the same device with a large screen N = 13, on the small screen N = 8)
P.S.2 Can eat what is more optimal way to change any element RecyclerView?
item_recycler.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="#+id/day_text_view_list_item_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorWhite"
android:background="#null"/>
<LinearLayout
android:id="#+id/rounded_linear_layout"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/round_shape"
android:gravity="center_horizontal"
android:orientation="vertical">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/icon_image_view_list_item_bottom"
android:layout_width="42dp"
android:layout_height="42dp"
android:background="#null"
app:srcCompat="#drawable/testicon1"
tools:ignore="MissingPrefix"/>
<TextView
android:id="#+id/temperature_text_view_list_item_bottom"
android:textColor="#color/colorWhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
For the gravity issue:
You have to set android:layout_gravity="center_vertical" or android:layout_gravity="center" to your item's root element (LinearLayout).
In addition:
Instead of changing LayoutParams manually, I suggest you to use viewType and create different layouts for position 0 and position > 0. This way, you ensure only position 0 item is bigger and you don't modify it's natural functioning.
You would add this method:
private static final int TYPE_BIG = 0;
private static final int TYPE_STANDARD = 1;
#Override
public int getItemViewType(int position) {
return position == 0 ? TYPE_BIG : TYPE_STANDARD;
}
And modify your onCreateViewHolder:
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int layout = viewType == TYPE_BIG
? R.layout.item_recycler_big
: R.layout.item_recycler;
View v = View.inflate(parent.getContext(), layout, parent);
return new ViewHolder(v);
}
And your onBindViewHolder would be as simple as:
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
WeatherForDay weather = mWeathersList.get(position);
holder.mDayTextView.setText(weather.getDay());
holder.mIconImageView.setImageResource(R.drawable.testicon1);
holder.mTempTextView.setText(weather.getTmp());
}
Hope this helps you!
Don't use position that provided from onBindViewHolder(final ViewHolder holder, final int position), it's not permanent (that's why your N-element also "large"). Use holder.getAdapterPosition() instead
Did you try to use some Gravity? For example:
a) Define android:gravity="center_vertical" for RecyclerView item layout
b) Define android:layout_gravity="center_vertical" for mRoundLayout
c) Define android:layout_centerInParent or
android:layout_centerVertical ="true"
I am facing a strange error where recyclerview is showing only a single item. Below is code for my recyclerview adapter :
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<chat> chats;
String username;
final int My=0,Their=1;
public ChatAdapter(List<chat> chats) {
this.chats = chats;
this.username = PushApp.getApp().getSPF().getStringData(Constants.ANAME);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType) {
case My:
View v1 = inflater.inflate(R.layout.my_chat_child, parent, false);
viewHolder = new MyChatHolder(v1);
break;
case Their:
View v2 = inflater.inflate(R.layout.their_chat_child, parent, false);
viewHolder = new TheirMessageHolder(v2);
break;
default:
View v = inflater.inflate(R.layout.my_chat_child, parent, false);
viewHolder = new MyChatHolder(v);
break;
}
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case My:
MyChatHolder myChatHolder = (MyChatHolder) holder;
myChatHolder.setMyMessage(chats.get(position).getMessage());
break;
case Their:
TheirMessageHolder theirMessageHolder = (TheirMessageHolder) holder;
theirMessageHolder.setTheirChat(chats.get(position).getFrom(), chats.get(position).getMessage());
break;
}
}
#Override
public int getItemViewType(int position) {
if(username.equalsIgnoreCase(chats.get(position).getFrom()))
return My;
return Their;
}
#Override
public int getItemCount() {
return chats.size();
}}
I have already used this code in other app and its working perfectly. I have checked the chats data which is also perfect.
Here's link to git repo layout files:
layout files
Don't use match_parent for height for your item view. One item fills whole screen vertically so you don't see another.
when you are creating row.xml for recyclerview should follow these things:
Always use "wrap_content" for the height of the row otherwise in "match_parent" it will occupy the whole screen for a single row.
You can also take the height in dp.
My mistake was I accidentally used:
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
instead of:
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
content_main.xml as follows
android:layout_width="match_parent"
android:layout_height="match_parent"
IN row_list.xml file make following changes
android:layout_width="match_parent"
android:layout_height="wrap_content"
I make above changes it runs.
Try changing the layout used in your item view to FrameLayout. Here is an example.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item"
android:layout_width="match_parent"
android:layout_height="?listPreferredItemHeight"
android:clickable="true"
android:focusable="true"
android:foreground="?selectableItemBackground">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"/>
</FrameLayout>
After checking the log make sure multiple items are added in the list but in UI its showing only 1 item means check this properties.
In item_xml file change property to
Correct Approach
android:layout_width="match_parent"
android:layout_height="wrap_content"
wrong Approach
android:layout_width="match_parent"
android:layout_height="match_parent"
I have this issue today, and after 1001 minutes, I find out this come from this line inside RecyclerView:
android:layout_marginTop="10dp"
My RecyclerView was put inside NestedScrollView, I think that is the indirect cause.
So I put here for anyone else meet the same issue. here the image
1) if your recyclerview is vertical then set height of recyclerview match_parent and row_item.xml height also match_parent
2) if your recyclerview is horizontal then set Width of recyclerview match_parent and row_item.xml Width also match_parent
for ex:-
Horizontal RecyclerView
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp" />
row_item.xml
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/rect"
android:gravity="center"
android:maxLines="2"
android:padding="10dp"
android:textColor="#color/white"
android:textSize="25sp" />
I have a StaggeredGrid in a RecyclerView and I am trying to dynamically set the image height in the onBindViewHolder. I thought the StaggeredGrid was supposed to handle all this automatically, so I set android:layout_width="match_parent and android:scaleType="fitStart on the ImageView, but there is lots of gaps around the image.
Since the StaggeredGrid isn't living up to it, I'm trying to help it a bit by defining the image height dynamically in the onBindViewHolder. I have the width and height of the image ahead of time, but the cell width of the grid isn't available. I tried holder.cardView.getLayoutParams().width and getMeasuredWidth(), but they both return zero or small numbers. I know there are other places the cell width would be available, but I really need it in the onBindViewHolder event so I can set and adjust the image.
Any ideas on how to achieve this by leveraging the StaggeredGrid? Any advice or experience is appreciated!
In my activity is this:
mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
In my adapter:
#Override
public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
MyModel item = mData.get(position);
int imageWidth = item.getFeatured_image().getWidth();
int imageHeight = item.getFeatured_image().getHeight();
int cellWidth = 540; // <==== how to find dynamically??!!
ViewGroup.LayoutParams imageLayoutParams = holder.thumbnailImageView.getLayoutParams();
imageLayoutParams.width = cellWidth;
imageLayoutParams.height = imageHeight * cellWidth / imageWidth;
holder.thumbnailImageView.setLayoutParams(imageLayoutParams);
MediaHelper.displayImage(item, holder.thumbnailImageView);
}
In my layout:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/row_my_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="2dp">
<ImageView
android:id="#+id/row_my_thumbnail_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:scaleType="fitStart" />
<TextView
android:id="#+id/row_my_title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#CCFFFFFF"
android:textStyle="bold"
android:padding="5dp" />
</android.support.v7.widget.CardView>
I also tried to add this which does give me the cell width, but the onGlobalLayout event gets triggered way after the image is set and adjusted in the onBindViewHolder event so it's too late.
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
final View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_my_post, parent, false);
final MyViewHolder viewHolder = new MyViewHolder(view);
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int viewWidth = view.getWidth();
int viewHeight = view.getHeight();
}
});
}
return viewHolder;
}
Here is the gaps which are random due to the various image sizes. What I would like is for ALL images to be the width of the cell and let the height of the image and cell adjust dynamically:
Try This:- add this in your ImageView--> android:adjustViewBounds="true"
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/row_my_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardCornerRadius="2dp">
<ImageView
android:id="#+id/row_my_thumbnail_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:adjustViewBounds="true"/>
<TextView
android:id="#+id/row_my_title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#CCFFFFFF"
android:textStyle="bold"
android:padding="5dp" />
</android.support.v7.widget.CardView>
Instead of getting the view height and width from onCreateViewHolder get it from onBindViewHolder as below:
#Override
public void onBindViewHolder(final MyAdapter.MyViewHolder holder, int position)
{
holder.itemView.post(new Runnable()
{
#Override
public void run()
{
MyModel item = mData.get(position);
int imageWidth = item.getFeatured_image().getWidth();
int imageHeight = item.getFeatured_image().getHeight();
int cellWidth = 540; // <==== how to find dynamically??!!
cellWidth = holder.itemView.getWidth();// this will give you width dynamically
ViewGroup.LayoutParams imageLayoutParams = holder.thumbnailImageView.getLayoutParams();
imageLayoutParams.width = cellWidth;
imageLayoutParams.height = imageHeight * cellWidth / imageWidth;
holder.thumbnailImageView.setLayoutParams(imageLayoutParams);
MediaHelper.displayImage(item, holder.thumbnailImageView);
}
});
}
I hope this will help you!!
The cell's width can be found at activity/fragment level and passed to the adapter before rendering time. Use something like this:
this.staggeredGridLayoutManager.getWidth() / this.staggeredGridLayoutManager.getSpanCount()
With this approach, you will be able to compute the cell's height before any rendering and avoid that initial flick introduced by post run approach.
Also, the image's aspect ratio will be preserved and you can reserve the required space before image load.
Be also aware that if any margins/paddings are present you should include them into previous calculation. Something like:
(this.staggeredGridLayoutManager.getWidth() - marginsAndOrPaddings) / this.staggeredGridLayoutManager.getSpanCount()
Update:
Starting with support packages 23.2.0, and at this point of lifecycle, this.staggeredGridLayoutManager.getWidth() returns 0. The total width can be achieved with this.recyclerView.getWidth()