Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView height issue in API 17 and API 16 - Android

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

like image 498
stud91 Avatar asked Jul 30 '15 09:07

stud91


2 Answers

"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:

  • Specify a minHeight on your ImageView or row layout
  • OR Specify a fixed height on your ImageView or row layout

If 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.

like image 130
Trevor Carothers Avatar answered Sep 28 '22 09:09

Trevor Carothers


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);
	}
});
like image 34
Harvi Sirja Avatar answered Sep 28 '22 08:09

Harvi Sirja