Wrap_content on recycleview, take element height (horizontal list)

I have a RecycleView set to horizontal. Now I would like the height to match the height of the element in the view. I do not know the height in advance.

I currently have this:

The main fragment:

<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="horizontal" android:layout_marginLeft="1dp" android:layout_marginRight="1dp" android:layout_marginTop="1dp" android:layout_gravity="top" android:id="@+id/my_listview"/> </RelativeLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> 

Picture

 <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/banner_image" android:adjustViewBounds="true" android:scaleType="fitXY" android:background="@color/dark_gray" /> 

but it always takes up the whole screen. therefore, I have many gaps between them. Is there a way I could use wrap_content for recycleview elements?

It looks like this: enter image description here

Currently, I have placed 2 banners, the top one with a large space and a small image - one that has a recyclerview. The one below is just an image placed on the image (this is what it should look like, but I want it with recyclerview so that I can scroll through the banners if I have several)

+6
source share
4 answers

Now I would like the height to match the height of the element in the View. I do not know the height in advance.

During layout transfer, LinearLayoutManager cannot predict the height of all down views in the adapter that are yet to be created in advance.

You can measure() view the element, possibly the first from the adapter, and then set the height of the RecycleView through the code.

If all the elements can have different heights, you will have to measure them all, which, of course, is not the right way for a good user interface.

+1
source

You are probably not spreading the layout correctly.

Have you used

 layoutInflater.inflate(R.layout.your_layout, null); 

Try using instead. (Another way interferes with the measurement)

 layoutInflater.inflate(R.layout.your_layout, parent, false); 
0
source

Is there a reason you have a RecycleView wrapped inside a RelativeLayout ? First I tried to migrate the RecycleView from RelativeLayout .

Secondly, I see that your ViewPager has layout_weight , but I do not see the android:weightSum in the parent layout. Is there a reason you are specifying the ViewPager to have weight but not RecycleView ? Have you tried setting the size of the RecyleView to some dp to verify the layout is correct and the ViewPager uses the weight attribute correctly?

Thirdly, I would try to play with Imageview . Try maybe wrapping it inside a RelativeLayout , maybe? You will have to play around with the options there and see if this works for you. Since you want something custom, there is no direct answer to this question.

0
source

I also ran into the same problem, and I wanted to view two images in two columns with the same size in RecyclerView, and I could solve the problem using the code below. Try it.

 // 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); } } 
0
source

Source: https://habr.com/ru/post/981822/


All Articles