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();
}
}
}
网友评论