SoFunction
Updated on 2025-04-11

Very practical side-sliding delete control SwipeLayout

Preface: The effect similar to QQ sliding click deletion is often used in the project, and there are many open source libraries on the Internet. Personally, I feel that SwipeLayout is the best. The following describes how to use it.

1. First, import the required Jar packages, including 3, AndroidSwipeLayout-v1.1., AndroidViewAnimations-1.1., and nineoldandroids-2.4. The first jar package is the jar package we use this control, and the next two are the jar packages needed to delete the menu animation when sliding sideways. Here is how to use it.

The xml file of the main layout is as follows, which is a ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <ListView
    android:
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
  </ListView>

</LinearLayout>

The layout of each Item in the listview is as follows:

<?xml version="1.0" encoding="utf-8"?>
< xmlns:swipe="/apk/res-auto"
  xmlns:andro
  android:
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="#FF5534"
    android:gravity="center"
    android:tag="Bottom3"
    android:weightSum="10" >

    <ImageView
      android:
      android:layout_width="27dp"
      android:layout_height="30dp"
      android:layout_weight="1"
      android:src="@drawable/trash" />

    <TextView
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="5"
      android:text="Delete Item?"
      android:textColor="#fff"
      android:textSize="17sp" />

    <Button
      android:
      android:layout_width="0dp"
      android:layout_height="40dp"
      android:layout_weight="4"
      android:background="@drawable/white"
      android:text="Yes,Delete"
      android:textColor="#FF5534" />
  </LinearLayout>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/item_selector"
    android:padding="10dp" >

    <TextView
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />

    <TextView
      android:
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:tag="Hover"
      android:text="Just Do it . " />
  </LinearLayout>

</>

The LinerLayout above is equivalent to bottomView, and the one below is corresponding to SurfaceView. At the beginning, the SurfaceView is displayed on the phone screen, while the bottomView is outside the screen, gradually showing on the screen as the fingers slide.

as follows:

package ;

import ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;

public class ListViewAdapter extends BaseSwipeAdapter {

  private Context mContext;
  private List&lt;String&gt; mDatas;
  //private TextView mDelete;
  //private SwipeLayout swipeLayout;
  private int pos ;

  public ListViewAdapter(Context context, List&lt;String&gt; mDatas) {
     = context;
     = mDatas;
  }

  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    return ();
  }

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return (position);
  }

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  }

  @Override
  public void fillValues(int position, View convertView) {
    // TODO Auto-generated method stub
    ("fillValues", "position = "+position);
    TextView tv = (TextView) ();
    //((position + 1) + ".");
    ((position)+"....");

    final SwipeLayout sl = (SwipeLayout) (getSwipeLayoutResourceId(position));
    final TextView delete = (TextView) ();
    (position);
    (new OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        int pos = (Integer) ();
        String obj = (pos);

        ("onClick", "........pos ...."+pos+" obj = "+obj);
        (obj);
        notifyDataSetChanged();
        ();
      }
    });

  }

  @Override
  public View generateView(int position, ViewGroup arg1) {
    // TODO Auto-generated method stub
    ("generateView", "position = "+position);
    View v = (mContext).inflate(.swipe_lv_item,null);
    pos = position;
    final SwipeLayout swipeLayout = (SwipeLayout) ();

    (new SimpleSwipeListener() {
      @Override
      public void onOpen(SwipeLayout layout) {//Callback function when the hidden delete menu is opened        // TODO Auto-generated method stub
        ().duration(500).delay(100).playOn(());

      }
    });

    (new () {
          @Override
          public void onDoubleClick(SwipeLayout layout,
              boolean surface) {
            (mContext, "DoubleClick",Toast.LENGTH_SHORT).show();

          }
        });
//   ().setOnClickListener(
//       new () {
//         @Override
//         public void onClick(View view) {
//           (mContext, "click delete position = "+pos,Toast.LENGTH_SHORT).show();
//           ();
//         }
//       });

    return v;
  }

  @Override
  public int getSwipeLayoutResourceId(int position) {
    // TODO Auto-generated method stub
    return ;
  }

}

Notice, generateView just adds layout, it is best not to set click events inside. Click event to set fillValues ​​in this method.

as follows:

package ;

import ;
import ;

import ;

import ;
import ;
import ;

import ;

public class SwipeListViewActivity extends Activity{

  private ListView mListView;
  private ListViewAdapter mAdapter;
  private List<String> mDatas;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    (savedInstanceState);
    setContentView(.swipe_layout_main);

    getData();

    mListView = (ListView) findViewById(.swipe_listview);
    mAdapter = new ListViewAdapter(this, mDatas);
    ();
    (mAdapter);
  }

  public void getData(){
    mDatas = new ArrayList<String>();
    //for(int i =0; i<10; i++){
      ("A");
      ("B");
      ("C");
      ("D");
      ("E");
      ("F");
      ("G");
      ("H");
      ("I");
      ("J");


    //}
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.