• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Android仿QQ微信侧滑删除效果

java 搞代码 4年前 (2022-01-09) 17次浏览 已收录 0个评论

仿QQ侧滑删除效果图

自定义listview

public class DragDelListView extends ListView {   private boolean moveable=false;  private boolean closed=true;  private float mDownX,mDownY;  private int mTouchPosition,oldPosition=-1;  private DragDelItem mTouchView,oldView;  private Context context;  public DragDelListView(Context context) {    super(context);    init(context);  }  public DragDelListView(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);    init(context);  }  public DragDelListView(Context context, AttributeSet attrs) {    super(context, attrs);    init(context);  }  private void init(Context context) {    this.context=context;  }    @Override  public boolean onTouchEvent(MotionEvent ev) {    switch (ev.getAction()) {    case MotionEvent.ACTION_DOWN:      mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY());      mTouchView=(DragDelItem)getChildAt(mTouchPosition - getFirstVisiblePosition());      mDownX = ev.getX();      mDownY=ev.getY();      if(oldPosition==mTouchPosition ||closed)      {        moveable=true;        mTouchView.mDownX =(int)mDownX;      }else      {        moveable=false;        if(oldView!=null)        {          oldView.smoothCloseMenu();        }      }      oldPosition=mTouchPosition;      oldView=mTouchView;      break;    case MotionEvent.ACTION_MOVE:      if (Math.abs(mDownX-ev.getX()) < Math.abs(mDownY-ev.getY()) * dp2px(2)) {        break;       }       if (moveable)       {        int dis = (int) (mTouchView.mDownX -ev.getX());        if(mTouchView.state==mTouchView.STATE_OPEN)          dis+=mTouchView.mMenuView.getWidth();        mTouchView.swipe(dis);        ev.setAction(MotionEvent.ACTION_CANCEL);      }       break;    case MotionEvent.ACTION_UP:       if (moveable)       {        if ((mTouchView.mDownX -ev.getX()) > (mTouchView.mMenuView.getWidth()/2)) {          // open          mTouchView.smoothOpenMenu();          closed=false;        } else {          // close          mTouchView.smoothCloseMenu();          closed=true;        }        ev.setAction(MotionEvent.ACTION_CANCEL);      }      break;    }    return super.onTouchEvent(ev);  }   private int dp2px(int dp) {    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,        getContext().getResources().getDisplayMetrics());  }  }

自定义滑动条目

public class DragDelItem extends LinearLayout {   public static final int STATE_CLOSE = 0;  public static final int STATE_OPEN = 1;  private View mContentView;  public View mMenuView;  public int mDownX;  public int state = STATE_CLOSE;  public boolean isFling;  private int mBaseX;  private Scroller scroll;   public DragDelItem(View contentView, View menuView) {    super(contentView.getContext());    scroll=new Scroller(getContext());    mContentView = contentView;    mMenuView = menuView;    init();  }   private DragDelItem(Context context, AttributeSet attrs) {    super(context, attrs);  }   private DragDelItem(Context context) {    super(context);  }    private void init() {    setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,        LayoutParams.WRAP_CONTENT));    LayoutParams contentParams = new LayoutParams(        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);    mContentView.setLayoutParams(contentParams);     mMenuView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,        LayoutParams.WRAP_CONTENT));     addView(mContentView);    addView(mMenuView);   }    public void swipe(int dis) {    if (dis > mMenuView.getWidth()) {      dis = mMenuView.getWidth();    }    if (dis < 0) {      dis = 0;    }    mContentView.layout(-dis, mContentView.getTop(),        mContentView.getWidth() - dis, getMeasuredHeight());    mMenuView.layout(mContentView.getWidth() - dis, mMenuView.getTop(),        mContentView.getWidth() + mMenuView.getWidth() - dis,        mMenuView.getBottom());  }   @Override  public void computeScroll() {    if (state == STATE_OPEN) {      if (scroll.computeScrollOffset()) {        swipe(scroll.getCurrX());        postInvalidate();      }    } else {      if (scroll.computeScrollOffset()) {        swipe(mBaseX - scroll.getCurrX());        postInvalidate();      }    }  }   public void smoothCloseMenu() {    state = STATE_CLOSE;    mBaseX = -mContentView.getLeft();    scroll.startScroll(0, 0, mBaseX, 0, 350);    postInvalidate();  }   public void smoothOpenMenu() {     state = STATE_OPEN;    scroll.startScroll(-mContentView.getLeft(), 0,        mMenuView.getWidth()/2, 0, 350);    postInvalidate();  }   @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    super.onMeasure(widthMeasureSpec, heightMeasureSpec);     mMenuView.measure(MeasureSpec.makeMeasureSpec(0,        MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(        getMeasuredHeight(), MeasureSpec.EXACTLY));    mContentView.measure(MeasureSpec.makeMeasureSpec(0,        MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(<a>本文来源gao($daima.com搞@代@#码8网^</a>        getMeasuredHeight(), MeasureSpec.EXACTLY));   }   @Override  protected void onLayout(boolean changed, int l, int t, int r, int b) {    mContentView.layout(0, 0, getMeasuredWidth(),        mContentView.getMeasuredHeight());    mMenuView.layout(getMeasuredWidth(), 0,        getMeasuredWidth() + mMenuView.getMeasuredWidth(),        mContentView.getMeasuredHeight());   } }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Android仿QQ微信侧滑删除效果
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址