
关于在AndroID中实现ListVIEw的d性效果,有很多不同的方法,网上一搜,也有很多,下面贴出在项目中经常用到的两种实现ListVIEwd性效果的方法(基本上拿来就可以用),供大家参考:
第一种比较简单,好容易理解,只是动态改变了ListVIEw在Y轴上的可移动距离,代码如下:
import androID.content.Context; import androID.util.AttributeSet; import androID.util.displayMetrics; import androID.Widget.ListVIEw; /** * d性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的d性效果,这里可以根据手势来进行更多的扩展,代码如下:
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; /** * 具有d性效果的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,attrs); this.context = context; } public FlexibleListVIEw(Context context,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,-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,float veLocityX,float veLocityY) { return false; } } 以上两种常用AndroID ListVIEwd性效果的实现方法,整理出来,希望对大家有所帮助!
总结以上是内存溢出为你收集整理的Android ListViewd性效果的实现方法全部内容,希望文章能够帮你解决Android ListViewd性效果的实现方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)