Skip to content

Commit

Permalink
修复弹窗由于状态错误导致无法关闭的Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
junixapp committed Feb 22, 2019
1 parent f326a6b commit 5907d9b
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 100 deletions.
Binary file modified app/release/app-release.apk
Binary file not shown.
27 changes: 1 addition & 26 deletions app/src/main/java/com/lxj/xpopupdemo/DemoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,15 @@
* Create by lxj, at 2019/2/2
*/
public class DemoActivity extends AppCompatActivity {
RecyclerView recycler_view;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
recycler_view = findViewById(R.id.recycler_view);
recycler_view.setLayoutManager(new LinearLayoutManager(this));
ArrayList<String> data = new ArrayList<>();
for (int i = 0; i < 16; i++) {
data.add(""+i);
}
recycler_view.setAdapter(new CommonAdapter<String>(android.R.layout.simple_list_item_1, data) {
@Override
protected void convert(@NonNull ViewHolder holder, @NonNull String s, int position) {
holder.setText(android.R.id.text1, s);
}
});

findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
XPopup.get(DemoActivity.this).asBottomList("我是标题", new String[]{"aaaa", "bbbb", "bbbb", "ddddd"}, new OnSelectListener() {
@Override
public void onSelect(int position, String text) {
Toast.makeText(DemoActivity.this, text, Toast.LENGTH_LONG).show();
}
}).show();
}
});


XPopup.get(DemoActivity.this).asLoading().show("c");

