Android Recyclerview nested Recyclerview

Source: Internet
Author: User

Principle

Recyclerview nested Recyclerview entries, the project may often have such requirements, but we set the child entry to Recyclerview, but it does not appear. I tried for a long time, and finally found the reason: the height of the sub-Recylcerview must be set first . You have to spend energy to determine the height of the entry in the sub-recyclerview, and then determine the height of the sub-Recyclerview, set to sub-recylcerview, so that Recyclerview can correctly display the sub-reclyclerview.

Effect

Code

The home page layout is a vertically arranged recyclerview

<?xml version="1.0"encoding="Utf-8"?><Android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"Android:id="@+id/recylcerview"Android:layout_width="match_parent"Android:layout_height="match_parent"/>

Next, initialize the layout in mainactivity and then make some false data

 protected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Basicparaminit ();        InitData ();    Initrecyclerview (); }    Private  voidBasicparaminit () {displaymetrics metric=NewDisplaymetrics ();        Getwindowmanager (). Getdefaultdisplay (). Getmetrics (Metric); ScreenWidth=Metric.widthpixels; }    Private voidInitData () {Data=Newdatainfor (); ArrayList<Integer> resourcelist =NewArraylist<>();        Resourcelist.add (R.DRAWABLE.AAA);        Resourcelist.add (R.mipmap.ic_launcher);        Resourcelist.add (R.DRAWABLE.AAA);        Resourcelist.add (R.mipmap.ic_launcher);        Resourcelist.add (R.DRAWABLE.AAA);        Resourcelist.add (R.mipmap.ic_launcher);        Resourcelist.add (R.DRAWABLE.AAA);        Resourcelist.add (R.mipmap.ic_launcher); Data.griddata= Data.horizontaldata = Data.verticaldata =resourcelist; }    Private voidInitrecyclerview () {Recylcerview=(Recyclerview) Findviewbyid (R.id.recylcerview); Recylcerview.setlayoutmanager (NewLinearlayoutmanager ( This, Linearlayoutmanager.vertical,false));        Recylcerview.setbackgroundresource (R.COLOR.C_E0E0E2); Recylcerview.setadapter (NewRecyclerviewadapter ()); }

Next look at Recyclerview's adapter:

Private classRecyclerviewadapter extends Recyclerview.adapter<baseholder>{        PrivateFinalintHorizontal_view = +; PrivateFinalintVertical_view =1001; PrivateFinalintGrid_view =1002; @Override PublicBaseholder Oncreateviewholder (viewgroup parent,intViewType) {            Switch(viewtype) { CaseHorizontal_view:return NewHorizontalviewholder (R.layout.item_recyclerview,parent,viewtype);  CaseGrid_view:return NewGridviewholder (R.layout.item_recyclerview,parent,viewtype);  CaseVertical_view:return NewItemviewholder (R.layout.item_x2_imageview,parent,viewtype); }            return NULL; } @Override Public voidOnbindviewholder (Baseholder holder,intposition) {            if(Holder instanceof Horizontalviewholder) {holder.refreshdata (data.horizontaldata,position); }Else if(Holder instanceof Gridviewholder) {holder.refreshdata (data.griddata,position); }Else if(Holder instanceof Itemviewholder) {holder.refreshdata (data.verticaldata.Get(Position-2), Position-2); }} @Override Public intGetItemCount () {return 2+data.verticalData.size (); } @Override Public intGetitemviewtype (intposition) {            if(Position = =0)returnHorizontal_view; if(Position = =1)returnGrid_view; returnVertical_view; }    }

As can be seen, we have a total of three types of entries, the first is the horizontal sliding sub-recyclerview, the second is the GridView-shaped sub-recyclerview, the third is the normal sub-entries, according to ViewType to return to different viewholder, There should be no problem here.

Next is the various types of viewholder, in holder, we want to calculate the height of the entry and then set to sub-Recyclerview

Private classGridviewholder extends Baseholder<list<integer>>{        PrivateRecyclerview Item_recyclerview; PrivateFinalintOne_line_show_number =3; PrivateList<integer>data;  PublicGridviewholder (intViewId, ViewGroup Parent,intViewType)            {Super (viewId, parent, ViewType); Item_recyclerview=(Recyclerview) Itemview.findviewbyid (R.id.item_recyclerview); } @Override Public voidRefreshData (list<integer> data,intposition)            {super.refreshdata (data, position);  This. data =data; //3 display per line, horizontal displayItem_recyclerview.setlayoutmanager (NewGridlayoutmanager (mainactivity. This, One_line_show_number,linearlayoutmanager.horizontal,false)); Viewgroup.layoutparams Layoutparams=Item_recyclerview.getlayoutparams (); //number of rows calculated            intlinenumber = Data.size ()%one_line_show_number==0? Data.size ()/one_line_show_number:data.size ()/one_line_show_number +1; //Calculate height = number of rows * height of each row + (number of rows-1) *10dp margin + 10DP (for centering)//since each row shows 3 entries, in order to keep the square, then the height should be also width/3//the height of the calculation needs to be well understood, otherwise it will create a nested Recyclerview can slide phenomenonLayoutparams.height = linenumber * (screenwidth/3) + (linenumber-1) *dip2px (Ten) + dip2px (Ten);            Item_recyclerview.setlayoutparams (Layoutparams);            Item_recyclerview.setbackgroundresource (r.color.colorprimary); Item_recyclerview.setadapter (NewGridadapter ()); }        Private classGridadapter extends Recyclerview.adapter<baseholder>{@Override PublicBaseholder Oncreateviewholder (viewgroup parent,intViewType) {                return NewItemviewholder (R.layout.item_x2_imageview,parent,viewtype); } @Override Public voidOnbindviewholder (Baseholder holder,intposition) {holder.refreshdata (data.Get(position), position); } @Override Public intGetItemCount () {returndata.size (); }        }    }

Other code I will not post, want to see the source can be directly downloaded:
Https://github.com/z593492734/nesting-recylcerview

Overall, Recyclerview nested Recyclerview is very simple, but also quite useful, I hope this demo can give you some inspiration.

Android Recyclerview nested Recyclerview

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.