当前位置: 移动技术网 > IT编程>移动开发>Android > Android ListView实现仿iPhone实现左滑删除按钮的简单实例

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

西园寺,www.9887.cc,日本风土人情

需要自定义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>   /**<br>    * 这里的event是我们显示的从floatdellistview的ontouchevent里面传进来的,<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实现左滑删除按钮的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网