自定义view实现抽屉布局

public class MainActivity extends AppCompatActivity {

    private SlidingView slidingView;
    private View menuView;
    private View contentView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }


    private void init() {
        slidingView = findViewById(R.id.sv);
        //布局填充器
        menuView = LayoutInflater.from(this).inflate(R.layout.layout_menu,null);
        contentView = LayoutInflater.from(this).inflate(R.layout.layout_content,null);
        //给自定义的view添加布局
        slidingView.addContent(contentView);
        slidingView.addMenu(menuView);
    }

}
public class SlidingView extends FrameLayout {

    private LinearLayout contentLinear;
    private LinearLayout menunLinear;
    private int width = 400;
    private PointF lastPointF = new PointF();

    public SlidingView(Context context,AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        //写一个父布局
        contentLinear = new LinearLayout(getContext());
        FrameLayout.LayoutParams contentLp = new FrameLayout.LayoutParams(
                LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        contentLinear.setOrientation(LinearLayout.VERTICAL);
        contentLinear.setLayoutParams(contentLp);
        contentLinear.setBackgroundColor(Color.RED);
        //记住要添加view
        addView(contentLinear);

        //写一个抽屉布局
        menunLinear = new LinearLayout(getContext());
        FrameLayout.LayoutParams menulp = new FrameLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT);
        //设置为负数 使抽屉刚好贴在布局左边
        menulp.leftMargin = -width;
        menunLinear.setOrientation(LinearLayout.VERTICAL);
        menunLinear.setBackgroundColor(Color.BLUE);
        menunLinear.setLayoutParams(menulp);
        //记住添加!!!
        addView(menunLinear);
    }
    //写一个方法用来调用添加父布局
    public void addContent(View view){
        if(view == null){
            return;
        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(lp);
        //父布局添加传过来的view
        contentLinear.addView(view);
    }
    //调用添加抽屉
    public void addMenu(View view){
        if(view == null){
            return;
        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(lp);
        menunLinear.addView(view);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //按下
        if(ev.getAction() == MotionEvent.ACTION_DOWN){
            //把指针移到坐标位置
            lastPointF.x = ev.getX();
            lastPointF.y = ev.getY();
//            Log.i("TAG","X:--->"+lastPointF.x+"Y:--->"+lastPointF.y);
        // 拖动
        }else if(ev.getAction() == MotionEvent.ACTION_MOVE){
            //new一个新的坐标 作为最新坐标
            PointF pointF = new PointF(ev.getX(), ev.getY());
            //用移动结束时的坐标减去最开始移动时的坐标
            float distanceX = Math.abs(pointF.x - lastPointF.x);
            float distanceY = Math.abs(pointF.y - lastPointF.y);
            //判断是上滑还是下滑 y轴*2是因为人们手指在滑动到最后时更多是x轴移动
            //所以为了平衡 就给y轴*2
            if((2*distanceY) > distanceX){
                return super.dispatchTouchEvent(ev);
            }
            //x轴的移动长度 随意大小
            if(distanceX >= 10){
                //获取抽屉布局对象
                FrameLayout.LayoutParams lp = (LayoutParams) menunLinear.getLayoutParams();
                //判断是拉出来还是拉回去 如果为负 则把数值转成正
                if((pointF.x - lastPointF.x) < 0){
                    distanceX = -distanceX;
                }
                //lp.leftMargin是子布局的左边框
                //之前的左边框我们设置成了负数
                //现在用她它加我们拖动的距离
                lp.leftMargin = (int) (lp.leftMargin+distanceX);
                //如果左边框大于0 让它全部出来
                if(lp.leftMargin > 0){
                    lp.leftMargin = 0;
                    return true;
                }
                //这个是防止不停的往回拖 lp.leftMargin的值不断添加 原本是-400 可能就拖成了-500
                //导致之后想拖出来会出问题
                if(lp.leftMargin < -width){
                    lp.leftMargin = -width;
                    return true;
                }
                menunLinear.requestLayout();
                lastPointF.x = pointF.x;
                lastPointF.y = pointF.y;
            }

        //抬起
        }else if(ev.getAction() == MotionEvent.ACTION_UP){
            FrameLayout.LayoutParams lp = (LayoutParams) menunLinear.getLayoutParams();
            //判断拖动的长度 防止误触 就是你只拖出来一点点 就显示全部
            if(lp.leftMargin < -width/2){
                lp.leftMargin = -width;
            }else{
                lp.leftMargin = 0;
            }
            menunLinear.requestLayout();
        }
        return super.dispatchTouchEvent(ev);
    }
}

你可能感兴趣的:(android,android)