自定义组合控件之滑动item+删除

package com.hqzn.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ScrollView;
import android.widget.Scroller;
import android.widget.TextView;

/**
 * Created by Administrator on 2016/9/27.
 */
public class SidingItem extends FrameLayout{
    private View view;
    private TextView tv;
    private int viewWidth;
    private int tvWidth;
    private int hight;
    private Scroller scroller;
    public SidingItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        scroller=new Scroller(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        viewWidth=view.getMeasuredWidth();
        tvWidth=tv.getMeasuredWidth();
        hight=getHeight();
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        tv.layout(0,0,tvWidth,hight);
        view.layout(tvWidth,0,viewWidth+tvWidth,hight);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        tv= (TextView) getChildAt(0);
        view=getChildAt(1);
    }
    int lastx;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
       super.onTouchEvent(event);
        int eventx= (int) event.getRawX();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
             lastx= eventx;
                break;
            case MotionEvent.ACTION_MOVE:
                int disx=eventx-lastx;
                int toScrollX = getScrollX() - disx;
                System.out.println(toScrollX);
// 屏蔽非法值
                if (toScrollX < 0) {
                    toScrollX = 0;
                }else if(toScrollX > viewWidth){
                    toScrollX = viewWidth;
                }
// scrollTo(toScrollX, 0);// 也可以
                scrollTo(toScrollX, getScrollY());
// 重新付值
                lastx = eventx;
                break;
            case MotionEvent.ACTION_UP:
                int totallScrollX = getScrollX();
                if(totallScrollX < viewWidth/2){
                    System.out.println("totallScrollX < menuWidth/2");
                    closeMenu();
                }else{
                    System.out.println("totallScrollX >= menuWidth/2");
                    openMenu();
                }
                break;
        }
        return true;
    }

    private void closeMenu() {
        scroller.startScroll(getScrollX(),getScrollY(),0-getScrollX(),getScrollY());
        invalidate();// 会导致 执行 computeScroll
    }
    private void openMenu(){
        scroller.startScroll(getScrollX(),getScrollY(),viewWidth-getScrollX(),getScrollY());
        invalidate();// 会导致 执行 computeScroll
    }
    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.getCurrX(), scroller.getCurrY());
            System.out.println("滚动完成");
            invalidate();// 强制重绘制
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值