广告
返回顶部
首页 > 资讯 > 移动开发 >Android ListView实现仿iPhone实现左滑删除按钮的简单实例
  • 200
分享到

Android ListView实现仿iPhone实现左滑删除按钮的简单实例

listviewiphone按钮Android 2022-06-06 07:06:09 200人浏览 独家记忆
摘要

需要自定义ListView。这里就交FloatDelListView吧。 复写onTouchEvent方法。如下: @Override public boolean on

需要自定义ListView。这里就交FloatDelListView吧。

复写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>          // 计算水平和垂直方向移动速度 
        velocityTracker.computeCurrentVelocity(1000); 
        float velocityX = Math.abs(velocityTracker.getXVelocity()); 
        float velocityY = Math.abs(velocityTracker.getYVelocity()); 
<BR>          // 水平移动距离 
        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>          // 如果水平滑动距离大于零,并且水平滑动速率比垂直大,说明是水平滑动 
        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>   // 当前视图在列表中的索引,在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>   // 用来显示或者隐藏删除按钮。 
  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>   
  @Override
  public boolean onTouchEvent(MotionEvent event) { 
    switch (MotionEventCompat.getActionMasked(event)) { 
      case MotionEvent.ACTION_MOVE:<BR>          // 获取删除按钮对象,视图layout中必须要有id为del_button的Button标签 
        Button view = (Button) findViewById(R.id.del_button); 
        view.setText(R.string.del);<BR>          // 设置Button的MarginLayoutParams,当然可以做成各种动作,比如渐隐之类的显示出来。 
        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实现仿iPhone实现左滑删除按钮的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程网。

您可能感兴趣的文章:android为ListView每个Item上面的按钮添加事件android中在Activity中响应ListView内部按钮的点击事件的两种方法Android自定义View制作动态炫酷按钮实例解析Android自定义View实现拖动选择按钮Android ListView ImageView实现单选按钮实例Android自定义View之圆形进度条式按钮Android自定义View实现开关按钮Android基于ImageView绘制的开关按钮效果示例自定义滑动按钮为例图文剖析Android自定义View绘制Android自定义View实现可展开、会呼吸的按钮


--结束END--

本文标题: Android ListView实现仿iPhone实现左滑删除按钮的简单实例

本文链接: https://www.lsjlt.com/news/24228.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作