使用ViewDragHelper实现简单的Drawerlayout

1.继承FrameLayou

public class MyDrawLayout extends FrameLayout {
    private ViewDragHelper viewDragHelper;
    private LinearLayout cehuaView;
    private LinearLayout contentView;
    private boolean isOpen = false;//是否开启状态

    public MyDrawLayout(@NonNull Context context) {
        super(context);
        init();
    }

    public MyDrawLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyDrawLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        viewDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                return child == cehuaView;//设置只有侧滑内容能滑动
            }

            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                if (child == cehuaView) {//判断侧滑块的滑动距离
                    if (left > 0) {
                        return 0;
                    }
                }
                return left;
            }

            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
                int topBound = getPaddingTop();
                int bottomBound = getHeight() - child.getHeight() - topBound;
                return Math.min(Math.max(top, topBound), bottomBound);
            }

            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                if (releasedChild == cehuaView) {
                    //判断如果滑动距离大于滑动模块的一半时候显示出来,没有的话收缩回去
                    if (releasedChild.getX() + releasedChild.getWidth() < releasedChild.getWidth() / 2) {
                        // TODO 隐藏
                        viewDragHelper.smoothSlideViewTo(releasedChild, -releasedChild.getWidth(), 0);
                        isOpen = false;
                    } else {
                        // TODO 显示
                        viewDragHelper.smoothSlideViewTo(releasedChild, 0, 0);
                        isOpen = true;
                    }
                    invalidate();
                }
            }

            @Override
            public void onEdgeTouched(int edgeFlags, int pointerId) {
                viewDragHelper.captureChildView(cehuaView, pointerId);
            }

            @Override
            public int getViewHorizontalDragRange(View child) {
                //当child里面有消耗事件的时候,需要返回大于0才能水平移动
                return 1;
            }
            @Override
            public int getViewVerticalDragRange(View child) {
                //当child里面有消耗事件的时候,需要返回大于0才能垂直移动
                return 1;
            }
        });
        //开启边界检测
        viewDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
    }

    @Override
    public void computeScroll() {
        if (viewDragHelper.continueSettling(true)) {
            invalidate();
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        //侧滑view
        cehuaView.layout(-cehuaView.getMeasuredWidth(), 0, 0, cehuaView.getMeasuredHeight());
        //内容view
        contentView.layout(0, 0, contentView.getMeasuredWidth(), contentView.getMeasuredHeight());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        cehuaView = (LinearLayout) getChildAt(1);
        contentView = (LinearLayout) getChildAt(0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return viewDragHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        viewDragHelper.processTouchEvent(event);
        return true;
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void openDrawer() {
        viewDragHelper.smoothSlideViewTo(cehuaView, 0, 0);
        isOpen = true;
        invalidate();
    }

    public void closeDrawer() {
        viewDragHelper.smoothSlideViewTo(cehuaView, -cehuaView.getWidth(), 0);
        isOpen = false;
        invalidate();
    }
}

2.在XML中使用

<com.example.user.myapplication.MyDrawLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mydraw"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_red_light">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="主内容" />

        <Button
            android:onClick="change"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开关" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:background="@android:color/holo_orange_light">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="侧滑内容" />
        <Button
            android:onClick="change"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开关" />

    </LinearLayout>

</com.example.user.myapplication.MyDrawLayout>

3.在代码中使用

public class MainActivity extends AppCompatActivity {
    private MyDrawLayout myDrawLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        myDrawLayout = (MyDrawLayout) findViewById(R.id.mydraw);
    }

    public void change(View view) {
        if(myDrawLayout.isOpen()) {
            myDrawLayout.closeDrawer();
        }else {
            myDrawLayout.openDrawer();
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,204评论 25 708
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,904评论 18 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,229评论 4 61
  • 做,有一个行为,为了定向实施。 想的再多不如一做。做的再多不如一想。想与做要统一。为了目的地而统一。定向后就做,如...
    鹿伟伦阅读 940评论 0 0
  • 撕心裂肺的哭泣后,看了《从你的全世界经过》中张萍说的话。突然就明白,我是多么的不自知。 他说:“我感觉吧,这姑娘有...
    杨123阅读 106评论 0 0