Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android ListView彈性效果的實現方法

Android ListView彈性效果的實現方法

編輯:關於Android編程

關於在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彈性效果的實現方法,整理出來,希望對大家有所幫助!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved