Android仿微信实现左滑显示删除按钮功能
程序员文章站
2024-02-28 17:14:16
在实际项目中删除列表中的某一项是非常常见的功能,传统的做法可以使用长按监听器等,而现在流行的做法是左滑弹出删除按钮,微信,qq等都是这么做的,下面做一个示例,代码如下:...
在实际项目中删除列表中的某一项是非常常见的功能,传统的做法可以使用长按监听器等,而现在流行的做法是左滑弹出删除按钮,微信,qq等都是这么做的,下面做一个示例,代码如下:
主页面mainactivity:代码比较简单常规
package com.home.testslideview; import java.util.arraylist; import java.util.list; import android.app.activity; import android.os.bundle; import android.widget.listview; import com.home.textslideview.r; public class mainactivity extends activity { private listview listview; private list<newinfobean> list = new arraylist<newinfobean>(); // 适配器 private slideadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); initview(); initdata(); setadapter(); } /** * 初始化页面控件 */ private void initview() { listview = (listview) findviewbyid(r.id.main_lv); } /** * 初始化数据 */ private void initdata() { list.add(new newinfobean("这是测试内容1")); list.add(new newinfobean("这是测试内容2")); list.add(new newinfobean("这是测试内容3")); list.add(new newinfobean("这是测试内容4")); list.add(new newinfobean("这是测试内容5")); list.add(new newinfobean("这是测试内容6")); list.add(new newinfobean("这是测试内容7")); list.add(new newinfobean("这是测试内容8")); list.add(new newinfobean("这是测试内容9")); list.add(new newinfobean("这是测试内容10")); } /** * 设置适配器 */ private void setadapter() { if (adapter == null) { adapter = new slideadapter(this, list); listview.setadapter(adapter); } else { adapter.setlist(list); adapter.notifydatasetchanged(); } } }
实体类newinfobean:具体项目中由自己定义:
package com.home.testslideview; public class newinfobean { public slideview slideview; private string content; public slideview getslideview() { return slideview; } public void setslideview(slideview slideview) { this.slideview = slideview; } public string getcontent() { return content; } public void setcontent(string content) { this.content = content; } public newinfobean() { super(); } public newinfobean(string content) { super(); this.content = content; } }
适配器slideadapter:也比较简单
package com.home.testslideview; import java.util.list; import android.content.context; import android.util.sparsearray; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.textview; import com.home.testslideview.slideview.onslidelistener; import com.home.textslideview.r; public class slideadapter extends baseadapter implements onslidelistener, onclicklistener { private layoutinflater inflater; private list<newinfobean> list; private context context; public slideadapter(context context, list<newinfobean> list) { if (inflater == null) { inflater = layoutinflater.from(context); } this.list = list; this.context = context; } @override public int getcount() { return list.size(); } @override public object getitem(int position) { return list.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup arg2) { slideview slideview = (slideview) convertview; newinfobean bean = list.get(position); if (slideview == null) { slideview = new slideview(context); slideview.setonslidelistener(this); } // 设置内容 textview contenttext = getadapterview(slideview, r.id.slideview_tv_content, position); contenttext.settext(bean.getcontent()); // 删除按钮 textview deltext = getadapterview(slideview, r.id.slideview_tv_del, position); deltext.setonclicklistener(this); bean.slideview = slideview; bean.slideview.shrink(); return slideview; } @suppresswarnings("unchecked") public <t extends view> t getadapterview(view convertview, int id, object tag) { sparsearray<view> viewholder = null; try { if (convertview.gettag(r.id.view_holder) instanceof sparsearray<?>) { viewholder = (sparsearray<view>) convertview .gettag(r.id.view_holder); } } catch (classcastexception e) { } if (viewholder == null) { viewholder = new sparsearray<view>(); convertview.settag(r.id.view_holder, viewholder); convertview.settag(r.id.order_id, tag); } view childview = viewholder.get(id); if (childview == null) { childview = convertview.findviewbyid(id); childview.settag(tag); viewholder.put(id, childview); } return (t) childview; } public list<newinfobean> getlist() { return list; } public void setlist(list<newinfobean> list) { this.list = list; } @override public void onslide(view view, int status) { } @override public void onclick(view v) { switch (v.getid()) { case r.id.slideview_tv_del: int position = (integer) v.gettag(); list.remove(position); notifydatasetchanged(); break; default: break; } } }
比较关键的两个类:
自定义的listview:slidelistview
package com.home.testslideview; import android.content.context; import android.util.attributeset; import android.view.motionevent; import android.view.view; import android.widget.listview; public class slidelistview extends listview { private slideview itemview; public slidelistview(context context) { super(context); } public slidelistview(context context, attributeset attrs) { super(context, attrs); } public void shrinklistitem(int position) { view item = getchildat(position); if (item != null) { try { ((slideview) item).shrink(); } catch (classcastexception e) { e.printstacktrace(); } } } @override public boolean ontouchevent(motionevent event) { switch (event.getaction()) { case motionevent.action_down: { int x = (int) event.getx(); int y = (int) event.gety(); int position = pointtoposition(x, y); if (position != invalid_position) { newinfobean data = (newinfobean) getitematposition(position); itemview = data.slideview; } } default: break; } if (itemview != null) { itemview.onrequiretouchevent(event); } return super.ontouchevent(event); } }
自定义的listview中的每一行控件:slideview(借鉴网上的一个示例):
package com.home.testslideview; import android.content.context; import android.util.attributeset; import android.util.log; import android.util.typedvalue; import android.view.layoutinflater; import android.view.motionevent; import android.view.view; import android.widget.linearlayout; import android.widget.scroller; import com.home.textslideview.r; public class slideview extends linearlayout { private static final string tag = slideview.class.getsimplename(); private context mcontext; private scroller mscroller; private onslidelistener monslidelistener; private int mholderwidth = 80; private int mlastx = 0; private int mlasty = 0; private static final int tan = 2; private layoutinflater inflater; public interface onslidelistener { public static final int slide_status_off = 0; public static final int slide_status_start_scroll = 1; public static final int slide_status_on = 2; /** * @param view * current slideview * @param status * slide_status_on or slide_status_off */ public void onslide(view view, int status); } public slideview(context context) { super(context); initview(); } public slideview(context context, attributeset attrs) { super(context, attrs); initview(); } private void initview() { mcontext = getcontext(); if (inflater == null) { inflater = layoutinflater.from(mcontext); } mscroller = new scroller(mcontext); setorientation(linearlayout.horizontal); view.inflate(mcontext, r.layout.slide_view_merge, this); mholderwidth = math.round(typedvalue.applydimension( typedvalue.complex_unit_dip, mholderwidth, getresources() .getdisplaymetrics())); } public void setonslidelistener(onslidelistener onslidelistener) { monslidelistener = onslidelistener; } public void shrink() { if (getscrollx() != 0) { this.smoothscrollto(0, 0); } } public void onrequiretouchevent(motionevent event) { int x = (int) event.getx(); int y = (int) event.gety(); int scrollx = getscrollx(); log.d(tag, "x=" + x + " y=" + y); switch (event.getaction()) { case motionevent.action_down: { if (!mscroller.isfinished()) { mscroller.abortanimation(); } if (monslidelistener != null) { monslidelistener.onslide(this, onslidelistener.slide_status_start_scroll); } break; } case motionevent.action_move: { int deltax = x - mlastx; int deltay = y - mlasty; if (math.abs(deltax) < math.abs(deltay) * tan) { break; } int newscrollx = scrollx - deltax; if (deltax != 0) { if (newscrollx < 0) { newscrollx = 0; } else if (newscrollx > mholderwidth) { newscrollx = mholderwidth; } this.scrollto(newscrollx, 0); } break; } case motionevent.action_up: { int newscrollx = 0; if (scrollx - mholderwidth * 0.75 > 0) { newscrollx = mholderwidth; } this.smoothscrollto(newscrollx, 0); if (monslidelistener != null) { monslidelistener.onslide(this, newscrollx == 0 ? onslidelistener.slide_status_off : onslidelistener.slide_status_on); } break; } default: break; } mlastx = x; mlasty = y; } private void smoothscrollto(int destx, int desty) { // 缓慢滚动到指定位置 int scrollx = getscrollx(); int delta = destx - scrollx; mscroller.startscroll(scrollx, 0, delta, 0, math.abs(delta) * 3); invalidate(); } @override public void computescroll() { if (mscroller.computescrolloffset()) { scrollto(mscroller.getcurrx(), mscroller.getcurry()); postinvalidate(); } } }
main.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.home.testslideview.slidelistview android:id="@+id/main_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:cachecolorhint="#00000000" android:fadingedge="none" android:listselector="#00000000" android:scrollbars="none" /> </linearlayout>
slide_view_merge.xml:
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <linearlayout android:id="@+id/slideview_layout_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:gravity="center_vertical" android:orientation="horizontal" > <textview android:id="@+id/slideview_tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容部分"/> </linearlayout> <linearlayout android:id="@+id/view_layout_del" android:layout_width="80dp" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_torightof="@id/slideview_layout_content" android:clickable="true" android:gravity="center_vertical" android:orientation="horizontal" > <textview android:id="@+id/slideview_tv_del" android:layout_width="80dp" android:layout_height="match_parent" android:layout_marginbottom="2dp" android:gravity="center" android:padding="15dp" android:text="删除"/> </linearlayout> </merge>
源码下载:高仿微信左滑删除效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 一起动手编写Android图片加载框架