Skip to content

Commit

Permalink
优化Drawer弹窗的fling滑动
Browse files Browse the repository at this point in the history
  • Loading branch information
junixapp committed Feb 20, 2019
1 parent 2b35745 commit 03e2762
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public void onItemSelected(AdapterView<?> parent, View view, final int position,
@Override
public void run() {
XPopup.get(getContext())
// .popupAnimation(datas[position])
.asCustom(new CustomPopup2(getContext()))
.popupAnimation(datas[position])
.asCustom(new CustomPopup(getContext()))
// .setWidthAndHeight(XPopupUtils.getWindowWidth(getContext()),XPopupUtils.getWindowHeight(getContext()))
.show();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,10 @@ public void onSelect(int position, String text) {
@Override
public void onPause() {
super.onPause();
Log.e("tag", "onPause");
}

@Override
public void onStop() {
super.onStop();
Log.e("tag", "onStop");
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/custom_popup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eee"
android:background="#fff"
android:orientation="vertical"
android:padding="20dp">

Expand Down
49 changes: 41 additions & 8 deletions library/src/main/java/com/lxj/xpopup/widget/PopupDrawerLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
Expand Down Expand Up @@ -61,14 +62,38 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
child.layout(getMeasuredWidth(), 0, getMeasuredWidth() + child.getMeasuredWidth(), getMeasuredHeight());
}
hasLayout = true;
}else {
} else {
child.layout(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
}
}

// float x,y;
// @Override
// public boolean dispatchTouchEvent(MotionEvent ev) {
// switch (ev.getAction()) {
// case MotionEvent.ACTION_DOWN:
// x = ev.getX();
// y = ev.getY();
// break;
// case MotionEvent.ACTION_MOVE:
// float dx = ev.getX() - x;
// float dy = ev.getY() - y;
// if(Math.abs(dx) > Math.abs(dy))
//
// break;
// case MotionEvent.ACTION_UP:
// case MotionEvent.ACTION_CANCEL:
//
// break;
// }
// return super.dispatchTouchEvent(ev);
// }

boolean isIntercept = false;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return dragHelper.shouldInterceptTouchEvent(ev);
isIntercept = dragHelper.shouldInterceptTouchEvent(ev);
return isIntercept;
}

@Override
Expand Down Expand Up @@ -125,15 +150,23 @@ public void onViewPositionChanged(@NonNull View changedView, int left, int top,
public void onViewReleased(@NonNull View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
int centerLeft = 0;
int finalLeft = 0;
if (position == Position.Left) {
centerLeft = -child.getMeasuredWidth() / 2;
int finalLeft = child.getLeft() < centerLeft ? -child.getMeasuredWidth() : 0;
dragHelper.smoothSlideViewTo(releasedChild, finalLeft, releasedChild.getTop());
if (xvel < -1000) {
finalLeft = -child.getMeasuredWidth();
} else {
centerLeft = -child.getMeasuredWidth() / 2;
finalLeft = child.getLeft() < centerLeft ? -child.getMeasuredWidth() : 0;
}
} else {
centerLeft = getMeasuredWidth() - child.getMeasuredWidth() / 2;
int finalLeft = releasedChild.getLeft() < centerLeft ? getMeasuredWidth() - child.getMeasuredWidth() : getMeasuredWidth();
dragHelper.smoothSlideViewTo(releasedChild, finalLeft, releasedChild.getTop());
if (xvel > 1000) {
finalLeft = getMeasuredWidth();
} else {
centerLeft = getMeasuredWidth() - child.getMeasuredWidth() / 2;
finalLeft = releasedChild.getLeft() < centerLeft ? getMeasuredWidth() - child.getMeasuredWidth() : getMeasuredWidth();
}
}
dragHelper.smoothSlideViewTo(releasedChild, finalLeft, releasedChild.getTop());
ViewCompat.postInvalidateOnAnimation(PopupDrawerLayout.this);
}
};
Expand Down

0 comments on commit 03e2762

Please sign in to comment.