当前位置: 移动技术网 > 移动技术>移动开发>Android > 详解SwipeListView框架实现微信\QQ滑动删除效果

详解SwipeListView框架实现微信\QQ滑动删除效果

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

qq或者微信出现过滑动,最近联系人列表,可以删去当前选中的联系人,这个功能很棒。

就是试着做了下。其实是使用了开源框架swipelistview 。

 

swipelistview 与一般的listview使用方式差不多,只是增加了一些特殊功能。 

<com.fortysevendeg.swipelistview.swipelistview
  xmlns:swipe="http://schemas.android.com/apk/res-auto"
  android:id="@+id/example_lv_list"
  android:listselector="#00000000"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  swipe:swipefrontview="@+id/front"
  swipe:swipebackview="@+id/back"
  swipe:swipeactionleft="[reveal | dismiss]"
  swipe:swipeactionright="[reveal | dismiss]"
  swipe:swipemode="[none | both | right | left]"
  swipe:swipecloseallitemswhenmovelist="[true | false]"
  swipe:swipeopenonlongpress="[true | false]"
  swipe:swipeanimationtime="[miliseconds]"
  swipe:swipeoffsetleft="[dimension]"
  swipe:swipeoffsetright="[dimension]"
  /> 


•swipefrontview -listview item正常显示的控件id,且必须与item的布局文件中的控件id一样
•swipebackview - 手指滑动时显示的,隐藏在frontview后面,且必须与item的布局文件中控件id一样
•swipeactionleft - 左滑的动作,默认reveal,即显示backview,还有dismiss,choice会触发响应的方法。
•swipeactionright - 右滑动作,其他同上
•swipemode - default: 'both' 设置左滑、右滑、都支持
•swipecloseallitemswhenmovelist - 当滚动listview时,关闭所有展开的item,最好不要设置为false,由于item的   
• 复用,false存在一些问题。
•swipeopenonlongpress - default: 'true' 长按时触发显示
•swipeanimationtime - 动画时间长度
•swipeoffsetleft - left offset 左偏移量
•swipeoffsetright - right offset 右偏移量

   mswipelistview = (swipelistview) findviewbyid(r.id.id_swipelistview); 
  madapter = new dataadapter(this, mdatas , mswipelistview); 
  mswipelistview.setadapter(madapter); 
 
  mswipelistview.setswipelistviewlistener(new baseswipelistviewlistener() 
  { 
   @override 
   //重写baseswipelistviewlistener父类需要的方法
   }; 

使用方式很简单 和普通的listview 相似,不需要多说。 

对于 listview的item删除单个元素,只需要在adapter中处理button的点击事件,或者写一个回调传回activity中处理

我这里给出在adapter中处理的方式的代码: 

 @override 
 public view getview(final int position, view convertview, viewgroup parent) 
 { 
  convertview = minflater.inflate(r.layout.list_item, null); 
 
  textview tv = (textview) convertview.findviewbyid(r.id.id_text); 
  button del = (button) convertview.findviewbyid(r.id.id_remove); 
  tv.settext(mdatas.get(position)); 
  del.setonclicklistener(new onclicklistener() 
  { 
   @override 
   public void onclick(view v) 
   { 
    mdatas.remove(position); 
    notifydatasetchanged(); 
     /** 
     * 关闭swipelistview 
     * 不关闭的话,刚删除位置的item存在问题 
     * 在监听事件中onlistchange中关闭,会出现问题 
     */ 
    mswipelistview.closeopeneditems(); 
   } 
  }); 
   
  return convertview; 
 }

源码下载:https://github.com/honjane/swipelistviewdemo

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

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

相关文章:

验证码:
移动技术网