Multiple implementations of the Android swipe delete (i)

Source: Internet
Author: User

Personal habits, first

Colleague is a sister (this is very important), write sliding delete kinetic energy when used swipelayout, and then sad is that the sliding time is intercepted, the solution is not mentioned first, in (a) first explained Swipelayout download ListView and achieve sliding deletion effect, Of course, there are many ways to load a ListView, which will be discussed later,

First you need to understand Viewdraghelper,viewdraghelper override the touch event parameter so that you can drag to change the location of the sub-view, specific charm see:

Need to see the source of the link Http://www.tuicool.com/articles/JZbEvya

2013 Google I/O conference introduced two new layout:slidingpanelayout and drawerlayout are used to viewdraghelper processing drag events, good words not too much capacity limited energy not enough not to study too deep, will use on OK, See sliding Delete example code for details:

"" Created Swipelayout

Package com.one.swipe;

Import Android.content.Context;
Import Android.graphics.Rect;
Import Android.support.v4.view.ViewCompat;
Import Android.support.v4.widget.ViewDragHelper;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.widget.FrameLayout;

/**
* Equivalent to inheriting ViewGroup
*/
public class Swipelayout extends Framelayout {

/**
* Use the enumeration of three states, code mode
*/
public static enum Status {
Close, Open, Swiping
}

public interface onswipelistener{
void OnClose (swipelayout layout);
void OnOpen (swipelayout layout);

void Onstartopen (swipelayout layout);
void Onstartclose (swipelayout layout);
}

Private Status status = Status.close;
Private Onswipelistener Onswipelistener;

Public Status GetStatus () {
return status;
}

public void SetStatus (status status) {
This.status = status;
}

Public Onswipelistener Getonswipelistener () {
return onswipelistener;
}

public void Setonswipelistener (Onswipelistener onswipelistener) {
This.onswipelistener = Onswipelistener;
}

--------------------------above and our usual settings adapter almost bound listening and layout settings parameters
Private Viewdraghelper Mhelper;

Public Swipelayout (Context context) {
This (context, NULL);
}

Public Swipelayout (context context, AttributeSet Attrs) {
This (context, attrs, 0);
}

Public Swipelayout (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);

1. Create viewdraghelper second parameter default value
Mhelper = Viewdraghelper.create (this,1.0f, callback);
}

3. Methods for overriding a series of callbacks
Viewdraghelper.callback Callback = new Viewdraghelper.callback () {

@Override
public boolean Trycaptureview (View child, int pointerid) {
return true;
}

/**
* Processing limit range for horizontal movement
*/
public int Getviewhorizontaldragrange (View child) {
return mrange;
};
/**
* Vertical direction of processing
*/
public int Clampviewpositionhorizontal (View child, int left, int dx) {
The value returned determines where it will be moved.
if (child = = Mfrontview) {
if (left <-Mrange) {
Limit left Range
Return-mrange;
}else if (Left > 0) {
Limit right Range
return 0;
}
}else if (child = = Mbackview) {
if (left < Mwidth-mrange) {
Limit left Range
return mwidth-mrange;
}else if (Left > Mwidth) {
Limit right Range
return mwidth;
}
}
return left;
};

When the position changes, pass the horizontal offset to the other layout
public void onviewpositionchanged (View Changedview, int. left, int top, int dx, int dy) {
if (Changedview = = Mfrontview) {
Drag the pre-layout, passing the offset dx just occurred to the post-layout
Mbackview.offsetleftandright (DX);
} else if (Changedview = = Mbackview) {
Drag-and-drop is a post-layout that passes the offset DX just occurred to the front layout
Mfrontview.offsetleftandright (DX);
}

Dispatchdragevent ();

Invalidate ();
};
/**
* Released SVN seems to have this setting, rollback
*/
public void onviewreleased (View releasedchild, float xvel, float yvel) {
You'll be called when you let go
Xvel Right +, left-
if (Xvel = = 0 && mfrontview.getleft () <-Mrange * 0.5f) {
Open ();
}else if (Xvel < 0) {
Open ();
}else {
Close ();
}
};
};
Private View Mbackview;
Private View Mfrontview;
private int mrange;
private int mwidth;
private int mheight;



/**
* Update the current status
*/
protected void Dispatchdragevent () {


Status Laststatus = status;
Get the latest status
Status = UpdateStatus ();

When the state changes, invoke the method in the listener
if (laststatus! = Status && Onswipelistener! = null) {
if (status = = Status.open) {
Onswipelistener.onopen (this);
}else if (status = = Status.close) {
Onswipelistener.onclose (this);
}else if (status = = status.swiping) {
if (Laststatus = = Status.close) {
Onswipelistener.onstartopen (this);
}else if (laststatus = = Status.open) {
Onswipelistener.onstartclose (this);
}
}
}



}

