Android ListView弹性效果的实现方法
程序员文章站
2024-03-03 19:25:22
关于在android中实现listview的弹性效果,有很多不同的方法,网上一搜,也有很多,下面贴出在项目中经常用到的两种实现listview弹性效果的方法(基本上拿来就可...
关于在android中实现listview的弹性效果,有很多不同的方法,网上一搜,也有很多,下面贴出在项目中经常用到的两种实现listview弹性效果的方法(基本上拿来就可以用),供大家参考:
第一种比较简单,好容易理解,只是动态改变了listview在y轴上的可移动距离,代码如下:
import android.content.context; import android.util.attributeset; import android.util.displaymetrics; import android.widget.listview; /** * 弹性listview。 * @author e */ public class flexilistview extends listview{ //初始可拉动y轴方向距离 private static final int max_y_overscroll_distance = 100; //上下文环境 private context mcontext; //实际可上下拉动y轴上的距离 private int mmaxyoverscrolldistance; public flexilistview(context context){ super(context); mcontext = context; initbouncelistview(); } public flexilistview(context context, attributeset attrs) { super(context, attrs); mcontext = context; initbouncelistview(); } public flexilistview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); mcontext = context; initbouncelistview(); } private void initbouncelistview(){ final displaymetrics metrics = mcontext.getresources().getdisplaymetrics(); final float density = metrics.density; mmaxyoverscrolldistance = (int) (density * max_y_overscroll_distance); } @override protected boolean overscrollby(int deltax, int deltay, int scrollx, int scrolly, int scrollrangex, int scrollrangey, int maxoverscrollx, int maxoverscrolly, boolean istouchevent) { //实现的本质就是在这里动态改变了maxoverscrolly的值 return super.overscrollby(deltax, deltay, scrollx, scrolly, scrollrangex, scrollrangey, maxoverscrollx, mmaxyoverscrolldistance, istouchevent); } }
第二种方法,结合了手势来实现listview的弹性效果,这里可以根据手势来进行更多的扩展,代码如下:
import android.content.context; import android.graphics.rect; import android.util.attributeset; import android.view.gesturedetector; import android.view.gesturedetector.ongesturelistener; import android.view.motionevent; import android.view.view; import android.view.animation.translateanimation; import android.widget.listview; /** * 具有弹性效果的listview。主要是实现父类dispatchtouchevent方法和ongesturelistener中onscroll方法。 * @author e */ public class flexiblelistview extends listview implements ongesturelistener{ private context context = null; private boolean outbound = false; private int distance; private int firstout; public flexiblelistview(context context, attributeset attrs) { super(context, attrs); this.context = context; } public flexiblelistview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); this.context = context; } public flexiblelistview(context context) { super(context); this.context = context; } gesturedetector lisgesturedetector = new gesturedetector(context, this); @override public boolean dispatchtouchevent(motionevent event) { int act = event.getaction(); if ((act == motionevent.action_up || act == motionevent.action_cancel) && outbound) { outbound = false; // scroll back } if (!lisgesturedetector.ontouchevent(event)) { outbound = false; } else { outbound = true; } rect rect = new rect(); getlocalvisiblerect(rect); translateanimation am = new translateanimation( 0, 0, -rect.top, 0); am.setduration(300); startanimation(am); scrollto(0, 0); return super.dispatchtouchevent(event); } @override public boolean ondown(motionevent e) { return false; } @override public void onshowpress(motionevent e) { } @override public boolean onsingletapup(motionevent e) { return false; } @override public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { int firstpos = getfirstvisibleposition(); int lastpos = getlastvisibleposition(); int itemcount = getcount(); // outbound top if (outbound && firstpos != 0 && lastpos != (itemcount - 1)) { scrollto(0, 0); return false; } view firstview = getchildat(firstpos); if (!outbound) firstout = (int) e2.getrawy(); if (firstview != null&& (outbound || (firstpos == 0 && firstview.gettop() == 0 && distancey < 0))) { // record the length of each slide distance = firstout - (int) e2.getrawy(); scrollto(0, distance / 2); return true; } // outbound bottom return false; } @override public void onlongpress(motionevent e) { } @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { return false; } }
以上两种常用android listview弹性效果的实现方法,整理出来,希望对大家有所帮助!