Skip to content

Commit f8e6c74

Browse files
committed
优化 Demo 工程的使用案例
1 parent df23456 commit f8e6c74

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

app/src/main/java/com/hjq/window/demo/MainActivity.java

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import android.os.Build;
88
import android.os.Bundle;
99
import android.support.annotation.NonNull;
10-
import android.support.annotation.Nullable;
1110
import android.support.design.widget.Snackbar;
1211
import android.support.v7.app.AppCompatActivity;
1312
import android.support.v7.widget.LinearLayoutManager;
1413
import android.support.v7.widget.RecyclerView;
1514
import android.util.Log;
1615
import android.view.Gravity;
16+
import android.view.KeyEvent;
1717
import android.view.View;
1818
import android.view.ViewGroup;
1919
import android.view.WindowManager;
@@ -33,6 +33,7 @@
3333
import com.hjq.window.OnWindowLayoutInflateListener;
3434
import com.hjq.window.OnWindowLifecycleCallback;
3535
import com.hjq.window.OnWindowViewClickListener;
36+
import com.hjq.window.OnWindowViewKeyListener;
3637
import com.hjq.window.OnWindowViewLongClickListener;
3738
import com.hjq.window.demo.DemoAdapter.OnItemClickListener;
3839
import com.hjq.window.demo.DemoAdapter.OnItemLongClickListener;
@@ -128,7 +129,7 @@ public void onClick(View v) {
128129

129130
@Override
130131
public void onClick(@NonNull EasyWindow<?> easyWindow, @NonNull TextView view) {
131-
cancelAndRecycleEasyWindow(easyWindow);
132+
easyWindow.cancel();
132133
}
133134
})
134135
.show();
@@ -167,7 +168,7 @@ public void onWindowCancel(@NonNull EasyWindow<?> easyWindow) {
167168

168169
@Override
169170
public void onClick(final @NonNull EasyWindow<?> easyWindow, @NonNull TextView view) {
170-
cancelAndRecycleEasyWindow(easyWindow);
171+
easyWindow.cancel();
171172
}
172173
})
173174
.showAsDropDown(v, Gravity.BOTTOM);
@@ -182,19 +183,20 @@ public void onClick(final @NonNull EasyWindow<?> easyWindow, @NonNull TextView v
182183

183184
@Override
184185
public void onClick(final @NonNull EasyWindow<?> easyWindow, @NonNull TextView view) {
185-
cancelAndRecycleEasyWindow(easyWindow);
186+
easyWindow.cancel();
186187
}
187188
})
188189
.show();
189190

190191
} else if (viewId == R.id.btn_main_web) {
191192

192193
EasyWindow.with(this)
193-
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
194+
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED)
195+
.setWindowSizePercent(0.8f, 0.8f)
194196
.setContentView(R.layout.window_web, new OnWindowLayoutInflateListener() {
195197

196198
@Override
197-
public void onWindowLayoutInflateFinished(@NonNull EasyWindow<?> easyWindow, @Nullable View view, int layoutId, @NonNull ViewGroup parentView) {
199+
public void onWindowLayoutInflateFinished(@NonNull EasyWindow<?> easyWindow, @NonNull View view, int layoutId, @NonNull ViewGroup parentView) {
198200
WebView webView = view.findViewById(R.id.wv_window_web_content);
199201
WebSettings settings = webView.getSettings();
200202
// 允许文件访问
@@ -225,6 +227,7 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
225227
}
226228
});
227229

230+
// 加载指定的网页链接
228231
webView.loadUrl("https://github.com/getActivity/EasyWindow");
229232
}
230233
})
@@ -235,24 +238,44 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
235238

236239
@Override
237240
public void onClick(@NonNull EasyWindow<?> easyWindow, @NonNull ImageView view) {
238-
cancelAndRecycleEasyWindow(easyWindow);
241+
easyWindow.cancel();
239242
}
240243
})
241244
.setOnLongClickListenerByView(R.id.iv_window_web_close, new OnWindowViewLongClickListener<View>() {
245+
242246
@Override
243247
public boolean onLongClick(@NonNull EasyWindow<?> easyWindow, @NonNull View view) {
244248
Toaster.show("关闭按钮被长按了");
245249
return false;
246250
}
247251
})
252+
.setOnKeyListenerByView(R.id.wv_window_web_content, new OnWindowViewKeyListener<WebView>() {
253+
254+
@Override
255+
public boolean onKey(@NonNull EasyWindow<?> easyWindow, @NonNull WebView webView, @NonNull KeyEvent event, int keyCode) {
256+
if (event.getAction() == KeyEvent.ACTION_DOWN) {
257+
// 判断网页是否能回退
258+
if (webView.canGoBack()) {
259+
// 如果可以,则返回到上一页
260+
webView.goBack();
261+
} else {
262+
// 如果不行,则关闭窗口
263+
easyWindow.cancel();
264+
}
265+
return true;
266+
}
267+
return false;
268+
}
269+
})
248270
.show();
249271

