Recyclerview: Creating the perfect control you're familiar with

Source: Internet
Author: User

Recyclerview control has been out for a long time, presumably a lot of people are familiar with its basic usage, and then do not repeat it. It can be a perfect alternative to the ListView and the GridView, vertical horizontal can be, waterfall flow can also be done! So many features, as long as this one control can be realized, think of all the heart. Personally, it's a little more cumbersome to use, so build a familiar and perfect control yourself.

1. Layout Manager: Layoutmanger

To use Recyclerview display content, Layoutmanger is essential, the inside of the method Setlayoutmanger is to set this thing, but for perhaps a not very familiar with the control, often there will always be missing place. In our past the ListView or the GridView, have not set this thing, so I think, can you re-inherit Recyclerview to build a ListView-like control, save unnecessary trouble.

Recyclerview has obtained its own instance when calling the method Setlayoutmanger, that is, not NULL,

So Baserecycleview inherited Recyclerview When you set the layout manager in Onfinishinflate,

Here I abstract a method Setfinallayoutmanger, can be defined according to the requirements.

public abstract void Setfinallayoutmanger ();

<span style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span> @Overrideprotected void Onfinishinflate () { Super.onfinishinflate (); Setfinallayoutmanger (); Additemdecoration (New Divideritemdecoration (GetContext ()));} </span>

2. Do not click and long press the event, do your own set Bai

(1) Define click and long-press event interface

<span style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span>//Click event Listener Interface public interface onitemclicklistener{void Onitemclick (View itemview,int position);} </span>
<span style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span><pre name= "code" class= "java" ><span style= " White-space:pre "></span>//long press event listener interface public interface Onlongitemclicklistener{void Onlongitemclick (View Itemview,int position);} </span>

(2) opening up the method of monitoring events, when you are in use when the end is to set the monitoring first or set Adpter, different people have different habits, so I did the following treatment.

<span style= "FONT-SIZE:18PX;" ><span style= "White-space:pre" ></span>public void Setadapter (Adapter Adapter) {Super.setadapter ( adapter); if (adapter instanceof baserecyleadapter) {baserecyleadapter bradapter = (baserecyleadapter) adapter;if ( Mlistener! = null) {Bradapter.setonitemclicklistener (mlistener);} if (Mlonglistener! = null) {Bradapter.setonlongitemclicklistener (Mlonglistener);}}} /** * Set Line click event */public void Setonitemclicklistener (Onitemclicklistener listener) {This.mlistener = listener;@ Suppresswarnings ("Rawtypes") baserecyleadapter adapter = cast (); Adapter.setonitemclicklistener (Mlistener);} /** * Set the president by Event */public Void Setonlongclicklistener (Onlongitemclicklistener listener) {This.mlonglistener = listener;@ Suppresswarnings ("Rawtypes") baserecyleadapter adapter = cast (); Adapter.setonlongitemclicklistener (MLongListener);} /** * Get inherited from Baserecyleadapter Adapter */@SuppressWarnings ("Rawtypes") Private Baserecyleadapter cast () {Adapter Adapter = Getadapter (); if (adapter! = NULL) {if (ADApter instanceof Baserecyleadapter) {baserecyleadapter bradapter = (baserecyleadapter) Adapter;return BrAdapter;} Else{throw new ClassCastException ("Getadapter () from Recyclerview can not cast Baserecyleadapter," + "so UserD adapter Shou LD extends Baserecyleadapter ");}} return null;} </span>
when setting the adapter, will determine whether the listener event has been set, if set, call Baserecycleadapter in the settings to listen to the event method (Baserecycleadapter is also rewritten, follow-up will be listed); When setting the listener event, it will determine if the adapter has been set, and if so, call the setting in Baserecycleadapter to listen for the event. Repeat below, the set of adapter must inherit from Baserecycleadapter.

The basic Baserecycleview has been basically completed, and the next step is to achieve the finalrecycleview that you need to reconstruct the

3. Build your own satisfaction Finalrecycleview (inherit from Baserecycleview)

(1) This Finalrecycleview is a diversified recycleview, so a custom attribute is used. Create Attrs.xml in Res/values

<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable        name= " Finalrecycleviewstyle ">        <attr name=" showstyle "format=" integer "/> <!--display form, list or grid--        <attr name= "Spancount" format= "integer"/> <!--grid Columns--    </declare-styleable>        </resources></span></span>

(2) Create Integer.xml in res/values (used to represent showstyle in Attrs)

<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"?><resources> <integer    name= "Listview_vertical" > 1000</integer>    <integer name= "listview_horizontal" >1001</integer>    <integer name= " Gridview_normal ">1002</integer>    <integer name=" Staggered_horizontal ">1003</integer>    <integer name= "staggered_vertical" >1004</integer></resources></span>
(3) attribute is obtained, it is impossible to customize the control of this is not to pour you, not to repeat.

