Use Recylerview to complete drag sorting high imitation QQ sideslip Delete function _android

Source: Internet
Author: User

Recently used to recylerview complete drag sort, sideslip delete, record here.

Need to use: Itemtouchhelper.callback this class.

Effect Chart:

In Recylerview activity, there are other things to do besides setting up adapter:

Myitemtouchhelpercallback callback = new Myitemtouchhelpercallback (adapter); This class inherits Itemtouchhelper.callback, which is a helper class that helps handle Recylerview drag sideslip 
itemtouchhelper = new Itemtouchhelper ( callback); Instantiate a Itemtouchhelper object with the callback instantiated above. Routine code, nothing to say, fixed procedure 
helper.attachtorecyclerview (Recyclerview);//Let help handle recylerview sliding, sideslip operation

The operation in the activity is so simple.

Next, we define an interface, which is for decoupling, because I see some articles that deal with OnMove and onswipe in the activity, and are abstracted to better understand.

Public interface Onmoveandswipelistener {public 
boolean onitemmove (int fromposition, int toposition); Note Return Boolean oh public 
void Onitemdismiss (int position); 
}

Implement this interface in the adapter of Recylerview,

To duplicate the two methods above,

@Override Public 
boolean onitemmove (int fromposition, int toposition) { 
if (Fromposition < toposition) { 
When moving down, do data exchange for 
(int i = Fromposition i < toposition; i++) { 
collections.swap (list,i,i+1); 
} 
} else{ 
//Move up, do data exchange for 
(int i = fromposition; I >toposition; i--) { 
collections.swap (list,i,i-1); 
} 
} 
Notifyitemmoved (fromposition,toposition); 
return true; 
} 
@Override public 
void Onitemdismiss (int position) { 
//This example does not operate on sideslip, and if necessary imitate the Onitemmove method, delete the corresponding position data, and refresh the interface is OK 
}

This adapter work is done, as is also very simple, there are fixed routines, attention to see the demo are there is a default animation, this animation effect is you set the Recylerview, Recylerview.setitemanimator (parameter); set in, if you do not call this method, it is the default animation effect.

In addition to setting up adapter things to do are:

Writing a class inherits Itemtouchhelper.callback, which is the last step, and is the class that this article first instantiated in the activity,

The code is not too much:

