当前位置: 移动技术网 > 移动技术>移动开发>Android > Android ListView滑动删除操作(SwipeListView)

Android ListView滑动删除操作(SwipeListView)

2019年07月24日  | 移动技术网移动技术  | 我要评论

新版本的微信和qq上引入的滑动删除功能是现在比较流行的一个功能。其实这个滑动删除的控件,github上已经有了,是一个热门的开源框架swipelistview。不过,这个swipelistview是一个framelayout,即是一个两层的布局,上面的布局front覆盖了下面的布局back,滑动的时候则会滑开front,这样下面的back就显示出来了。但是看了一下微信的滑动删除好像不是这样的,感觉更像是一个超出了屏幕的单层布局,滑动的时候是右边超出屏幕的button进入屏幕,猜测应该不是使用swipelistview控件。qq的滑动删除则是在listview的item右边隐藏一个button,但检测到滑动事件的时候,给button一个出现的动画,使其可见,这个方案应该是最好实现的了。

本篇主要是学习swipelistview这个开源框架。

使用这个框架有两种方式,一种是导入swipelistviewlibrary这个工程,将其作为一个android工程的依赖库。由于swipelistviewlibrary库工程自身也依赖另外一个热门的开源框架nineoldandroids,这个也很容易就能网上或者github上搜到。

导入这两个库工程,对于nineoldandroids,做如下设置,其实主要就是勾选is library这个选项,这样就能是nineoldandroids工程作为别的工程的依赖库使用: 

对于swipelistviewlibrary,除了要勾选is library选项,记得在旁边的add里面,加上上面的nineoldandroids作为本库的依赖库:

下面就是使用这个库了,先clean一下上面两个库工程,很多时候工程的错误,clean一下就好了。然后新建自己的工程,在add选项里面添加swipelistviewlibrary工程就行。这样就能直接使用swipelistview这个控件了,很简单,代码如下: 

 <com.fortysevendeg.swipelistview.swipelistview
  android:id="@+id/swipe_lv"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  app:swipemode="left"
  app:swipeanimationtime="300"
  app:swipeoffsetleft="200dp"
  app:swipefrontview="@+id/front"
  app:swipebackview="@+id/back"
  app:swipeactionleft="reveal"/>

其中app:swipefrontview属性就是指定前面说的framelayout里面上面一层的view的id,app:swipebackview则是指定下面一层的view的id,在下面自定义baseadatpter要使用的item的布局里面可以看到:

 <?xml version="1.0" encoding="utf-8"?>
<framelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
  <relativelayout
    android:id="@+id/back"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <button
      android:id="@+id/close_btn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centervertical="true"
      android:layout_toleftof="@+id/delete_btn"
      android:text="close"
      android:textappearance="?android:attr/textappearancemedium"
      android:focusable="false"
      android:focusableintouchmode="false"/>
    <button
      android:id="@+id/delete_btn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centervertical="true"
      android:layout_alignparentright="true"
      android:text="delete"
      android:textappearance="?android:attr/textappearancemedium"
      android:focusable="false"
      android:focusableintouchmode="false"/>
  </relativelayout>
  <relativelayout
    android:id="@+id/front"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
    <textview
      android:id="@+id/content_tv"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center"
      android:text="hello world"
      android:textappearance="?android:attr/textappearancemedium"
      android:textcolor="@android:color/black"/>
  </relativelayout>
</framelayout>

在activity里面初始化的代码:

arrays = new arraylist<string>(arrays.aslist(util.arrays));

    mswipelv = (swipelistview)findviewbyid(r.id.swipe_lv);
    madapter = new myadapter(this, arrays);
    mswipelv.setadapter(madapter);
    mswipelv.setswipelistviewlistener(new baseswipelistviewlistener() {

      @override
      public void onclosed(int position, boolean fromright) {
      }
    });

以及自定义baseadapter中的getview(): 

    @override
    public view getview(final int position, view convertview, final viewgroup parent) {
      viewholder holder;

      if(convertview == null) {
        holder = new viewholder();
        convertview = layoutinflater.from(mcontext).inflate(
            r.layout.layout_swipe_list_item, null);
        holder.mcontenttv = (textview)convertview.findviewbyid(r.id.content_tv);
        holder.mclosebtn = (button)convertview.findviewbyid(r.id.close_btn);
        holder.mdeletebtn = (button)convertview.findviewbyid(r.id.delete_btn);

        convertview.settag(holder);
      } else {
        holder = (viewholder)convertview.gettag();
      }

      holder.mcontenttv.settext(arrays.get(position));
      holder.mclosebtn.setonclicklistener(new view.onclicklistener() {

        @override
        public void onclick(view v) {
          ((swipelistview)parent).closeanimate(position);
        }
      });
      holder.mdeletebtn.setonclicklistener(new view.onclicklistener() {

        @override
        public void onclick(view v) {
          ((swipelistview)parent).closeopeneditems();
          mhandler.postdelayed(new runnable() {

            @override
            public void run() {
              marrays.remove(position);
              madapter.notifydatasetchanged();
            }
          }, 350);
        }
      });

      return convertview;
    }

然后就ok了,运行工程的效果如下图: 

另外一种是用swipelistview控件的方法就是直接导入官方给出的两个jar包,上面开篇的地址里可以看到,但是直接导入这两个jar包,不代表可以立即使用了!首先先把这个包添加到新建工程的build path里面,如果你的工程没有添加android的支持包android-support-v4.jar记得也添加以下,然后记得从前面已经导入过的swipelistviewlibrary库工程中的res\values\swipelistview__attrs.xml文件复制到新建工程的res/values/目录下,这个文件主要是申明swipelistview控件里面的各项属性的,直接导入的jar包是没有包含申明这些属性的文件的。然后就是向上面一样在代码里面引用了,不过需要注意两点:一,jar包里面swipelistview的包名和库工程里面的包名是不一样的,引用的时候需要注意以下;二,准备妥当,确认前面步骤无误后,有时在编译工程时回报错,说没有申明swipefrontview和swipebackview两个属性,这个问题好像是swipelistview框架的一个bug,stackoverflow上有人指出过,,大意就是在布局文件里面申明swipefrontview和swipebackview这两个属性的值得时候,最好不要自定义id的名称,而是使用swipelistview_backview和swipelistview_frontview。

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网