(4) The most critical approach is, Setfinallayoutmanger, this is our abstract approach in Baserecyleview, Setlayoutmanger is set up here.

<span style= "font-size:18px;" > if (showstyle = = Res.getinteger (r.integer.listview_vertical)) {<span style= "White-space:pre" ></span> ; Setlayoutmanager (new Linearlayoutmanager (Mcontext));} else if (showstyle = = Res.getinteger (r.integer.listview_horizontal)) {Setlayoutmanager (New Linearlayoutmanager ( Mcontext, Linearlayoutmanager.horizontal, False));} else if (showstyle = = Res.getinteger (r.integer.gridview_normal)) {Setlayoutmanager (new Gridlayoutmanager (MContext, Spancount));} else if (showstyle = = Res.getinteger (r.integer.staggered_horizontal)) {Setlayoutmanager (new Staggeredgridlayoutmanager (Spancount, Staggeredgridlayoutmanager.horizontal));} else if (showstyle = = Res.getinteger (r.integer.staggered_vertical)) {Setlayoutmanager (new Staggeredgridlayoutmanager (Spancount, staggeredgridlayoutmanager.vertical));} </span> 
according to your own set of different showstyle to set up a different layout manager, very simple, do not say OK?!

4.FinalRecycleView build complete, Next is Finalviewholder, this is very simple, directly inherit the Recyclerview.viewholder code as follows:

<span style= "FONT-SIZE:18PX;" >package Com.dandy.recycleview;import Android.support.v7.widget.recyclerview;import Android.util.SparseArray; Import Android.view.view;import Android.widget.textview;public class Finalviewholder extends Recyclerview.viewholder {Private View mconvertview;private sparsearray<view> mviews;public finalviewholder (view view) {Super view; This.mconvertview = View;this.mviews = new sparsearray<view> ();}  /** * Gets the control through the ID of the control, if not, add views * @param viewId * @return */@SuppressWarnings ("unchecked") public <t extends view> T getView (int viewId) {View view = Mviews.get (ViewId), if (view = = null) {view = Mconvertview.findviewbyid (viewId); mviews. Put (viewId, view);} Return (T) view;} /** * Set string for TextView * @param viewId * @param text * @param visible * @return */public void setText (int viewId, String tex T) {TextView view = GetView (viewId); if (view! = null && text! = null) {view.settext (text);}} Public View Getcontentview () {return mconvertview;}} </spAn> 
(5) Finally, our baserecyleadapter is built, rewriting the Oncreateviewholder and Onbindviewholder methods, and

The Onbindviewholder method sets the listener event, which is relatively simple and the code is as follows:

<span style= "FONT-SIZE:18PX;" >package Com.dandy.recycleview;import Java.util.list;import Com.dandy.recycleview.baserecycleview.onitemclicklistener;import Com.dandy.recycleview.baserecycleview.onlongitemclicklistener;import Android.content.context;import Android.support.v7.widget.recyclerview.adapter;import Android.view.layoutinflater;import Android.view.View; Import Android.view.view.onlongclicklistener;import Android.view.viewgroup;import Android.view.view.onclicklistener;public abstract class Baserecyleadapter <T> extends adapter< Finalviewholder>{public Onitemclicklistener mlistener;public Onlongitemclicklistener mLongListener;public Context mcontext;public list<t> mdatas;public int itemlayoutid;public baserecyleadapter (context Context,List <T> datas,int itemlayoutid) {this.mcontext = Context;this.mdatas = Datas;this.itemlayoutid = itemLayoutId;} @Overridepublic int GetItemCount () {if (Mdatas! = null) {return mdatas.size ();} return 0;} @Overridepublic FinalviewHolder Oncreateviewholder (viewgroup viewgroup, int position) {View view = Layoutinflater.from (Mcontext). Inflate ( Itemlayoutid,viewgroup,false); return new Finalviewholder (view);} @Overridepublic void Onbindviewholder (Finalviewholder viewholder, final int position) {Viewholder.getcontentview (). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {if (Mlistener! = null) { Mlistener.onitemclick (v, Position);}}); Viewholder.getcontentview (). Setonlongclicklistener (New Onlongclicklistener () {@Overridepublic Boolean Onlongclick ( View v) {if (Mlonglistener! = null) {Mlonglistener.onlongitemclick (V, position); return true;} return false;}}); Convertbindviewholder (viewholder,position);} public abstract void Convertbindviewholder (Finalviewholder viewholder, final int position);p ublic void Setonitemclicklistener (Onitemclicklistener listener) {This.mlistener = listener;} public void Setonlongitemclicklistener (Onlongitemclicklistener listener) {This.mlonglistener = Listener;}} </span> 
(6) Self-satisfied Recyleview build completed, how the effect, do not explain


Source code Download link


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

Recyclerview: Creating the perfect control you're familiar with

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.