Android Recyclerview (ii)

Source: Internet
Author: User

The previous article on the use of Recyclerview has made a brief introduction, this article will continue to introduce. Recyclerview adapter is different from the ListView adapter, in addition to providing the Notifydatasetchanged method, but also provides more data update methods, combined with itemanimator, can play a good effect. The methods provided in adapter are as follows:

Following the above case based on the continuation, the final results such as:

The main code is as follows:

1. Add default animation for Recyclerview

Recyclerview.setitemanimator (new defaultitemanimator ());

2, implementation of the deletion method:

 Public void delitem (view view) {            Mdata.remove (2);            Adapter.notifyitemremoved (2);}

3. Implement the New method:

 Public void AddItem (view view) {    Mdata.add (2, "This is the new item");    Adapter.notifyiteminserted (2);}

By doing this, you can delete and add an item to a location with the default animation effect.

In addition to the above implementations, we use more of the effect of sliding disappear and drag and drop, in Recyclerview, can also be implemented and use the official API. Before we do this, let's familiarize ourselves with the relevant APIs.

Itemtouchhelper, this class is a tool class that implements Recyclerview sliding vanishing and drag-and-drop, and provides a callback interface. In this interface, there are the following methods:

Getmovementflags (Recyclerview Recyclerview, Recyclerview.viewholder viewholder): Used to return drag and drop, slide direction and state, etc. You need to call Makemovementflags (int dragflags, int swipeflags) to generate the return value.
OnMove (Recyclerview Recyclerview, Recyclerview.viewholder Viewholder, Recyclerview.viewholder target): The method is called when the item is dragged , the first parameter represents the dragged item, and the second parameter represents the target item.
Direction): This method is called when the item is slid, and the second parameter represents the direction of the slide.
Isitemviewswipeenabled (): Supports sliding return true on behalf of support.
Islongpressdragenabled (): Supports dragging, returns true for support.

By implementing the above method, we can complete the simple slide vanishing and drag and drop effect, the core code is as follows:

  Definition of Itemtouchhelper:

Itemtouchhelper =NewItemtouchhelper (NewItemtouchhelper.callback () {@Override Public intgetmovementflags (Recyclerview recyclerview, Recyclerview.viewholder viewholder) {//The direction of support when sliding        intDragflags = Itemtouchhelper.up |Itemtouchhelper.down; //The direction of support when dragging        intSwipeflags = Itemtouchhelper.start |Itemtouchhelper.end; //This method must be called to tell Itemtouchhelper which flags are supported        returnmakemovementflags (Dragflags, swipeflags); } @Override Public BooleanOnMove (Recyclerview recyclerview, Recyclerview.viewholder Viewholder, Recyclerview.viewholder target) {Adapte        R.move (Viewholder.getadapterposition (), target.getadapterposition ()); return true; } @Override Public voidonswiped (Recyclerview.viewholder Viewholder,intdirection)    {Adapter.delete (viewholder.getadapterposition ()); } @Override Public Booleanisitemviewswipeenabled () {return true; } @Override Public Booleanislongpressdragenabled () {return true; }});

Define the Delete and Move methods separately in adapter, as follows:

 Public void Delete (int  position) {    mdata.remove (position);    notifyitemremoved (position);}  Public void Move (int from,int. to ) {    = mdata.remove (from);     > from? To-1 : To, prev);    Notifyitemmoved (from, to);}

The final implementation effect is as follows:

Jerry Education
Source:http://www.cnblogs.com/jerehedu/
Copyright Notice: The copyright of this article belongs to cigarettes DeskJerry EducationSection Technology Co., Ltd. and blog Park are shared, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Technical Consultation:

Android Recyclerview (ii)

Related Article

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.