ScrollView nested Recyclerview, android:layout_height= "wrap_content" does not work, and Recyclerview fills the remaining entire screen space, which is equivalent to Android: Layout_height= "Match_parent" to reset the onmeasure height by overriding the Recyclerview method of Gridlayoutmanager or Linearlayoutmanager.
Here are just a few examples of Gridlayoutmanager, linearlayoutmanager similar
A. Setting LayoutManager
rvPhotos.setLayoutManager(new PhotoLayoutManage(this3));
B.recyclerview's Adapter
Adapter defines the height of the variable in item
privateint itemHeight;publicintgetItemHeightreturn itemHeight;}
Sets the height of the item item display in the Viewholder construction method of the adapter
itemView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override publicbooleanonPreDraw() { itemHeight=convertView.getMeasuredHeight(); returntrue; } });
C. Custom Gridlayoutmanager override Onmeasure method
Public class photolayoutmanage extends gridlayoutmanager{ //Recyclerview height with item adaptive Public Photolayoutmanage(Context context,intSpancount) {Super(Context,spancount); }@Override Public void onmeasure(Recyclerview.recycler recycler, Recyclerview.state State,Final intWidthspec,Final intHEIGHTSPEC) {Try{//cannot use view view = recycler.getviewforposition (0); //measurechild (view, Widthspec, heightspec); //int measuredheight view.getmeasuredheight (); This height is inaccurate. if(adapter!=NULL&&adapter.getitemheight () >0) {intMeasuredwidth = View.MeasureSpec.getSize (Widthspec);intMeasuredheight = Adapter.getitemheight () +rvphotos.getpaddingbottom () +rvphotos.getpaddingtop ();intline = Adapter.getitemcount ()/Getspancount ();if(Adapter.getitemcount ()% Getspancount () >0) line++; Setmeasureddimension (measuredwidth, measuredheight * line); }Else{Super. Onmeasure (Recycler,state,widthspec,heightspec); } }Catch(Exception e) {Super. Onmeasure (Recycler,state,widthspec,heightspec); } } }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Recyclerview height is available with the item adaptive Gridlayoutmanager and Linearlayoutmanager