欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android使用SwipeListView实现类似QQ的滑动删除效果

程序员文章站 2024-03-07 00:02:38
qq的滑动删除效果很不错,要实现这种效果,可以使用swipelistview。 1. 下载com.fortysevendeg.swipelistview这个项目(以前...

qq的滑动删除效果很不错,要实现这种效果,可以使用swipelistview。

1. 下载com.fortysevendeg.swipelistview这个项目(以前github上有,现在github上没有了,百度了很多次才下载到的),导入eclipse,右键单击,选择properties->android,选中library下面的islibrary。

Android使用SwipeListView实现类似QQ的滑动删除效果

2. 新建一个项目myswipelistview,加入swipelistview这个库。

Android使用SwipeListView实现类似QQ的滑动删除效果

3. 在主窗体里面放入一个swipelistview控件:

<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:orientation="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.swipelistview.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="@drawable/choice_selected"
      swipe:swipedrawableunchecked="@drawable/choice_unselected"
      swipe:swipefrontview="@+id/front"
      swipe:swipemode="both" 
      swipe:swipeactionleft="reveal"
      swipe:swipeactionright="dismiss"
      swipe:swipeopenonlongpress="true"
    />

</linearlayout>

其中两个重要的属性:
swipe:swipefrontview:上面的view,即不滑动时显示的view。
swipe:swipebackview:下面的view,即滑动后显示的view。
这两个view都定义在swipelistview的行布局文件里面: 

<?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"
    >
    
  <linearlayout
    android:id="@+id/back"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffcccccc"
    android:gravity="right"
    android:tag="back" >

    <button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_delete"
        android:text="删除"/>

    <button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_update"
        android:text="更新"/>

  </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="wrap_content"
        android:id="@+id/example_row_iv_image"/>

    <textview
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_torightof="@id/example_row_iv_image"
        android: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/example_row_tv_title"
        android:id="@+id/example_row_tv_description"/>

  </relativelayout>  
  
  
</framelayout>

swipelistview的行布局文件使用framelayout布局,framelayout里面所有的所有子元素都堆叠在framelayout的左上角。 

4. swipelistview和其他listview一样,也需要adapter,使用方法也是一样的。这里就不详细讲了。 

5. 在主窗体java文件中实现swipelistview的功能,代码如下:

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:" + position); 
      } 
 
      @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, "onstartclose:" + 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 <= 10; i++) 
      mlist.add("这是第" + i +"条数据!"); 
  }
  
}

最主要的代码即mswipelistview.setswipelistviewlistener(new baseswipelistviewlistener(){}),通过这行代码,为swipelistview控件设置了listener,可以根据自己的需要重载baseswipelistviewlistener的各种方法。 

运行结果:

Android使用SwipeListView实现类似QQ的滑动删除效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。