Android仿QQ微信侧滑删除效果
程序员文章站
2024-03-31 11:13:22
仿qq侧滑删除效果图
1.自定义listview
public class dragdellistview extends listview {...
仿qq侧滑删除效果图
1.自定义listview
public class dragdellistview extends listview { private boolean moveable=false; private boolean closed=true; private float mdownx,mdowny; private int mtouchposition,oldposition=-1; private dragdelitem mtouchview,oldview; private context context; public dragdellistview(context context) { super(context); init(context); } public dragdellistview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); init(context); } public dragdellistview(context context, attributeset attrs) { super(context, attrs); init(context); } private void init(context context) { this.context=context; } @override public boolean ontouchevent(motionevent ev) { switch (ev.getaction()) { case motionevent.action_down: mtouchposition = pointtoposition((int) ev.getx(), (int) ev.gety()); mtouchview=(dragdelitem)getchildat(mtouchposition - getfirstvisibleposition()); mdownx = ev.getx(); mdowny=ev.gety(); if(oldposition==mtouchposition ||closed) { moveable=true; mtouchview.mdownx =(int)mdownx; }else { moveable=false; if(oldview!=null) { oldview.smoothclosemenu(); } } oldposition=mtouchposition; oldview=mtouchview; break; case motionevent.action_move: if (math.abs(mdownx-ev.getx()) < math.abs(mdowny-ev.gety()) * dp2px(2)) { break; } if (moveable) { int dis = (int) (mtouchview.mdownx -ev.getx()); if(mtouchview.state==mtouchview.state_open) dis+=mtouchview.mmenuview.getwidth(); mtouchview.swipe(dis); ev.setaction(motionevent.action_cancel); } break; case motionevent.action_up: if (moveable) { if ((mtouchview.mdownx -ev.getx()) > (mtouchview.mmenuview.getwidth()/2)) { // open mtouchview.smoothopenmenu(); closed=false; } else { // close mtouchview.smoothclosemenu(); closed=true; } ev.setaction(motionevent.action_cancel); } break; } return super.ontouchevent(ev); } private int dp2px(int dp) { return (int) typedvalue.applydimension(typedvalue.complex_unit_dip, dp, getcontext().getresources().getdisplaymetrics()); } }
2.自定义滑动条目
public class dragdelitem extends linearlayout { public static final int state_close = 0; public static final int state_open = 1; private view mcontentview; public view mmenuview; public int mdownx; public int state = state_close; public boolean isfling; private int mbasex; private scroller scroll; public dragdelitem(view contentview, view menuview) { super(contentview.getcontext()); scroll=new scroller(getcontext()); mcontentview = contentview; mmenuview = menuview; init(); } private dragdelitem(context context, attributeset attrs) { super(context, attrs); } private dragdelitem(context context) { super(context); } private void init() { setlayoutparams(new abslistview.layoutparams(layoutparams.match_parent, layoutparams.wrap_content)); layoutparams contentparams = new layoutparams( layoutparams.wrap_content, layoutparams.wrap_content); mcontentview.setlayoutparams(contentparams); mmenuview.setlayoutparams(new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content)); addview(mcontentview); addview(mmenuview); } public void swipe(int dis) { if (dis > mmenuview.getwidth()) { dis = mmenuview.getwidth(); } if (dis < 0) { dis = 0; } mcontentview.layout(-dis, mcontentview.gettop(), mcontentview.getwidth() - dis, getmeasuredheight()); mmenuview.layout(mcontentview.getwidth() - dis, mmenuview.gettop(), mcontentview.getwidth() + mmenuview.getwidth() - dis, mmenuview.getbottom()); } @override public void computescroll() { if (state == state_open) { if (scroll.computescrolloffset()) { swipe(scroll.getcurrx()); postinvalidate(); } } else { if (scroll.computescrolloffset()) { swipe(mbasex - scroll.getcurrx()); postinvalidate(); } } } public void smoothclosemenu() { state = state_close; mbasex = -mcontentview.getleft(); scroll.startscroll(0, 0, mbasex, 0, 350); postinvalidate(); } public void smoothopenmenu() { state = state_open; scroll.startscroll(-mcontentview.getleft(), 0, mmenuview.getwidth()/2, 0, 350); postinvalidate(); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); mmenuview.measure(measurespec.makemeasurespec(0, measurespec.unspecified), measurespec.makemeasurespec( getmeasuredheight(), measurespec.exactly)); mcontentview.measure(measurespec.makemeasurespec(0, measurespec.unspecified), measurespec.makemeasurespec( getmeasuredheight(), measurespec.exactly)); } @override protected void onlayout(boolean changed, int l, int t, int r, int b) { mcontentview.layout(0, 0, getmeasuredwidth(), mcontentview.getmeasuredheight()); mmenuview.layout(getmeasuredwidth(), 0, getmeasuredwidth() + mmenuview.getmeasuredwidth(), mcontentview.getmeasuredheight()); } }
3.所用到的布局文件
—swipecontent.xml代码
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#999999" android:padding="8dp" > <imageview android:id="@+id/iv_icon" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/ic_launcher" /> <textview android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_marginleft="10dp" android:layout_torightof="@+id/iv_icon" android:text="name" android:textcolor="@android:color/black" android:textsize="18sp" /> </relativelayout> —swipemenu.xml代码 <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" > <textview android:id="@+id/tv_open" android:layout_width="90dp" android:layout_height="match_parent" android:gravity="center" android:background="#c2c2c2" android:text="置顶" android:textcolor="@android:color/white" android:textsize="18sp" /> <textview android:id="@+id/tv_del" android:layout_width="90dp" android:layout_height="match_parent" android:gravity="center" android:background="#ff0000" android:text="删除" android:textcolor="@android:color/white" android:textsize="18sp" /> </linearlayout>
4.主界面代码
public class mainactivity extends activity { private list<applicationinfo> mapplist; private dragdellistview mlistview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list); mapplist = getpackagemanager().getinstalledapplications(0); mlistview = (dragdellistview) findviewbyid(r.id.listview); mlistview.setadapter(new appadapter(mapplist)); } class appadapter extends baseadapter { private list<applicationinfo> mapplist; public appadapter(list<applicationinfo> applist) { mapplist=applist; } @override public int getcount() { return mapplist.size(); } @override public applicationinfo getitem(int position) { return mapplist.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(final int position, view convertview, viewgroup parent) { viewholder holder=null; view menuview=null; if (convertview == null) { convertview = view.inflate(getapplicationcontext(), r.layout.swipecontent, null); menuview = view.inflate(getapplicationcontext(), r.layout.swipemenu, null); convertview = new dragdelitem(convertview,menuview); holder=new viewholder(convertview); } else { holder = (viewholder) convertview.gettag(); } applicationinfo item = getitem(position); holder.iv_icon.setimagedrawable(item.loadicon(getpackagemanager())); holder.tv_name.settext(item.loadlabel(getpackagemanager())); holder.tv_open.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { toast.maketext(mainactivity.this, "置顶:"+position, toast.length_short).show(); } }); holder.tv_del.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { toast.maketext(mainactivity.this, "删除:"+position, toast.length_short).show(); } }); return convertview; } class viewholder { imageview iv_icon; textview tv_name; textview tv_open,tv_del; relativelayout relativelayout; public viewholder(view view) { iv_icon = (imageview) view.findviewbyid(r.id.iv_icon); tv_name = (textview) view.findviewbyid(r.id.tv_name); tv_open=(textview)view.findviewbyid(r.id.tv_open); tv_del=(textview)view.findviewbyid(r.id.tv_del); relativelayout = (relativelayout) view.findviewbyid(r.id.rl_layout); //改变relativelayout宽度 windowmanager wm = (windowmanager) getapplicationcontext().getsystemservice(context.window_service); int width = wm.getdefaultdisplay().getwidth(); relativelayout.setminimumwidth(width-60); view.settag(this); } } } }
主界面布局代码
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <com.draglistview.dragdellistview android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" /> </relativelayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。