Implementation of the Recyclerview universal adapter

Source: Internet
Author: User

    • We need to maintain a reference to a collection of data within the Recyclerviewadapter
    • Need to implement item's click events, including short press and long press, and need to provide a way to set up listeners externally
    • This adapter must be generic, so it should be generic, and viewholder is also generic and can use Sparsearray to maintain all the collections of view
    • Binding of data (abstract method, deferred to subclass implementation) is required within recyclerviewadapter, and listener bindings
Writing listeners

Divided into short press events and long press events

    • Short-Press Listener
package;import android.view.View;/** * Created by lizhangqu on 2015/6/3. */public interface OnItemClickListener<T> {    void onClick(View view, T item);}
    • Long-Press Listener
package;import android.view.View;/** * Created by lizhangqu on 2015/6/3. */public interface OnItemLongClickListener<T> {    void onLongClick(View view, T item);}
Writing a Universal adapter
    • Setting up listeners;;ImportAndroid.util.SparseArray;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.ImageView;ImportAndroid.widget.TextView;ImportJava.util.List;;;/** * This was a baseadapter for Recyclerview * Author Lizhangqu * Version 1.0 * Date 2015/6/3. */PublicAbstract  class baserecyclerviewadapter<T, VH extends  Baserecyclerviewadapter. Sparsearrayviewholder> extends recyclerview. Adapter<VH> {    /** * Click Listener * /    protectedOnitemclicklistener Monitemclicklistener;/** * Long Click Listener */    protectedOnitemlongclicklistener Monitemlongclicklistener;/** * Set a LONG click Listener * * @param onitemlongclicklistener */public void Setonitemlongclicklistener (Onitemlongclicklistener onitemlongclicklistener) {Monitemlongclicklistener    = Onitemlongclicklistener; }/** * Set a click Listener * * @param onitemclicklistener */public void Setonitemclicklistener (Onitemclicklistener onitemclicklistener) {Monitemclicklistener = OnItemClickLis    Tener; }}
    • Write a constructor method to assign a data collection
 /** * data */ protected  list<t> mlist;     /** * @param  list the datas to attach the adapter */ public  baserecyclerviewadapter  (List    <T> list) {mlist = list; }
    • Implements several necessary methods, such as getting item and overwrite GetItemCount method

       /** * get a item by index * * @param  position * @return  */ protected  T  GetItem  (int  position) {return  Mlist.get (position);}  @Override  public  int  getitemcount 
       () {return  mlist.size ();}  
    • Implementing a method for mapping layout files to view
   /** * Inflate a view by ViewGroup, ID, etc * * @param viewgroup * @param LayoutID * @return * *    protectedViewInflateitemview(ViewGroup ViewGroup,intLayoutID) {returnInflateitemview (ViewGroup, LayoutID,false); }/** * Inflate a view by ViewGroup, ID, etc * * @param viewgroup * @param LayoutID * @param Attach * @return * *    protectedViewInflateitemview(ViewGroup ViewGroup,intLayoutID,BooleanAttach) {returnLayoutinflater.from (Viewgroup.getcontext ()). Inflate (LayoutID, viewgroup, attach); }
    • Implementation of the Onbindviewholder method, this step is very important, the specific implementation of the extraction into two methods to implement, Binddatatoitemview is an abstract method, deferred to the subclass implementation, Binditemviewclicklistener Method Setup Listener
  /** * A final function to avoid your override * Use template design pattern * * @param VH * @param Position * *    @Override     Public Final void Onbindviewholder(VH VH,intPosition) {FinalT item = getItem (position);        Binddatatoitemview (VH, item);    Binditemviewclicklistener (VH, item); }/** * Bind data to Itemview * * @param VH viewholder * @param Item Item */    protected Abstract void Binddatatoitemview(VH VH, T Item);/** * Bind click Listner to Itemview * * @param VH viewholder * @param Item Item */    protected Final void Binditemviewclicklistener(VH VH,FinalT Item) {if(Monitemclicklistener! =NULL) {Vh.itemView.setOnClickListener (NewView.onclicklistener () {@Override                 Public void OnClick(View view)                {Monitemclicklistener.onclick (view, item);        }            }); }if(Monitemlongclicklistener! =NULL) {Vh.itemView.setOnLongClickListener (NewView.onlongclicklistener () {@Override                 Public Boolean Onlongclick(View v) {Monitemlongclicklistener.onlongclick (V, item);return true;        }            }); }    }
    • Implements universal Viewholder, internally using Sparsearray to maintain view objects
     Public Static  class sparsearrayviewholder extends recyclerview. Viewholder {        Private Finalsparsearray<view> views; Public Sparsearrayviewholder(View Itemview) {Super(Itemview); views =NewSparsearray<view> (); } Public<t extends View> TGetView(intID) {View view = Views.get (ID);if(View = =NULL{view = Itemview.findviewbyid (ID);            Views.put (ID, view); }return(T) view; }    }

A generic GetView method is implemented internally to cache the view into Sparsearray

    • Implement some property setting methods of general Viewholder, use chained call, return this object
         PublicSparsearrayviewholderSetText(intViewId, Stringvalue{TextView view = GetView (viewId); View.settext (value);returnSparsearrayviewholder. This; } PublicSparsearrayviewholderSetTextColor(intViewId,intTextColor) {TextView view = GetView (viewId); View.settextcolor (TextColor);returnSparsearrayviewholder. This; } PublicSparsearrayviewholderSetimageresource(intViewId,intIMAGERESID) {ImageView view = GetView (viewId); View.setimageresource (IMAGERESID);returnSparsearrayviewholder. This; } PublicSparsearrayviewholderSetBackgroundColor(intViewId,intColor) {View view = GetView (viewId); View.setbackgroundcolor (color);returnSparsearrayviewholder. This; } PublicSparsearrayviewholderSetbackgroundresource(intViewId,intBackgroundres) {View view = GetView (viewId); View.setbackgroundresource (Backgroundres);returnSparsearrayviewholder. This; } PublicSparsearrayviewholdersetvisible(intViewId, Boolean visible) {View view = GetView (viewId); View.setvisibility (visible?) View.VISIBLE:View.GONE);returnSparsearrayviewholder. This; } PublicSparsearrayviewholderSetonclicklistener(intViewId, View.onclicklistener Listener) {View view = GetView (viewId); View.setonclicklistener (listener);returnSparsearrayviewholder. This; } PublicSparsearrayviewholderSetontouchlistener(intViewId, View.ontouchlistener Listener) {View view = GetView (viewId); View.setontouchlistener (listener);returnSparsearrayviewholder. This; } PublicSparsearrayviewholderSetonlongclicklistener(intViewId, View.onlongclicklistener Listener) {View view = GetView (viewId); View.setonlongclicklistener (listener);returnSparsearrayviewholder. This; } PublicSparsearrayviewholderSettag(intViewId, Object tag) {View view = GetView (viewId); View.settag (tag);returnSparsearrayviewholder. This; }
    • See GitHub Address: Https://

Implementation of the Recyclerview universal adapter

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: 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.