Using Recyclerview in Android Studio (three)

Source: Internet
Author: User

Continue to yesterday's come, yesterday finally worked out an example, but that no click event, need to add themselves, referring to other people's example, made a relatively simple, mainly to change the part of the Myrecycleradatper.java.

Add the following interfaces:

Add a static interface.    Private Onitemclicklistener listener;    public static interface Onitemclicklistener {        void Onitemclick (view view, int position);    }    public void Setonitemclicklistener (Onitemclicklistener listener) {        This.listener = listener;    }

  

Add the following code to the Onbindviewholder

     if (listener! = null) {            Holder.v.setonclicklistener (new View.onclicklistener () {                @Override public                Void OnClick (View v) {                    listener.onitemclick (v,position);}}            );        }

  

This is good, is the more concise method that can be found at present.

It can be used directly in the main interface as follows:

        Recycleadapter.setonitemclicklistener (New Myrecycleradatper.onitemclicklistener () {            @Override            public void Onitemclick (view view, int position) {                log.e ("Constr", "" + position);            }        });

  

Watch the recorded value of LOG.E after clicking.

Custom data adapter All code is as follows, for reference, although not good, but can use, I do not know what meaning, later slowly understand it:

public class Myrecycleradatper extends recyclerview.adapter<myrecycleradatper.myviewholder> {private List<    Filelist> Mdatas;    Private Context Mcontext;    Private Layoutinflater Inflater;    Add a static interface.    Private Onitemclicklistener listener;    public static interface Onitemclicklistener {void Onitemclick (view view, int position);    } public void Setonitemclicklistener (Onitemclicklistener listener) {This.listener = listener;        } public Myrecycleradatper (context context, list<filelist> datas) {this. mcontext=context; This.        Mdatas=datas; Inflater=layoutinflater.    From (Mcontext);    } @Override public int getitemcount () {return mdatas.size (); } @Override Public Myviewholder oncreateviewholder (viewgroup parent, int viewtype) {View view = Inflater.inf        Late (r.layout.item_view,parent, false);        Myviewholder holder= New Myviewholder (view);    return holder; } @Override Publicvoid Onbindviewholder (Myviewholder holder, final int position) {filelist da=mdatas.get (position);            if (position%2==0) {//holder.tv.setbackgroundcolor (Color.Blue);        Holder.v.setbackgroundcolor (Color.gray); } if (listener! = null) {Holder.v.setonclicklistener (new View.onclicklistener () {@Overr                IDE public void OnClick (View v) {Listener.onitemclick (v,position);        }            });        } holder.tv.setText (Da.getname ());    Holder.msg.setText (da.getzt ());        } class Myviewholder extends recyclerview.viewholder{TextView TV;        TextView msg;        View v;            Public Myviewholder (view view) {super (view);            tv= (TextView) View.findviewbyid (R.ID.TXT_XS);            Msg= (TextView) View.findviewbyid (r.id.txt_msg);        V=view; }    }}

  

Using Recyclerview in Android Studio (three)

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.