XPopup.get(this).autoDismiss(false).asBottomList("haha", new String[]{"aaaa", "bbbb", "bbbb", "ddddd"}, new OnSelectListener() {
XPopup.get(this).autoDismiss(false).asBottomList("haha", new String[]{"点我显示弹窗", "点我显示弹窗", "点我显示弹窗", "点我显示弹窗"}, new OnSelectListener() {
@Override
public void onSelect(int position, String text) {
Toast.makeText(DemoActivity.this, text, Toast.LENGTH_LONG).show();
Expand Down
25 changes: 16 additions & 9 deletions app/src/main/java/com/lxj/xpopupdemo/custom/ZhihuCommentPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.lxj.easyadapter.CommonAdapter;
Expand Down Expand Up @@ -47,22 +48,28 @@ protected void convert(@NonNull ViewHolder holder, @NonNull String s, int positi
commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.SimpleOnItemClickListener(){
@Override
public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
//不要直接这样做,会导致消失动画未执行完就跳转界面,不流畅。可以将消失后的逻辑移到onDismiss回调方法中
// dismiss();
// getContext().startActivity(new Intent(getContext(), DemoActivity.class));
//上面的代码是一个不好的示例,为了得到最佳体验,您可以等dismiss动画完全结束去执行一些东西,而不是立即就执行。
//像下面这样:
dismiss(new Runnable() {
@Override
public void run() {
getContext().startActivity(new Intent(getContext(), DemoActivity.class));
}
});
// getContext().startActivity(new Intent(getContext(), DemoActivity.class))
dismiss();
}
});
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(commonAdapter);
}

//完全可见执行
@Override
protected void onShow() {
super.onShow();
}

//完全消失执行
@Override
protected void onDismiss() {
getContext().startActivity(new Intent(getContext(), DemoActivity.class));
}

@Override
protected int getMaxHeight() {
return (int) (XPopupUtils.getWindowHeight(getContext())*.85f);
Expand Down
21 changes: 2 additions & 19 deletions app/src/main/res/layout/activity_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,12 @@
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:text="测试布局焦点获取"
android:text="测试多弹窗"
android:textSize="20sp"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="200dp" />
android:layout_height="wrap_content" />

<Space
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@android:color/transparent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:paddingStart="25pt"
android:paddingEnd="25pt" />

<Button
android:id="@+id/btn"
android:text="delete"
android:layout_width="match_parent"
android:layout_height="200dp" />

</android.support.v7.widget.LinearLayoutCompat>
</android.support.v4.widget.NestedScrollView>
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ publish {
userOrg = 'li-xiaojun'
groupId = 'com.lxj'
artifactId = 'xpopup'
publishVersion = '1.4.0'
publishVersion = '1.4.1'
repoName = 'jrepo'
desc = '功能强大,UI简洁,交互优雅的通用弹窗!可以替代Dialog,PopupWindow,PopupMenu,BottomSheet,DrawerLayout等组件,自带十几种效果良好的动画, 支持完全的UI和动画自定义!'
website = 'https://github.com/li-xiaojun/XPopup'
Expand Down
1 change: 1 addition & 0 deletions library/src/main/java/com/lxj/xpopup/XPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public void run() {
pv.popupInfo.decorView.removeView(pv);
KeyboardUtils.removeLayoutChangeListener(pv.popupInfo.decorView);
popupViews.remove(pv);

if (pv.popupInfo != null && pv.popupInfo.xPopupCallback != null) {
pv.popupInfo.xPopupCallback.onDismiss();
}
Expand Down
18 changes: 10 additions & 8 deletions library/src/main/java/com/lxj/xpopup/core/BasePopupView.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ protected void doAfterShow(){
postDelayed(new Runnable() {
@Override
public void run() {
afterShow.run();
popupStatus = PopupStatus.Show;
onShow();
afterShow.run();
}
}, getAnimationDuration());
}
Expand Down Expand Up @@ -318,20 +319,21 @@ protected void doAfterDismiss(){
postDelayed(new Runnable() {
@Override
public void run() {
afterDismiss.run();
popupStatus = PopupStatus.Dismiss;
onDismiss();
if(afterDismiss!=null)afterDismiss.run();
}
}, getAnimationDuration());
}

/**
* 结束后做一些事情
* @param afterDismiss
* 消失动画执行完毕后执行
*/
protected void dismiss(Runnable afterDismiss){
this.afterDismiss = afterDismiss;
dismiss();
}
protected void onDismiss(){}
/**
* 显示动画执行完毕后执行
*/
protected void onShow(){}

private float x, y;
private long downTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
Expand Down
9 changes: 9 additions & 0 deletions library/src/main/java/com/lxj/xpopup/enums/LayoutStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lxj.xpopup.enums;

/**
* Description:
* Create by lxj, at 2019/2/22
*/
public enum LayoutStatus {
Open,Close
}
47 changes: 16 additions & 31 deletions library/src/main/java/com/lxj/xpopup/widget/PopupDrawerLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@
import android.widget.FrameLayout;

import com.lxj.xpopup.animator.ShadowBgAnimator;
import com.lxj.xpopup.enums.LayoutStatus;

/**
* Description: 根据手势拖拽子View的layout,这种类型的弹窗比较特殊,不需要额外的动画器,因为
* 动画是根据手势滑动而发生的
* Create by dance, at 2018/12/20
*/
public class PopupDrawerLayout extends FrameLayout {
enum DrawerStatus{
Open, Close
}

public enum Position {
Left, Right
}

DrawerStatus status = DrawerStatus.Close;
LayoutStatus status = null;
ViewDragHelper dragHelper;
View child;
Position position = Position.Left;
Expand Down Expand Up @@ -70,29 +69,8 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
}
}

// 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) {
isIntercept = dragHelper.shouldInterceptTouchEvent(ev);
Expand All @@ -108,7 +86,7 @@ public boolean onTouchEvent(MotionEvent event) {
ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(@NonNull View view, int i) {
return true;
return !dragHelper.continueSettling(true);
}

@Override
Expand Down Expand Up @@ -136,8 +114,8 @@ public void onViewPositionChanged(@NonNull View changedView, int left, int top,
if (position == Position.Left) {
// fraction = (now - start)*1f / (end - start)
fraction = (left + child.getMeasuredWidth()) * 1f / child.getMeasuredWidth();
if (left == -child.getMeasuredWidth() && listener != null && status!= DrawerStatus.Close){
status = DrawerStatus.Close;
if (left == -child.getMeasuredWidth() && listener != null && status != LayoutStatus.Close) {
status = LayoutStatus.Close;
listener.onClose();
}
} else {
Expand All @@ -148,8 +126,8 @@ public void onViewPositionChanged(@NonNull View changedView, int left, int top,
setBackgroundColor(bgAnimator.calculateBgColor(fraction));
if (listener != null) {
listener.onDismissing(fraction);
if(fraction==1f && status!=DrawerStatus.Open){
status = DrawerStatus.Open;
if (fraction == 1f && status != LayoutStatus.Open) {
status = LayoutStatus.Open;
listener.onOpen();
}
}
Expand Down Expand Up @@ -188,6 +166,12 @@ public void computeScroll() {
}
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
status = null;
}

/**
* 打开Drawer
*/
Expand Down Expand Up @@ -222,6 +206,7 @@ public void setOnCloseListener(OnCloseListener listener) {

public interface OnCloseListener {
void onClose();

void onOpen();

/**
Expand Down
12 changes: 6 additions & 6 deletions library/src/main/java/com/lxj/xpopup/widget/SmartDragLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.widget.OverScroller;

import com.lxj.xpopup.animator.ShadowBgAnimator;
import com.lxj.xpopup.enums.LayoutStatus;
import com.lxj.xpopup.util.XPopupUtils;

/**
Expand All @@ -29,7 +30,7 @@ public class SmartDragLayout extends CardView implements NestedScrollingParent {
boolean dismissOnTouchOutside = true;
boolean hasShadowBg = true;
boolean isUserClose = false;

LayoutStatus status = LayoutStatus.Close;
public SmartDragLayout(Context context) {
this(context, null);
}
Expand Down Expand Up @@ -93,9 +94,6 @@ public boolean onTouchEvent(MotionEvent event) {
downTime = System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
if (!scroller.isFinished()) {
scroller.abortAnimation();
}
if (enableGesture) {
int dy = (int) (event.getY() - touchY);
scrollTo(getScrollX(), getScrollY() - dy);
Expand Down Expand Up @@ -138,9 +136,11 @@ public void scrollTo(int x, int y) {
if (hasShadowBg)
setBackgroundColor(bgAnimator.calculateBgColor(fraction));
if(listener!=null){
if (isUserClose && fraction == 0f) {
if (isUserClose && fraction == 0f && status!=LayoutStatus.Close) {
status = LayoutStatus.Close;
listener.onClose();
}else if(fraction==1f && !scroller.computeScrollOffset() ){
}else if(fraction==1f && status!=LayoutStatus.Open){
status = LayoutStatus.Open;
listener.onOpen();
}
}
Expand Down

0 comments on commit 5907d9b

Please sign in to comment.