The Recyclerview of Metail design

Source: Internet
Author: User

1.RecyclerView Simple setup:

1 Private voidInitrecyclerview () {2Mlist =NewArraylist<>();3          for(inti = 0; I < 60; i++) {4Mlist.add (String.Format (getString (R.string.lesson), (1+i) + ""));5         }6 Setlayoutmanget (switchid);7Madapter =NewMainrecycleradapter (mlist);8 Mry.setadapter (madapter);9Mry.setitemanimator (Newdefaultitemanimator ());TenMadapter.setitemclicklistener (NewMainrecycleradapter.itemclicklistener () { One @Override A              Public voidClick (View View,intposition, String text) { -Toast.maketext (mainactivity. This, "click--" +text, Toast.length_short). Show (); - goactivity (position); the             } -         }); -}

The RV is highly decoupled and can be set up for Layoutmanger,itemanimotor,adaper,deliver split lines.

2. Set the entry to add, can be removed when the local refresh, as far as possible to optimize performance:

3. To use the Staggeredgridlayoutmanager

1). This simulates assigning a height to each item dynamically, remember to use

View View =layoutinflater.from (Parent.getcontext ()). Inflate (Android. R.layout.simple_list_item_1,parent,false);this way after looking at the source code found no artificial to add to the parent class of ViewGroup, but to Recyclerview to deal with their own

1 @Override2      PublicMainviewhold Oncreateviewholder (viewgroup parent,intViewType) {3 //View view = View.inflate (Parent.getcontext (), Android. R.layout.simple_list_item_1, null);//not added into parent4 //View view = View.inflate (Parent.getcontext (), Android. R.layout.simple_list_item_1, parent);//Dual Parent5View View =layoutinflater.from (Parent.getcontext ()). Inflate (Android. R.layout.simple_list_item_1,parent,false);6         return Newmainviewhold (view);7}

2). There is a problem with the above comment two ways, the first getlayoutparams parameter is empty, the second resoure (Android. R.layout.simple_list_item_1) has two parent, because the view source Discovery Root.add (view,params) added once, Recyclerview himself will add the item into it.

3). The above two methods are ultimately called this method, the corresponding or the third method

4). Diagram of the previous waterfall flow:

3.RecyclerView Set Entry Click event:

method One: Use Holder.getadaperposition () to obtain the position information of the current entry, which can get position information in real time, including AddItem and remove entries, preferably in the case of data set changes.

  mode Two: The use of custom Onclicklistener, the construction of the method when the position in, so position in a separate open stack, can be stored for a long time, preferably for the data set does not change the situation (after testing, Postion will not change in real time after AddItem and RemoveItem)

Listener:

Apply to Recyclerview:

4.RecycleView Split Line:

1). Getitemoffsets in the first recyclerview.itemdecoration (Rect outrect, view view, Recyclerview parent, Recyclerview.state State)

it is performed before the OnDraw () method, calling this method to first get the interval width between the entries, the Rect rectangle area. Gets the offset of the entry, and all entries are called once.2) The OnDraw method is the method to actually draw the divider here, let's start with a simple, linearlayoutmanager dividing line .First, you can take the system divider (convenient), the construction method into the layout direction

We can give divider a set method to dynamically set

Set the offset for all item:

Divider Drawing Position: (according to the above)

OnDraw () Real drawing:

Calculate margin:

Here is an example of drawing a vertical method, that is, a long rectangle, as an example:

Here is a very important point to note:

The Parent.getchildcount () method is to get the number of Childview on the screen one screen at a moment:

(1). When drawing here, the child's param is to be used Recyclerview.layoutparam, because it inherits and Maraglayout can get margin margin

(2). Child if there is a panning animation the distance to add animation here: Math.Round (Viewcompat.gettranslationy (Child));(3). Remember to start and end with C.save () and C.restore () to save the artboard(4). Here is Child.getbottom)---item5, for example, the value that is obtained on the screen refers to where:

Drawhorazotion () is the same principle, and then tell us how our custom itemdecoration is applied to Recyclerview:

Here's how to draw a GridView layout split line:1). About offsets here are the last and last columns to consider:

2). Here's how to judge the last column of the last line:

3). Judge the last column:

5.RecyclerView Add header and footer layouts like ListView:

1). By looking at the ListView add the source of the tail, which used the proxy mode, add the tail and the time to re-new a wrapheaderlistviewadapter, with two list<view> To store the added head and tail, getview,getcount and other methods or to use the original adapter, or to decorate or to the sub-class to achieve, so we also to imitate this:

define yourself a wrapheadrecycleradapter first:

2). Imitate the ListView to rewrite several important methods:here, the number is added to the head and tail .

3). Use Getitemviewtype to distinguish what type of current entry is (header, tail, normal entry)

Remember here the Madapter.getitemviewtype (adjposition) returned is the original normal item, the location is also starting from 04). Oncreateviewholder () method in the end-to-end position new out of the hold as long as it is used in conjunction with the framework, in fact, and no use, the purpose of viewhold is mainly to reuse, and the tail is directly addview added in, There is no large number of re-use cases

5). Onbindviewholder

6). Use or use the same as usual RV, Wrapheadrecyclerview let us secretly do

:

6.RecyclerView Add drag and slide:

1). Add drag and slide to use a itemtouchhelper, as well as Itemtouchhelper.callback:

1 getmovementflags () method

OnMove () method

Onswiped () method

Islongpressdragenabled () Method:

Onselectedchanged () Method:

ClearView () Method:

Onchilddraw () Method:

2). Two different classes of communication, using the interface interface to do the bridge, very good to reduce the coupling:

 PublicQqadapter (list<qqmessage>list, Startdraglistener Startdraglistener) {         This. List =list; Mstartdraglistener=Startdraglistener; }holder.iv_logo.setontouchlistener (NewView.ontouchlistener () {@Override Public BooleanOnTouch (View V, motionevent event) {if(event.getaction () = =Motionevent.action_down)                {Mstartdraglistener.onstartdrag (holder); }                return false; }        });

For example: here Qqadapter and outside communication, through the Startdraglistener interface, the icon Iv_logo Press move, let Itemtouchhelper call StartDrag () method

/***/Publicinterface  startdraglistener    {void  Onstartdrag (Recyclerview.viewholder viewholder);}

This is achieved in the activity:

3). Itemtouchhelper.callback need to put his drag and slip back to Qqadapter, to change the data set changes and view changes, here communication is only callback and Qqadapter, So can be transferred back and forth through the Itemtouchmovelistener interface, Qqadapter to implement the interface, respectively, data updates and attempts to change:in Mycallback:

In Qqadaper:

are notify methods that are called

As follows:

Attention:

:

The Recyclerview of Metail design

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.