Skip to content

Commit

Permalink
Bottom弹窗增加启用手势交互的开关
Browse files Browse the repository at this point in the history
  • Loading branch information
junixapp committed Dec 27, 2018
1 parent fc24bec commit 0753699
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 44 deletions.
Expand Up @@ -5,6 +5,7 @@
import android.view.View;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupAnimation;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.interfaces.OnInputConfirmListener;
import com.lxj.xpopup.interfaces.OnSelectListener;
Expand Down Expand Up @@ -120,14 +121,8 @@ public void onSelect(int position, String text) {
.show();
break;
case R.id.btnShowLoading:
XPopup.get(getActivity()).asLoading()
XPopup.get(getActivity()).asLoading(/*"正在加载中"*/)
.show();
// new Handler().postDelayed(new Runnable() {
// @Override
// public void run() {
// XPopup.get(getActivity()).dismiss();
// }
// }, 2000);
break;
case R.id.btnShowBottomList:
XPopup.get(getActivity()).asBottomList("请选择一项",new String[]{"条目1", "条目2", "条目3", "条目4","条目5"},
Expand All @@ -141,7 +136,6 @@ public void onSelect(int position, String text) {
break;
case R.id.btnShowBottomListWithCheck:
XPopup.get(getActivity()).asBottomList("请选择一项",new String[]{"条目1", "条目2", "条目3", "条目4","条目5"},
null, 3,
new OnSelectListener() {
@Override
public void onSelect(int position, String text) {
Expand Down
18 changes: 13 additions & 5 deletions library/src/main/java/com/lxj/xpopup/XPopup.java
Expand Up @@ -348,6 +348,7 @@ public XPopup asLoading(String title) {
.setTitle(title);
return this;
}

public XPopup asLoading() {
return asLoading(null);
}
Expand All @@ -359,25 +360,32 @@ public XPopup asLoading() {
* @param title 标题,可以不传,不传则不显示
* @param data 显示的文本数据
* @param iconIds 图标的id数组,可以没有
* @param checkedPosition 选中的位置,传-1为不选中
* @param selectListener 选中条目的监听器
* @return
*/
public XPopup asBottomList(String title, String[] data, int[] iconIds, int checkedPosition, OnSelectListener selectListener) {
public XPopup asBottomList(String title, String[] data, int[] iconIds, int checkedPosition, boolean enableGesture, OnSelectListener selectListener) {
if (popupStatus != PopupStatus.Dismiss) return this;
position(PopupType.Bottom);
this.popupView = new BottomListPopupView(contextRef.get())
.setStringData(title, data, iconIds)
.setCheckedPosition(checkedPosition)
.setOnSelectListener(selectListener);
.setOnSelectListener(selectListener)
.enableGesture(enableGesture);
return this;
}

public XPopup asBottomList(String title, String[] data, OnSelectListener selectListener) {
return asBottomList(title, data, null, -1, selectListener);
return asBottomList(title, data, null, -1,true, selectListener);
}

public XPopup asBottomList(String title, String[] data, int[] iconIds, OnSelectListener selectListener) {
return asBottomList(title, data, iconIds, -1, selectListener);
return asBottomList(title, data, iconIds, -1, true, selectListener);
}
public XPopup asBottomList(String title, String[] data, int[] iconIds, int checkedPosition, OnSelectListener selectListener) {
return asBottomList(title, data, iconIds, checkedPosition, true, selectListener);
}
public XPopup asBottomList(String title, String[] data, int[] iconIds, boolean enableGesture, OnSelectListener selectListener) {
return asBottomList(title, data, iconIds, -1, enableGesture, selectListener);
}


Expand Down
6 changes: 3 additions & 3 deletions library/src/main/java/com/lxj/xpopup/core/BasePopupView.java
Expand Up @@ -35,7 +35,7 @@ public abstract class BasePopupView extends FrameLayout implements PopupInterfac

protected PopupAnimator popupContentAnimator;
protected PopupAnimator shadowBgAnimator;
int touchSlop;
private int touchSlop;
public BasePopupView(@NonNull Context context) {
super(context);
touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Expand Down Expand Up @@ -252,8 +252,8 @@ public interface DismissProxy{
void dismiss();
}

float x, y;
long downTime;
private float x, y;
private long downTime;
@Override
public boolean onTouchEvent(MotionEvent event) {
// 如果自己接触到了点击,并且不在PopupContentView范围内点击,则进行判断是否是点击事件
Expand Down
34 changes: 26 additions & 8 deletions library/src/main/java/com/lxj/xpopup/core/BottomPopupView.java
Expand Up @@ -2,10 +2,8 @@

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.lxj.xpopup.R;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.util.XPopupUtils;
Expand All @@ -17,6 +15,7 @@
*/
public class BottomPopupView extends BasePopupView {
SmartDragLayout bottomPopupContainer;
boolean enableGesture = true; //是否启用手势交互,默认启用
public BottomPopupView(@NonNull Context context) {
super(context);
bottomPopupContainer = findViewById(R.id.bottomPopupContainer);
Expand All @@ -32,6 +31,7 @@ protected int getPopupLayoutId() {
@Override
protected void initPopupContent() {
super.initPopupContent();
bottomPopupContainer.enableGesture(enableGesture);
XPopupUtils.widthAndHeight(getPopupImplView(),getMaxWidth(), getMaxHeight());

bottomPopupContainer.setOnCloseListener(new SmartDragLayout.OnCloseListener() {
Expand All @@ -51,12 +51,20 @@ public void onClick(View v) {

@Override
public void doShowAnimation() {
bottomPopupContainer.open();
if(enableGesture){
bottomPopupContainer.open();
}else {
super.doShowAnimation();
}
}

@Override
public void doDismissAnimation() {
bottomPopupContainer.close();
if(enableGesture){
bottomPopupContainer.close();
}else {
super.doDismissAnimation();
}
}

/**
Expand All @@ -65,19 +73,24 @@ public void doDismissAnimation() {
*/
@Override
public int getAnimationDuration() {
return 0;
return enableGesture? 0: super.getAnimationDuration();
}

@Override
protected PopupAnimator getPopupAnimator() {
// 移除默认的动画器
return null;
return enableGesture ? null : super.getPopupAnimator();
}

@Override
public void dismiss() {
// 关闭Drawer,由于Drawer注册了关闭监听,会自动调用dismiss
bottomPopupContainer.close();
if(enableGesture){
// 关闭Drawer,由于Drawer注册了关闭监听,会自动调用dismiss
bottomPopupContainer.close();
}else {
super.dismiss();
}

}

/**
Expand All @@ -93,4 +106,9 @@ protected int getMaxWidth() {
return popupInfo.maxWidth==0 ? XPopupUtils.getWindowWidth(getContext())
: popupInfo.maxWidth;
}

public BottomPopupView enableGesture(boolean enableGesture){
this.enableGesture = enableGesture;
return this;
}
}
52 changes: 32 additions & 20 deletions library/src/main/java/com/lxj/xpopup/widget/SmartDragLayout.java
Expand Up @@ -8,12 +8,10 @@
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.OverScroller;
import android.widget.Scroller;

import com.lxj.xpopup.animator.ShadowBgAnimator;
import com.lxj.xpopup.util.XPopupUtils;
Expand All @@ -27,7 +25,8 @@ public class SmartDragLayout extends CardView implements NestedScrollingParent {
private View child;
OverScroller scroller;
ShadowBgAnimator bgAnimator = new ShadowBgAnimator();
NestedScrollingChildHelper childHelper;
boolean enableGesture = true;//是否启用手势

public SmartDragLayout(Context context) {
this(context, null);
}
Expand All @@ -38,9 +37,11 @@ public SmartDragLayout(Context context, AttributeSet attrs) {

public SmartDragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
scroller = new OverScroller(context);
setCardElevation(XPopupUtils.dp2px(context, 10));
setBackgroundColor(Color.TRANSPARENT);
if (enableGesture) {
scroller = new OverScroller(context);
setCardElevation(XPopupUtils.dp2px(context, 10));
setBackgroundColor(Color.TRANSPARENT);
}
}

int maxY;
Expand All @@ -50,8 +51,6 @@ public SmartDragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
public void onViewAdded(View c) {
super.onViewAdded(c);
child = c;

childHelper = new NestedScrollingChildHelper(child);
}

@Override
Expand All @@ -63,9 +62,14 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
// horizontal center
int l = getMeasuredWidth() / 2 - child.getMeasuredWidth() / 2;
child.layout(l, getMeasuredHeight(), l + child.getMeasuredWidth(), getMeasuredHeight() + child.getMeasuredHeight());
if (enableGesture) {
// horizontal center
child.layout(l, getMeasuredHeight(), l + child.getMeasuredWidth(), getMeasuredHeight() + child.getMeasuredHeight());
} else {
// like bottom gravity
child.layout(l, getMeasuredHeight() - child.getMeasuredHeight(), l + child.getMeasuredWidth(), getMeasuredHeight());
}
}

float touchX, touchY;
Expand All @@ -83,9 +87,11 @@ public boolean onTouchEvent(MotionEvent event) {
downTime = System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
int dy = (int) (event.getY() - touchY);
scrollTo(getScrollX(), getScrollY() - dy);
touchY = event.getY();
if (enableGesture) {
int dy = (int) (event.getY() - touchY);
scrollTo(getScrollX(), getScrollY() - dy);
touchY = event.getY();
}
break;
case MotionEvent.ACTION_UP:
finishScroll();
Expand All @@ -105,11 +111,12 @@ public boolean onTouchEvent(MotionEvent event) {
}

private void finishScroll() {
int threshold = isScrollUp ? (maxY - minY) / 3 : (maxY - minY) * 2 / 3;

int dy = (getScrollY() > threshold ? maxY : minY) - getScrollY();
scroller.startScroll(getScrollX(), getScrollY(), 0, dy, 400);
invalidate();
if (enableGesture) {
int threshold = isScrollUp ? (maxY - minY) / 3 : (maxY - minY) * 2 / 3;
int dy = (getScrollY() > threshold ? maxY : minY) - getScrollY();
scroller.startScroll(getScrollX(), getScrollY(), 0, dy, 400);
invalidate();
}
}

boolean isScrollUp;
Expand Down Expand Up @@ -154,7 +161,7 @@ public void close() {

@Override
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL && enableGesture;
}

@Override
Expand All @@ -165,14 +172,15 @@ public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes
public void onStopNestedScroll(View target) {
finishScroll();
}

@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
scrollTo(getScrollX(), getScrollY() + dyUnconsumed);
}

@Override
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
if (dy>0) {
if (dy > 0) {
//scroll up
int newY = getScrollY() + dy;
if (newY < maxY) {
Expand All @@ -197,6 +205,10 @@ public int getNestedScrollAxes() {
return ViewCompat.SCROLL_AXIS_VERTICAL;
}

public void enableGesture(boolean enableGesture) {
this.enableGesture = enableGesture;
}

private OnCloseListener listener;

public void setOnCloseListener(OnCloseListener listener) {
Expand Down

0 comments on commit 0753699

Please sign in to comment.