250272
} else if (viewId == R.id.btn_main_list) {
251273

252274
EasyWindow.with(this)
253275
.setContentView(R.layout.window_list, new OnWindowLayoutInflateListener() {
276+
254277
@Override
255-
public void onWindowLayoutInflateFinished(@NonNull EasyWindow<?> easyWindow, @Nullable View view, int layoutId, @NonNull ViewGroup parentView) {
278+
public void onWindowLayoutInflateFinished(@NonNull EasyWindow<?> easyWindow, @NonNull View view, int layoutId, @NonNull ViewGroup parentView) {
256279
RecyclerView recyclerView = view.findViewById(R.id.rv_window_list_view);
257280
recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
258281

@@ -286,10 +309,11 @@ public boolean onItemLongClick(View itemView, int position) {
286309

287310
@Override
288311
public void onClick(@NonNull EasyWindow<?> easyWindow, @NonNull ImageView view) {
289-
cancelAndRecycleEasyWindow(easyWindow);
312+
easyWindow.cancel();
290313
}
291314
})
292315
.setOnLongClickListenerByView(R.id.iv_window_list_close, new OnWindowViewLongClickListener<View>() {
316+
293317
@Override
294318
public boolean onLongClick(@NonNull EasyWindow<?> easyWindow, @NonNull View view) {
295319
Toaster.show("关闭按钮被长按了");
@@ -311,7 +335,7 @@ public boolean onLongClick(@NonNull EasyWindow<?> easyWindow, @NonNull View view
311335

312336
@Override
313337
public void onClick(@NonNull EasyWindow<?> easyWindow, @NonNull TextView view) {
314-
cancelAndRecycleEasyWindow(easyWindow);
338+
easyWindow.cancel();
315339
}
316340
})
317341
.show();
@@ -404,7 +428,7 @@ public void onWindowDraggingStop(@NonNull EasyWindow<?> easyWindow) {
404428
// 传入 Application 表示这个是一个全局的 Toast
405429
EasyWindow.with(application)
406430
.setContentView(R.layout.window_phone)
407-
.setWindowLocation(Gravity.END | Gravity.BOTTOM, 0, 200)
431+
.setWindowLocationPercent(Gravity.END | Gravity.BOTTOM, 0, 0.2f)
408432
// 设置指定的拖拽规则
409433
.setWindowDraggableRule(springBackWindowDraggableRule)
410434
.setOnClickListenerByView(android.R.id.icon, new OnWindowViewClickListener<ImageView>() {
@@ -421,6 +445,7 @@ public void onClick(@NonNull EasyWindow<?> easyWindow, @NonNull ImageView view)
421445
}
422446
})
423447
.setOnLongClickListenerByView(android.R.id.icon, new OnWindowViewLongClickListener<ImageView>() {
448+
424449
@Override
425450
public boolean onLongClick(@NonNull EasyWindow<?> easyWindow, @NonNull ImageView view) {
426451
Toaster.show("我被长按了");
@@ -429,13 +454,4 @@ public boolean onLongClick(@NonNull EasyWindow<?> easyWindow, @NonNull ImageView
429454
})
430455
.show();
431456
}
432-
433-
private void cancelAndRecycleEasyWindow(@NonNull EasyWindow<?> easyWindow) {
434-
// 有两种方式取消弹窗:
435-
// 1. easyWindow.cancel:顾名思义,取消显示
436-
// 2. easyWindow.recycle:在取消显示的基础上,加上了回收
437-
// 这两种区别在于,cancel 之后还能 show,但是 recycle 之后不能再 show
438-
// 通常情况下,如果你创建的 EasyWindow 对象在 cancel 之后永远不会再显示,取消弹窗建议直接用 recycle 方法,否则用 cancel 方法
439-
easyWindow.recycle();
440-
}
441457
}

app/src/main/res/layout/window_list.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
4-
android:layout_width="wrap_content"
4+
android:layout_width="150dp"
55
android:layout_height="wrap_content"
66
android:layout_gravity="center"
77
android:background="@drawable/dialog_toast_bg"

app/src/main/res/layout/window_list_item.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
android:id="@+id/tv_window_list_item_text"
5-
android:layout_width="wrap_content"
5+
android:layout_width="match_parent"
66
android:layout_height="wrap_content"
77
android:layout_marginLeft="15dp"
88
android:layout_marginTop="14dp"

app/src/main/res/layout/window_web.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
4-
android:layout_width="wrap_content"
5-
android:layout_height="wrap_content"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
66
android:layout_gravity="center"
77
android:layout_margin="100dp"
88
android:background="@drawable/dialog_toast_bg"
@@ -21,7 +21,7 @@
2121

2222
<WebView
2323
android:id="@+id/wv_window_web_content"
24-
android:layout_width="280dp"
25-
android:layout_height="400dp" />
24+
android:layout_width="match_parent"
25+
android:layout_height="match_parent" />
2626

2727
</LinearLayout>

0 commit comments

Comments
 (0)