Private Status UpdateStatus () {
int left = Mfrontview.getleft ();
if (left = = 0) {
return status.close;
}else if (left = =-mrange) {
return status.open;
}

return status.swiping;
}

@Override
public void Computescroll () {
Super.computescroll ();
Maintain smooth animations continue
if (Mhelper.continuesettling (true)) {
Viewcompat.postinvalidateonanimation (this);
}
}

/**
* Close
*/
protected void Close () {
Close (true);
}
public void Close (Boolean Issmooth) {

int finalleft = 0;
if (Issmooth) {
Trigger a smooth animation
if (Mhelper.smoothslideviewto (Mfrontview, Finalleft, 0)) {
Viewcompat.postinvalidateonanimation (this);
}

}else {
Layoutcontent (FALSE);
}
}

/**
* Open
*/
protected void Open () {
Open (true);
}
public void Open (Boolean Issmooth) {

int finalleft =-mrange;
if (Issmooth) {
Trigger a smooth animation
if (Mhelper.smoothslideviewto (Mfrontview, Finalleft, 0)) {
Viewcompat.postinvalidateonanimation (this);
}

}else {
Layoutcontent (FALSE);
}
}
2. Transfer Touch event intercept judgment, handle touch event
public boolean onintercepttouchevent (Android.view.MotionEvent ev) {
if (mhelper.shouldintercepttouchevent (EV)) {
return true;
}
return super.onintercepttouchevent (EV);
};
/**
* Time is handed to Mhelper, default return True
*/
@Override
public boolean ontouchevent (Motionevent event) {

try {
Mhelper.processtouchevent (event);
} catch (Exception e) {
E.printstacktrace ();
}

return true;
}

@Override
protected void OnLayout (Boolean changed, int left, int top, int. right,
int bottom) {
Super.onlayout (changed, left, top, right, bottom);

The default is off state
Layoutcontent (FALSE);
}

/**
* Display content according to the current opening status
* @param isOpen
*/
private void Layoutcontent (Boolean isOpen) {
Set pre-layout position
Rect rect = Computefrontrect (IsOpen);
Mfrontview.layout (Rect.left, Rect.top, Rect.right, Rect.bottom);
Post-layout position based on pre-layout position
Rect backrect = Computebackrectviafront (rect);
Mbackview.layout (Backrect.left, Backrect.top, Backrect.right, Backrect.bottom);

Adjust the order of any layout to the top
Bringchildtofront (Mfrontview);
}

/**
* Rectangular area of the post-calculation layout
* @param rect
* @return
*/
Private rect computebackrectviafront (rect rect) {
int left = Rect.right;
return new Rect (left, 0, left + mrange, 0 + mheight);
}

/**
* Rectangular area of pre-calculation layout
* @param isOpen
* @return
*/
Private Rect Computefrontrect (Boolean isOpen) {
int left = 0;
if (IsOpen) {
left =-mrange;
}
return new Rect (left, 0, left + mwidth, 0 + mheight);
}

@Override
protected void onsizechanged (int w, int h, int oldw, int oldh) {
Super.onsizechanged (W, H, OLDW, OLDH);

Mrange = Mbackview.getmeasuredwidth ();
Mwidth = Getmeasuredwidth ();
Mheight = Getmeasuredheight ();

}
/**
* Some operations such as assigning values
*/
@Override
protected void Onfinishinflate () {
Super.onfinishinflate ();

Mbackview = Getchildat (0);
Mfrontview = Getchildat (1);
}

/**
* This method can be used to set the drag distance and other operations
*/
@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
TODO auto-generated Method Stub
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);


}

}

============================================================

Package com.one.swipe;

Import java.util.ArrayList;
Import java.util.List;

Import Android.content.Context;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.TextView;

Import Com.one.swipe.SwipeLayout.OnSwipeListener;

