"Android Interface Effect 49" Recyclerview Height with item adaptive

Source: Internet
Author: User

When writing recyclerview.itemdecoration, in the OnDraw method, the height of the drawable is equal to the height of the recyclerview minus recyclerview up and down padding.

@Override Public voidOnDraw (Canvas C, Recyclerview parent, state state) {inttop =Parent.getpaddingtop (); intBottom = Parent.getheight ()-Parent.getpaddingbottom (); intChildCount =Parent.getchildcount ();  for(intI=0;i < childcount;i++) {View child=Parent.getchildat (i); Recyclerview.layoutparams Layoutparams=(Recyclerview.layoutparams) child.getlayoutparams (); intleft = Child.getright () +Layoutparams.rightmargin; intright = left +mdivider.getintrinsicwidth ();            Mdivider.setbounds (left, top, right, bottom);        Mdivider.draw (c); }    }

But the performance after the run is quite different from what I expected.


As you can see, the itemdecoration height is full screen, then check the XML layout file:

Activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" Match_parent "    android:layout_height = "Match_parent"    tools:context= "Com.xmy.recylerviewdemo.MainActivity" >    <  Android.support.v7.widget.RecyclerView        Android:id= "@+id/recyclerview"        android:layout_width = "Wrap_content"        android:layout_height= "Wrap_content"/>        </RelativeLayout>

Item.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:gravity= "Center"android:padding= "10.0dip"android:orientation= "Vertical" > <ImageView Android:id= "@+id/item_iv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:scaletype= "Center"android:src= "@drawable/img"Android:adjustviewbounds= "true"/> <TextView Android:id= "@+id/item_tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> </LinearLayout>

The height of Recyclerview and item in the XML layout file is set to Wrap_content, which is well adapted to item height? Look at the Android document, did not find about the Recyclerview height related instructions, it seems that only self-clothed.

According to Android-recylerview , Recyclerview is not responsible for the display of the item, and adapter is responsible for the data warehouse and item view, so it eventually locks the target to On the Recyclerview.layoutmanager. So try to inherit Linearlayoutmanager, found that there are onmeasure methods:

 Public void int widthspec,int heightspec)

Recyclerview.recycler can be obtained in the onmeasure. Recycler is responsible for managing the reuse of the item view, so we can get an instance of the item view through Recycler, and then, like the other ViewGroup, Use Measurechild to get the height of the child view and use the Setmeasureddimension setting to Recyclerview the same height.

 Public classMylayoutmanagerextendsLinearlayoutmanager { PublicMylayoutmanager (Context context) {Super(context); //TODO auto-generated Constructor stub} @Override Public voidOnmeasure (recycler recycler, state State,intWidthspec,intHeightspec) {View View= Recycler.getviewforposition (0); if(View! =NULL) {measurechild (view, Widthspec, Heightspec); intMeasuredwidth =measurespec.getsize (WIDTHSPEC); intMeasuredheight =view.getmeasuredheight ();        Setmeasureddimension (Measuredwidth, measuredheight); }    }}

run after the modification:


Finally, the GitHub link is on the sample program.

"Android Interface Effect 49" Recyclerview Height with item adaptive

Related Article

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.