Recyclerview height is available with the item adaptive Gridlayoutmanager and Linearlayoutmanager

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.