admin
2021-05-17 06a80d5c4b3a971cdc1ca3d91717ec3f6e03a443
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.tejia.lijin.app.util;
 
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
 
public class SlidingMenu extends HorizontalScrollView {
 
    //是否第一次初始化界面
    private boolean once;
 
    //屏幕宽度
    private int mScreenWidth;
 
    //可以显示下一个界面的最小滑动距离
    private int mScrollWidth;
 
    //下一个界面的宽度
    private int mExtraViewWidth = 400;
 
    public SlidingMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScreenWidth = getResources().getDisplayMetrics().widthPixels;
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /**
         * 显示的设置一个宽度
         */
        if (!once) {
            LinearLayout wrapper = (LinearLayout) getChildAt(0);
            ViewGroup contentView = (ViewGroup) wrapper.getChildAt(0);
            ViewGroup extraView = (ViewGroup) wrapper.getChildAt(1);
            mScrollWidth = mExtraViewWidth / 3;
            contentView.getLayoutParams().width = mScreenWidth;
            extraView.getLayoutParams().width = mExtraViewWidth;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
    }
 
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            // 隐藏下一个界面
            this.scrollTo(HorizontalScrollView.FOCUS_RIGHT, 0);
            once = true;
        }
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            // Up时,进行判断,如果显示区域大于菜单宽度一半则完全显示,否则隐藏
            case MotionEvent.ACTION_UP:
                int scrollX = getScrollX();
                if (scrollX > mScrollWidth)
                    this.smoothScrollTo(mScreenWidth - mExtraViewWidth, 0);
                else
                    this.smoothScrollTo(0, 0);
                return true;
        }
        return super.onTouchEvent(ev);
    }
 
}