Android 自定义弹性ListView控件实例代码(三种方法)
程序员文章站
2024-02-18 23:41:10
关于在android中实现listview的弹性效果,有很多不同的方法,网上一搜,也有很多,下面贴出在项目中经常用到的两种实现listview弹性效果的方法(基本上拿来就可...
关于在android中实现listview的弹性效果,有很多不同的方法,网上一搜,也有很多,下面贴出在项目中经常用到的两种实现listview弹性效果的方法(基本上拿来就可以用),供大家参考:
弹性listview
第一种方法:
import android.content.context; import android.content.res.configuration; import android.util.attributeset; import android.util.displaymetrics; import android.view.view; import android.view.viewconfiguration; import android.widget.listview; import java.lang.reflect.field; import java.lang.reflect.modifier; /** * created by noah on 2016/1/16. */ public class bouncelistview extends listview { private static final float max_y_overscroll_distance = 200; private float mmaxyoverscrolldistance; public bouncelistview(context context) { this(context, null); } public bouncelistview(context context, attributeset attrs) { this(context, attrs, 0); } public bouncelistview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); initbouncelistview(); } private void initbouncelistview(){ final displaymetrics metrics = getcontext().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) { return super.overscrollby(deltax, deltay, scrollx, scrolly, scrollrangex, scrollrangey, maxoverscrollx, (int)mmaxyoverscrolldistance, istouchevent); } /** * 设置本app所有的listview弹性粒度 * @param ctx * @param size * @return */ public boolean configglobalmaxoverscrolldistance(context ctx,int size) { try { final displaymetrics metrics = ctx.getresources().getdisplaymetrics(); final float density = metrics.density; int value = (int) (density * size); mmaxyoverscrolldistance = value; viewconfiguration config = viewconfiguration.get(ctx); field moverscrolldistance = viewconfiguration.class.getdeclaredfield("moverscrolldistance"); if(!moverscrolldistance.isaccessible() || !modifier.ispublic(moverscrolldistance.getmodifiers())) { moverscrolldistance.setaccessible(true); } moverscrolldistance.setint(config,value); } catch (exception e) { e.printstacktrace(); return false; } return true; } }
第二种比较简单,好容易理解,只是动态改变了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; } }
以上给大家分享了几种比较常用的方法,小编整理出来的,希望对大家有所帮助。
上一篇: Java的Spring AOP详细讲解