Android uses Swipelistview to achieve a sliding delete effect like QQ _android

Source: Internet
Author: User

QQ Sliding Delete effect is very good, to achieve this effect, you can use Swipelistview.

1. Download Com.fortysevendeg.swipelistview this project (formerly GitHub on, there is now GitHub, Baidu has been downloaded many times before), import Eclipse, right click, select Properties-> Android, select the islibrary below the library.

2. Create a new project Myswipelistview and join Swipelistview this library.

3. Put a Swipelistview control inside the main form:

<linearlayout 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:o" rientation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen Activity_horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen Activity_vertical_margin "tools:context=" com.hzhi.myswipelistview.MainActivity "> <com.fortysevendeg.swipel Istview.
      Swipelistview xmlns:swipe= "Http://schemas.android.com/apk/res-auto" android:id= "@+id/exampleswipelistview"
      Android:listselector= "#00000000" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" swipe:swipebackview= "@+id/back" swipe:swipecloseallitemswhenmovelist= "true" swipe:swipedrawablechecked= "@d Rawable/choice_selected "swipe:swipedrawableunchecked=" @drAwable/choice_unselected "swipe:swipefrontview=" @+id/front "swipe:swipemode=" both "Swipe:swipeactionlef" 

 T= "reveal" swipe:swipeactionright= "dismiss" swipe:swipeopenonlongpress= "true"/> </LinearLayout>

Two of these important properties:
swipe:swipefrontview: view above, which is displayed when not sliding.
Swipe:swipebackview: View below, that is, the view that is displayed after sliding.
These two view definitions are defined in the Swipelistview row layout file:  

<?xml version= "1.0" encoding= "Utf-8"?> <framelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "> <linearla Yout android:id= "@+id/back" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Androi D:background= "#ffcccccc" android:gravity= "right" android:tag= "back" > <button android:layout_wi Dth= "Wrap_content" android:layout_height= "wrap_content" android:id= "@+id/btn_delete" android:text=
        "Delete"/> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:id= "@+id/btn_update" android:text= "Update"/> </LinearLayout> <relativelayout android: orientation= "vertical" android:id= "@+id/front" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" Android:background= "#ffffffff" > <imageview android:layout_width= "wrap_content android:layout_height=" WR Ap_content "android:id=" @+id/example_row_iv_image "/> <textview android:layout_width=" Fill_paren T "android:layout_height=" wrap_content "android:layout_torightof=" @id/example_row_iv_image "Androi D:id= "@+id/example_row_tv_title"/> <textview android:layout_width= "Fill_parent" Android:layout_ height= "Wrap_content" android:layout_torightof= "@id/example_row_iv_image" android:layout_below= "@id/exampl E_row_tv_title "android:id=" @+id/example_row_tv_description "/> </RelativeLayout> </framelay

 Out>

The Swipelistview row layout file uses the Framelayout layout, framelayout all of the child elements are stacked in the upper-left corner of the framelayout.

4. Swipelistview, like other ListView, also needs adapter, and the same method is used. This is not a detailed story here.

5. Implement Swipelistview functionality in the main form Java file with the following code:

Package Com.hzhi.myswipelistview;
Import android.support.v7.app.ActionBarActivity;

Import Android.util.Log;

Import java.util.ArrayList;
Import Com.fortysevendeg.swipelistview.BaseSwipeListViewListener;

Import Com.fortysevendeg.swipelistview.SwipeListView;

Import Android.os.Bundle; @SuppressWarnings ("deprecation") public class Mainactivity extends Actionbaractivity {protected static final String 
  TAG = "Myswipelistview";
  Private arraylist<string> mlist;
  Private Myadapter Madapter;

  Private Swipelistview Mswipelistview;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    
    Setcontentview (R.layout.activity_main);
    InitData ();
    Mswipelistview = (Swipelistview) Findviewbyid (R.id.exampleswipelistview); 
    Madapter = new Myadapter (this, mlist, Mswipelistview);
    
    Mswipelistview.setadapter (Madapter); 
  Mswipelistview.setswipelistviewlistener (New Baseswipelistviewlistener () {@Override    public void onchoicechanged (int position, Boolean selected) {LOG.D (TAG, "onchoicechanged:" + position 
      + "," + selected); 
      @Override public void onchoiceended () {LOG.D (TAG, "onchoiceended"); 
      @Override public void onchoicestarted () {LOG.D (TAG, "onchoicestarted"); @Override public void Onclickbackview (int position) {LOG.D (TAG, "Onclickbackview:" + PO 
      Sition); @Override public void Onclickfrontview (int position) {LOG.D (TAG, "Onclickfrontview:" + 
      position);  @Override public void onclosed (int position, Boolean fromright) {log.d (TAG, "onclosed:" 
      + Position + "," + fromright);        
      @Override public void Ondismiss (int[] reversesortedpositions) {log.d (TAG, "Ondismiss"); } @Override public void onfirstlistItem () {LOG.D (TAG, "Onfirstlistitem"); 
      @Override public void Onlastlistitem () {LOG.D (TAG, "Onlastlistitem"); 
        @Override public void onlistchanged () {LOG.D (TAG, "onlistchanged"); 
 
      Mswipelistview.closeopeneditems (); @Override public void OnMove (int position, float x) {log.d (TAG, "onmove:" + position + 
      "," + x);  @Override public void onopened (int position, Boolean toright) {log.d (TAG, "onopened:" + 
      Position + "," + toright); @Override public void Onstartclose (int position, Boolean right) {LOG.D (TAG, "Onstartclo 
      SE: "+ position +", "+ right"; @Override public void Onstartopen (int position, int action, Boolean right) {LOG.D (TAG, 
      "Onstartopen:" + position + "," + Action + "," + right);
    
  } 
    }); } private void InitData () {mlist = new arraylist<string> ();

 for (int i = 0; I <= i++) Mlist.add ("This is the first" + i + "Data!");}

The main code is mswipelistview.setswipelistviewlistener (new Baseswipelistviewlistener () {}), which, through this line of code, Set the listener for the Swipelistview control, and you can overload the various methods of Baseswipelistviewlistener according to your needs.

Run Result:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.