Recyclerview to achieve Waterfall Flow effect (ii)

Source: Internet
Author: User

In the previous article we know that the default in Recyclerview provides us with three layout managers, namely Linearlayoutmanager, Gridlayoutmanager, Staggeredgridlayoutmanager. The Staggeredgridlayoutmanager can realize staggered grid layout, which can be used to realize waterfall flow.

Let's take a look at the following, then paste the code:



Most of the content is implemented in fact and the previous article is the same, not much of the narrative, on a different place, that is, we are bound in the adapter Viewholder method needs to re-set the Itemview layout height, I am here to generate a random number to set the altitude, The code is implemented as:

private void Getrandomheight (List<string> lists) {//Gets the height of the random item        Heights = new arraylist<> ();        for (int i = 0; i < lists.size (); i++) {            heights.add ((int) (200+math.random () *400));        }    }
In the Onbindviewholder method:

<span style= "FONT-SIZE:14PX;" > viewgroup.layoutparams params =  holder.itemView.getLayoutParams ();//Get item layoutparams layout parameters Params.height = Heights.get (position);//Assign a random height to the Itemview layout holder.itemView.setLayoutParams (params);//Set the params to Itemview layout </ Span>
To achieve the waterfall flow effect.

Put the code on it:

Mainactivity.java

public class Mainactivity extends Appcompatactivity {private Recyclerview mrecyclerview;    Private list<string> lists;    Private Myrecycleradapter adapter;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        InitData ();        Mrecyclerview = (Recyclerview) This.findviewbyid (R.id.recyclerview); Mrecyclerview.setitemanimator (New Defaultitemanimator ());//Mrecyclerview.additemdecoration ();//Set Split line MRecyc Lerview.setlayoutmanager (New Staggeredgridlayoutmanager (2, staggeredgridlayoutmanager.vertical));//        Set Recyclerview layout Manager to vertically arrange for 2 columns adapter = new Myrecycleradapter (this,lists);        Mrecyclerview.setadapter (adapter); Adapter.setonclicklistener (New Myrecycleradapter.onitemclicklistener () {@Override public void ITEMC Licklistener (view view, int postion) {toast.maketext (Mainactivity.this, "clicked:" +postion, Toast.length_short). Show ();                } @Override public void Itemlongclicklistener (view view, int postion) {//Long press DELETE                Lists.remove (postion);            Adapter.notifyitemremoved (postion);    }        });        } private void InitData () {lists = new ArrayList ();        for (int i = 0; i < i++) {Lists.add ("" + i); }    }}

Myrecycleradapter.java

public class Myrecycleradapter extends recyclerview.adapter<myviewholder> {private list<string> lists;    Private context context;    Private List<integer> Heights;    Private Onitemclicklistener Mlistener;        Public Myrecycleradapter (Context context,list<string> lists) {This.context = context;        this.lists = lists;    Getrandomheight (this.lists);        } private void Getrandomheight (List<string> lists) {//Get the height of the random Item heights = new Arraylist<> ();        for (int i = 0; i < lists.size (); i++) {heights.add ((int) (200+math.random () *400));        }} public interface onitemclicklistener{void Itemclicklistener (View view,int postion);    void Itemlongclicklistener (View view,int postion);    } public void Setonclicklistener (Onitemclicklistener listener) {This.mlistener = listener; } @Override Public Myviewholder oncreateviewholder (viewgroup parent, int viewtype) {View view = Layoutinflater.from (context). Inflate (R.layout.item,parent,false);        Myviewholder Viewholder = new Myviewholder (view);    return viewholder; } @Override public void Onbindviewholder (final myviewholder holder, int position) {Viewgroup.layoutparams PA Rams = Holder.itemView.getLayoutParams ();//Get Item layoutparams layout parameter params.height = heights.get (position);//Assign a random height To the item layout holder.itemView.setLayoutParams (params);//The params is set to the item layout Holder.mTv.setText (Lists.get (position)); /bind data to the control if (mlistener!=null) {//If the listener is set, then it is not empty and then callback the corresponding method Holder.itemView.setOnClickListener (new View.oncl Icklistener () {@Override public void OnClick (View v) {int pos = Holder.getl                Ayoutposition ();//Get the current click on the item's position pos Mlistener.itemclicklistener (Holder.itemview,pos);//Give the event to the interface we implement to handle it.            }            });   Holder.itemView.setOnLongClickListener (New View.onlongclicklistener () {             @Override public boolean Onlongclick (View v) {int pos = HOLDER.GETLAYOUTPO                    Sition ();//Get the current click on the item's position pos Mlistener.itemlongclicklistener (Holder.itemview,pos);//Give the event to the interface we implement to handle it.                return true;        }            });    }} @Override public int getitemcount () {return lists.size ();    }}class Myviewholder extends recyclerview.viewholder{TextView mTv;        Public Myviewholder (View Itemview) {super (Itemview);    MTv = (TextView) Itemview.findviewbyid (R.id.textview); }}

Source: http://download.csdn.net/detail/u010687392/8868745


Reprint please specify the source-http://blog.csdn.net/u010687392

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Recyclerview to achieve Waterfall Flow effect (ii)

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.