I am implementing RecyclerView
in my app to show a list of stuff. On my LG Nexus 5 (Android 5.1 M), if i set target sdk to 16 or 17 (basically to simulate what's happening on those phones with api 16, 17) my row height of the list changes dramatically.
However, this doesn't occur on API 18 or 18+. I cannot really figure out why this happens and what is the optimal approach to tackle this issue.
One More Thing: I use an ImageView
inside the list row. This is happening when in my ImageView
i am loading the images from the server. I use ImageLoader
library to do this. However if I use static images, then i don't have this issue
"if I use static images, then i don't have this issue"
When using static images, they are loaded in during onLayout()
for your ImageView
. When the image is loaded in during the layout, it affects the measured size in the onMeasure()
pass.
When the image isn't set statically(i.e. pulled down over network async), the ImageView
can't rely on the size of the image to determine its measured size. When the image is loaded, it will be shrunk to fit within the existing measured ImageView
.
Solutions:
minHeight
on your ImageView
or row layoutImageView
or row layoutIf you know what size you want for your row, you should use the fixed height.
There are default styles for RecyclerView
that can set attributes like minHeight
for your row layout that can vary across API versions. Thus without explicitly setting a fixed or minHeight
yourself, you can see different behavior across API versions.
I also have problem with height of recyclerView so found a solution. Give height from java code. It works for me.
recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
//use your layout
recyclerTagScroll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) recyclerTagScroll
.getLayoutParams();
params.height = recyclerTagScroll.getChildAt(0).getHeight();
recyclerTagScroll.setLayoutParams(params);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With