欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android ListView實(shí)現(xiàn)仿iPhone實(shí)現(xiàn)左滑刪除按鈕的簡(jiǎn)單實(shí)例

 更新時(shí)間:2016年08月23日 11:48:17   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇Android ListView實(shí)現(xiàn)仿iPhone實(shí)現(xiàn)左滑刪除按鈕的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

需要自定義ListView。這里就交FloatDelListView吧。

復(fù)寫onTouchEvent方法。如下:

@Override
  public boolean onTouchEvent(MotionEvent ev) { 
    switch (ev.getAction()) { 
      case MotionEvent.ACTION_DOWN:<BR>          // 獲取按下的條目視圖(child view) 
        int childCount = getChildCount(); 
        int[] listViewCoords = new int[2]; 
        getLocationOnScreen(listViewCoords); 
        int x = (int) ev.getRawX() - listViewCoords[0]; 
        int y = (int) ev.getRawY() - listViewCoords[1]; 
        for (int i = 0; i < childCount; i++) { 
          downChild = getChildAt(i); // 
          Rect rect = new Rect(); 
          assert downChild != null; 
          downChild.getHitRect(rect); 
 
          int childPosition = getPositionForView(downChild); 
 
          if (rect.contains(x, y)) { 
            downX = ev.getRawX(); 
            int downPosition = childPosition; 
 
            velocityTracker = VelocityTracker.obtain(); 
            assert velocityTracker != null; 
            velocityTracker.addMovement(ev); 
            break; 
          } 
        } 
        isSwipe = false; 
        break; 
      case MotionEvent.ACTION_MOVE: 
        velocityTracker.addMovement(ev);<BR>          // 計(jì)算水平和垂直方向移動(dòng)速度 
        velocityTracker.computeCurrentVelocity(1000); 
        float velocityX = Math.abs(velocityTracker.getXVelocity()); 
        float velocityY = Math.abs(velocityTracker.getYVelocity()); 
<BR>          // 水平移動(dòng)距離 
        float deltaX = ev.getRawX() - downX; 
        float deltaMode = Math.abs(deltaX); 
        if (deltaX > 150) {// right swipe(右滑) 
          isSwipeToLeft = false; 
        } else if (deltaX < -150) {// left swipe(左滑) 
          isSwipeToLeft = true; 
        }<BR>          // 如果水平滑動(dòng)距離大于零,并且水平滑動(dòng)速率比垂直大,說(shuō)明是水平滑動(dòng) 
        if (deltaMode > 0 && velocityY < velocityX) {<BR>            // 這里的FloatDelButtonLayout是自定義的LinearLayout。 
          ((FloatDelButtonLayout) downChild).showDelButton(ev, isSwipeToLeft); 
          isSwipe = true; 
        } 
        break; 
      case MotionEvent.ACTION_CANCEL: 
      case MotionEvent.ACTION_UP: 
        downChild.setSelected(false); 
        if (isSwipe) { 
          isSwipe = false; 
          return true; 
        } 
        break; 
    } 
    return super.onTouchEvent(ev); 
  }

FloatDelButtonLayou.java :

public class FloatDelButtonLayout extends LinearLayout { 
<BR>   // 提供刪除按鈕的接口 
  private OnDelListener delListener; 
<BR>   // 當(dāng)前視圖在列表中的索引,在delListener中使用 
  private int index; 
<BR>   // 右滑 還是 左滑?<BR>  private boolean isSwipeToLeft;<BR> 
  public void setOnDelListener(OnDelListener listener, int i) { 
    delListener = listener; 
    index = i; 
  } 
 
  public FloatDelButtonLayout(Context context) { 
    super(context, null); 
  } 
 
  public FloatDelButtonLayout(Context context, AttributeSet attrs) { 
    super(context, attrs, 0); 
  } 
 
  public FloatDelButtonLayout(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
  } 
<BR>   // 用來(lái)顯示或者隱藏刪除按鈕。 
  public void showDelButton(MotionEvent ev, boolean isSwipeToLeft) { 
    this.isSwipeToLeft = isSwipeToLeft; 
    onTouchEvent(ev); 
  } 
 
  private OnClickListener clickDel = new OnClickListener() { 
    @Override
    public void onClick(View v) { 
      delListener.onDel(index); 
    } 
  }; 
<BR>   /**<BR>    * 這里的event是我們顯示的從FloatDelListView的onTouchEvent里面?zhèn)鬟M(jìn)來(lái)的,<BR>   */
  @Override
  public boolean onTouchEvent(MotionEvent event) { 
    switch (MotionEventCompat.getActionMasked(event)) { 
      case MotionEvent.ACTION_MOVE:<BR>          // 獲取刪除按鈕對(duì)象,視圖layout中必須要有id為del_button的Button標(biāo)簽 
        Button view = (Button) findViewById(R.id.del_button); 
        view.setText(R.string.del);<BR>          // 設(shè)置Button的MarginLayoutParams,當(dāng)然可以做成各種動(dòng)作,比如漸隱之類的顯示出來(lái)。 
        MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams(); 
        assert layoutParams != null; 
        if (isSwipeToLeft) { 
          view.setVisibility(View.VISIBLE); 
          view.setOnClickListener(clickDel); 
          layoutParams.leftMargin = -200; 
        } else { 
          view.setVisibility(View.GONE); 
          layoutParams.leftMargin = 0; 
        } 
        view.setLayoutParams(layoutParams); 
        invalidate(); 
        break; 
    } 
    return super.onTouchEvent(event); 
  } 
 
  public interface OnDelListener { 
    void onDel(int i); 
  } 
}

以上這篇Android ListView實(shí)現(xiàn)仿iPhone實(shí)現(xiàn)左滑刪除按鈕的簡(jiǎn)單實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論