public class Myadapter extends Baseadapter {
Private context context;
Private list<string> List;
Private arraylist<swipelayout> Openeditems;


Public Myadapter (context context, list<string> List) {
Super ();
This.context=context;
This.list=list;
Openeditems = new arraylist<swipelayout> ();
}
@Override
public int GetCount () {
return List==null?0:list.size ();
}

@Override
Public Object getItem (int position) {
return list = = null? 0:position;
}

@Override
public long getitemid (int position) {
return 0;
}

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
Viewholder Holder;
if (convertview==null) {
Holder = new Viewholder ();
Convertview=view.inflate (context, r.layout.public_type_item, null);
Holder.tv_type= (TextView) Convertview.findviewbyid (R.id.tv_type);
Holder.tv_date= (TextView) Convertview.findviewbyid (r.id.tv_date);
Holder.tv_price= (TextView) Convertview.findviewbyid (R.id.tv_price);
Convertview.settag (holder);
}else{
Holder= (Viewholder) Convertview.gettag ();
}
Swipelayout SL = (swipelayout) Convertview;

Sl.setonswipelistener (New Onswipelistener () {

@Override
public void OnClose (Swipelayout layout) {
Openeditems.remove (layout);
}

@Override
public void OnOpen (Swipelayout layout) {
Openeditems.add (layout);
}

@Override
public void Onstartopen (Swipelayout layout) {
Close all entries that are already open
for (int i = 0; i < openeditems.size (); i++) {
Openeditems.get (i). Close (true);
}

Openeditems.clear ();
}

@Override
public void Onstartclose (Swipelayout layout) {
}

});

String Tv_type=holder.tv_type.gettext (). toString (). Trim ();
list<string> list = new arraylist<string> ();
List.add (Tv_type);
return convertview;
}
Class viewholder{
Public TextView Tv_type;
Public TextView tv_date;
Public TextView Tv_price;
}


}

====================================

Mainactivity

Package com.one.swipe;

Import java.util.ArrayList;
Import java.util.List;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.ListView;

public class Mainactivity extends Activity implements Onclicklistener {

Private Button btn;
Private ListView view;
Private list<string> List;
Private Myadapter adapter;
Private EditText et;


@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

BTN = (Button) Findviewbyid (R.ID.BTN);
Btn.setonclicklistener (this);
View = (ListView) Findviewbyid (R.id.listview);
ET = (EditText) Findviewbyid (r.id.et);


InitData ();
}


private void InitData () {
List = new ArrayList ();
for (int i = 0; i < 3; i++) {
List.add (i + "");
}

}

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Switch (V.getid ()) {
Case R.ID.BTN:


adapter = new Myadapter (Getapplicationcontext (), list);
View.setadapter (adapter);
Adapter.notifydatasetchanged ();


Break

Default
Break
}
}

}

================================

Main.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<scrollview xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:scrollbars= "Vertical" >

<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
>

<edittext
Android:id= "@+id/et"
Android:layout_width= "Match_parent"
android:layout_height= "40DP"
Android:layout_gravity= "Center_horizontal"
android:text=, "write something."
/>
<button
android:layout_margintop= "20DP"
Android:id= "@+id/btn"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "See Effect"
Android:layout_centerinparent= "true"
android:layout_gravity= "Center"
/>
<listview
Android:id= "@+id/listview"
Android:layout_width= "Fill_parent"
android:layout_height= "120DP"
android:layout_margintop= "20DP"
/>

</LinearLayout>
</ScrollView>

============================

Item.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<com.one.swipe.swipelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/sl"
Android:layout_width= "Match_parent"
android:layout_height= "@dimen/item_height" >
<linearlayout
Android:layout_width= "Wrap_content"
android:layout_height= "@dimen/item_height"
android:orientation= "Horizontal" >

<textview
Android:layout_width= "60DP"
android:layout_height= "@dimen/item_height"
Android:background= "#666666"
android:gravity= "Center"
Android:text= "Cancel"
Android:textcolor= "#FFFFFF"/>

<textview
Android:layout_width= "60DP"
android:layout_height= "@dimen/item_height"
Android:background= "#FF0000"
android:gravity= "Center"
android:text= "Delete"
Android:textcolor= "#FFFFFF"/>
</LinearLayout>
<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "@dimen/item_height"
android:background= "@drawable/bg_detail_item"
android:orientation= "Horizontal"
>

<textview
Android:id= "@+id/tv_type"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"
android:layout_weight= "1"
android:gravity= "Center"
Android:hint= "Do the ordinary One"
Android:textcolor= "@color/txt_date"
Android:textsize= "@dimen/text_size"/>

<textview
Android:id= "@+id/tv_date"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"
android:layout_weight= "1"
android:gravity= "Center"
Android:hint= "with 10 times times of painstaking"
Android:textcolor= "@color/txt_date"
Android:textsize= "@dimen/text_size"/>

<textview
Android:id= "@+id/tv_price"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "Center"
android:layout_weight= "1"
android:gravity= "Center"
Android:hint= "a Castle 2012"
Android:textcolor= "@color/txt_date"
Android:textsize= "@dimen/text_size"/>

</LinearLayout>
</com.one.swipe.SwipeLayout>

OK, that's all of it. There are also 5 of the Viewdraghelper have a simple to difficult demo finish, tomorrow in a piece of upload it,

Hosts file configuration c:\windows\system32\drivers\etc after the copy, enter, call me Lei Feng Good, google,github normal use do not forget Dabigatran oh: Android&go,let ' s go! 521039620

hosts:http://download.csdn.net/detail/onebelowzero2012/9359679

Multiple implementations of the Android swipe delete (i)

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.