更新时间:2023-11-07 10:18:04
Updated
Updating the answer to more to reporter request, as she wants to show all the view in the same height and of highest height as per largest description.
I have done a few tweeks in the code.
Created a method to calculate the projected height of textView by trying all the description in the list to get the highest height.
public static int getHeightOfLargestDescription(final Context context, final CharSequence text, TextView textView) {
final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
final Point displaySize = new Point();
wm.getDefaultDisplay().getSize(displaySize);
final int deviceWidth = displaySize.x;
textView.setTypeface(Typeface.DEFAULT);
textView.setText(text, TextView.BufferType.SPANNABLE);
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
return textView.getMeasuredHeight();
}
then used this method to in onCreateViewHolder
to get ready with the highest height to be used while binding the view.
MyViewHolder myViewHolder = new MyViewHolder(itemView);
for (Model m : modelList) {
currentItemHeight = getHeightOfLargestDescription(context, m.description, myViewHolder.description);
if (currentItemHeight > highestHeight) {
highestHeight = currentItemHeight;
}
}
Then used this highestHeight
in onBindViewHolder` to set the height of the description TexView, so that all the views always have the same height that is equal to the highest height.
viewHolder.description.setHeight(highestHeight);
Code is committed in the same repo
Previous Solution I have created the solution for your problem and committed in GitHub https://github.com/dk19121991/HorizontalRecyclerWithDynamicHeight
Let me share the little bit gist of the solution
You can get the height of the description view onBindViewHolder and store that to check for if that's highest or not and always set the height of the new view with the highest height you got till now.
viewHolder.description.post(new Runnable() {
@Override
public void run() {
currentItemHeight = viewHolder.description.getMeasuredHeight();
if (currentItemHeight > highestHeight) {
highestHeight = currentItemHeight;
/** this is needed to make sure already inflated view with small description also get resized to largest view till now
*and this be called only if the new view is of larger size then all of the view inflated till now
*/
notifyDataSetChanged();
}
viewHolder.description.setHeight(highestHeight);
}
});
You might have the question what if the older views which are already created are smaller and the new view which is going to be treated will be larger due to the large description.
No worry at all. To solve that I added notifyDataSetChanged();
whenever we are encountering a new highest height, this will make sure all the older view also get the new bigger height.
Click on this to see the app in action https://github.com/dk19121991/HorizontalRecyclerWithDynamicHeight/blob/master/runningSample.gif
Let me know if this solves your problem, if you have some more question feel free to ask.
Thanks