public class Myitemtouchhelpercallback extends Itemtouchhelper.callback {myadapter adapter; 
Public Myitemtouchhelpercallback (Myadapter adapter) {This.adapter = adapter; @Override public int Getmovementflags (Recyclerview recyclerview, Recyclerview.viewholder viewholder) {//Replication This method is used to set what I need What actions//I want to handle up and down drag, set up down, if the GridView type of recylerview,up,down,left,right can handle the final int movemode = Itemtouchhelper.up | 
Itemtouchhelper.down; This example does not handle sideslip, and if you need to handle sideslip, remove the annotation//final int swipemode = Itemtouchhelper.start | 
Itemtouchhelper.end; 
LOG.E ("Getmovementflags", "getmovementflags"); Return Makemovementflags (movemode, 0);//Do not handle anything, pass 0} @Override public boolean onmove (Recyclerview Recyclerview, Recycler 
View.viewholder Viewholder, Recyclerview.viewholder target) {//When you drag it triggers this callback, and then invokes the Onitemmove method of replication in adapter to Exchange data 
Return Adapter.onitemmove (Viewholder.getadapterposition (), target.getadapterposition ()); @Override public void onswiped (Recyclerview.viewholder viewholder, int direction) {///If you want to deal with sideslip, uncomment the logic as above OnMove//Adapter.onitemdismiss (Viewholder.getadapterposition ()); @Override public void onselectedchanged (Recyclerview.viewholder viewholder, int actionstate) {//This method fires when you select an item drag, the 
You can set a background color for the selected item here, highlighting that you have selected the background color super.onselectedchanged (Viewholder, actionstate); 
if (actionstate!= itemtouchhelper.action_state_idle) {viewHolder.itemView.setBackgroundColor (Color.ltgray); } @Override public void Clearview (Recyclerview recyclerview, Recyclerview.viewholder viewholder) {//Because you set the background color so you don't 
When selected, cancel the background color. 
Super.clearview (Recyclerview, Viewholder); 
ViewHolder.itemView.setBackgroundColor (0); } 
}

So far, a simple drag and sideslip is done, in fact, drag and sideslip logic are the same, according to the above steps, the effect came out.

The above is just a simple effect. To achieve the above effect, and then see the effect of a more advanced point:

Recycleviewswipedismiss

To do this, start by saying the above logic:

First, his layout for each item is two tiers:

<?xml version= "1.0" encoding= "Utf-8"?> <!--background view, used to prompt user action results--> <relativelayout xmlns:android= "http ://schemas.android.com/apk/res/android "xmlns:tools=" Http://schemas.android.com/tools "android:id=" @+id/rl_ Background "android:layout_width=" Match_parent "android:layout_height=" wrap_content "> <imageview android:id= "@+id/iv_done" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_ Centervertical= "true" android:layout_marginleft= "16DP" android:layout_marginstart= "16DP" android:src= "@mipmap/bt_ Ic_done_white_24 "/> <imageview android:id= @+id/iv_schedule" android:layout_width= "Wrap_content" Android: layout_height= "Wrap_content" android:layout_alignparentright= "true" Android:layout_centervertical= "true" Android: Layout_marginend= "16DP" android:layout_marginright= "16DP" android:src= "@mipmap/bt_ic_schedule_white_24"/> < !--sliding operation of view--> <linearlayout android:id= "@+id/ll_item" android:layout_width= "Match_parent" android:layout_height= "64DP" android:background= "@android: Color/white" android:gravity= "Center_ 
Vertical "android:orientation=" vertical "android:paddingleft=" 16DP "android:paddingright=" 16DP "> <textview Android:id= "@+id/tv_item" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: Textappearance= "Android:attr/textappearancemedium" tools:text= "Item"/> </LinearLayout> <view android: Layout_width= "Match_parent" android:layout_height= "1DP" android:layout_below= "@id/ll_item" android:background= "# B2b2b2 "/> </RelativeLayout>

Principle: Full Screen linearlayout is responsible for the sideslip effect, and the root layout is the color of the picture of that layer, because the linearlayout was slipped away, so the root layout and the picture on the above came out.

Several methods need to be myitemtouchhelpercallback in the process of replication, and several methods of multiple replication:

<span style= "White-space:pre" > </span> @Override public void Clearview (Recyclerview Recyclerview, Recyclerview.viewholder viewholder) {//Release view callback, clear background color, hide icon//default is Operation Viewholder Itemview, This calls the Itemtouchuiutil Clearview method to pass in the specified view Getdefaultuiutil (). Clearview ((Sampleadapter.itemviewholder) ViewHolder 
). Vitem); 
((Sampleadapter.itemviewholder) viewholder). Vbackground.setbackgroundcolor (color.transparent); 
((Sampleadapter.itemviewholder) viewholder). Ivschedule.setvisibility (View.gone); 
((Sampleadapter.itemviewholder) viewholder). Ivdone.setvisibility (View.gone); @Override public void onselectedchanged (Recyclerview.viewholder viewholder, int actionstate) {//when the Viewholder slide or drag state The callback if (Viewholder!= null) {//default is the Itemview of the Operation Viewholder, where the Itemtouchuiutil method is invoked to pass in the specified view Clearview ( 
). Onselected (((Sampleadapter.itemviewholder) viewholder). Vitem); @Override public void Onchilddraw (Canvas C, Recyclerview Recyclerview, Recyclerview.viewholder viewHolder, float DX, float dY, int actionstate, Boolean iscurrentlyactive) {//Itemtouchhelper OnDraw method invokes this method, you can use the Canvas object to enter The line is drawn, the pattern drawn is below the Recyclerview//default is the Itemview of the Operation Viewholder, where the Itemtouchuiutil Clearview method is invoked to pass in the specified view Getdefaultuiutil ( 
). OnDraw (c, Recyclerview, (Sampleadapter.itemviewholder) viewholder). Vitem, DX, DY, Actionstate, iscurrentlyactive); if (DX > 0) {//left sliding is prompt ((sampleadapter.itemviewholder) viewholder). Vbackground.setbackgroundresource ( 
R.color.colordone); 
((Sampleadapter.itemviewholder) viewholder). Ivdone.setvisibility (view.visible); 
((Sampleadapter.itemviewholder) viewholder). Ivschedule.setvisibility (View.gone); } if (DX < 0) {//Slide right when prompted (Sampleadapter.itemviewholder) viewholder). Vbackground.setbackgroundresource (r.color.co 
Lorschedule); 
((Sampleadapter.itemviewholder) viewholder). Ivschedule.setvisibility (view.visible); 
((Sampleadapter.itemviewholder) viewholder). Ivdone.setvisibility (View.gone); @Override public void Onchilddrawover (Canvas c, Recyclerview Recyclerview, Recyclerview.viewholder viewholder, float DX, float dY, int actionstate, Boolean Iscurrentlya ctive) {//Itemtouchhelper's Ondrawover method invokes the method, which can be drawn using the canvas object, and the pattern drawn above the Recyclerview//default is the Viewholder of the Operation Itemview. This calls the Itemtouchuiutil Clearview method to pass in the specified view Getdefaultuiutil (). Ondrawover (c, Recyclerview, ( 
Sampleadapter.itemviewholder) viewholder). Vitem, DX, DY, Actionstate, iscurrentlyactive); } 
});

The

Use the Getdefaultuiutil () method to specify that the sliding operation is linearlayout, so that only linearlayout is slipping each time. All the other operations are the same.

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.