Skip to content

Commit

Permalink
增加打断返回键点击逻辑的监听啦
Browse files Browse the repository at this point in the history
  • Loading branch information
ekwong committed Apr 6, 2020
1 parent ce102b2 commit 603b4c9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repositories {

```groovy
dependencies {
implementation 'com.github.EKwongChum:EkPopWindow:1.2.0'
implementation 'com.github.EKwongChum:EkPopWindow:1.2.1'
}
```

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ allprojects {
$minSdkVersion = 21
$compileSdkVersion = 28
$supportVersion = "28.0.0"
$versionCode = 4
$versionName = "1.2.0"
$versionCode = 5
$versionName = "1.2.1"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ekwong.lib;

/**
* @author ekwong
*/
public interface BackInterruption {

void interrupt();
}
41 changes: 34 additions & 7 deletions ekpopwindowlib/src/main/java/com/ekwong/lib/EkPopWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

import com.ekwong.ekpopwindowlib.R;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

/**
* @author erkang
Expand All @@ -28,7 +27,9 @@ public abstract class EkPopWindow {

private boolean isShow;

private List<BackPressedListener> backPressedListeners = new LinkedList<>();
private Set<BackPressedListener> backPressedListeners = new HashSet<>();

private Set<BackInterruption> backInterruptions = new HashSet<>();

public EkPopWindow(Context context, int resLayout, View decorView) {
mContext = context;
Expand All @@ -45,6 +46,22 @@ private void initBuilder() {
setBuilderConfig(mBuilder);
}

public boolean addBackPressedListener(BackPressedListener listener) {
return backPressedListeners.add(listener);
}

public boolean removeBackPressedListener(BackPressedListener listener) {
return backPressedListeners.remove(listener);
}

public boolean addBackInterruption(BackInterruption interruption) {
return backInterruptions.add(interruption);
}

public boolean removeBackInterruption(BackInterruption interruption) {
return backInterruptions.remove(interruption);
}

/**
* 设置弹窗的配置
*
Expand Down Expand Up @@ -97,11 +114,21 @@ public boolean isShow() {
return isShow;
}

/**
* 当返回按钮被点击的时候调用
*/
public void onBackPressed() {
if (!backPressedListeners.isEmpty()) {
for (BackPressedListener listener : backPressedListeners) {
listener.onBackPressed();
if (!backInterruptions.isEmpty()) {
for (BackInterruption listener : backInterruptions) {
listener.interrupt();
}
} else {
if (!backPressedListeners.isEmpty()) {
for (BackPressedListener listener : backPressedListeners) {
listener.onBackPressed();
}
}
dismiss();
}
}

Expand Down

0 comments on commit 603b4c9

Please sign in to comment.