Skip to content

Commit bb48623

Browse files
committed
[FIX]simplify the onclick in the top view curtain
1 parent 0bbd8aa commit bb48623

5 files changed

Lines changed: 79 additions & 31 deletions

File tree

app/src/main/java/com/qw/curtain/sample/SimpleGuideActivity.java

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.qw.curtain.lib.Curtain;
1313
import com.qw.curtain.lib.IGuide;
14+
import com.qw.curtain.lib.OnViewInTopClickListener;
1415
import com.qw.curtain.lib.Padding;
1516
import com.qw.curtain.lib.shape.RoundShape;
1617

@@ -60,21 +61,10 @@ private void showInitGuide() {
6061
.withPadding(findViewById(R.id.btn_shape_custom), Padding.all(10))
6162
.setTopView(R.layout.view_guide_1)
6263
// .setNoCurtainAnimation(true)
63-
.setCallBack(new Curtain.CallBack() {
64+
.addOnTopViewClickListener(R.id.tv_i_know, new OnViewInTopClickListener<IGuide>() {
6465
@Override
65-
public void onShow(final IGuide iGuide) {
66-
iGuide.findViewByIdInTopView(R.id.tv_i_know)
67-
.setOnClickListener(new View.OnClickListener() {
68-
@Override
69-
public void onClick(View v) {
70-
iGuide.dismissGuide();
71-
}
72-
});
73-
}
74-
75-
@Override
76-
public void onDismiss(IGuide iGuide) {
77-
showThirdGuide();
66+
public void onClick(View current, IGuide currentHost) {
67+
currentHost.dismissGuide();
7868
}
7969
}).show();
8070
}
@@ -84,21 +74,10 @@ private void showThirdGuide() {
8474
new Curtain(SimpleGuideActivity.this)
8575
.with(findViewById(R.id.btn_open_left))
8676
.setTopView(R.layout.view_guide_2)
87-
.setCallBack(new Curtain.CallBack() {
77+
.addOnTopViewClickListener(R.id.tv_i_know, new OnViewInTopClickListener<IGuide>() {
8878
@Override
89-
public void onShow(final IGuide iGuide) {
90-
iGuide.findViewByIdInTopView(R.id.tv_i_know)
91-
.setOnClickListener(new View.OnClickListener() {
92-
@Override
93-
public void onClick(View v) {
94-
iGuide.dismissGuide();
95-
}
96-
});
97-
}
98-
99-
@Override
100-
public void onDismiss(IGuide iGuide) {
101-
79+
public void onClick(View current, IGuide currentHost) {
80+
currentHost.dismissGuide();
10281
}
10382
}).show();
10483
}

curtain/src/main/java/com/qw/curtain/lib/Curtain.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.view.View;
77

88
import androidx.annotation.ColorRes;
9+
import androidx.annotation.IdRes;
910
import androidx.annotation.LayoutRes;
1011
import androidx.annotation.MainThread;
1112
import androidx.annotation.NonNull;
@@ -115,11 +116,24 @@ public Curtain withShape(@NonNull View which, Shape shape) {
115116
}
116117

117118
/**
118-
* set the embellish view of the curtain
119+
* set the describe view of the curtain
119120
*/
120121
public Curtain setTopView(@LayoutRes int layoutId) {
121122
this.buildParams.topLayoutRes = layoutId;
122123
return this;
124+
125+
}
126+
127+
/**
128+
* set the onClick in the top view
129+
* this method base on setTopView
130+
* @param viewId the view in top view
131+
* @param onClickListener the onClickListener
132+
* @see #setTopView(int)
133+
*/
134+
public Curtain addOnTopViewClickListener(@IdRes int viewId, OnViewInTopClickListener<IGuide> onClickListener) {
135+
this.buildParams.topViewOnClickListeners.append(viewId, onClickListener);
136+
return this;
123137
}
124138

125139
/**
@@ -230,14 +244,16 @@ public static class Param {
230244

231245
int animationStyle = Constance.STATE_NOT_SET;
232246

247+
SparseArray<OnViewInTopClickListener> topViewOnClickListeners = new SparseArray<>();
248+
233249
}
234250

235251
public interface CallBack {
236252

237253
/**
238254
* call when show success
239255
*/
240-
void onShow(IGuide iGuide);
256+
void onShow(IGuide curtain);
241257

242258
/**
243259
* call when dismiss

curtain/src/main/java/com/qw/curtain/lib/GuideDialogFragment.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public static GuideDialogFragment newInstance(Curtain.Param param) {
5151
}
5252
guideView.setHollowInfo(tobeDraw);
5353
guider.setGuideView(guideView);
54-
5554
return guider;
5655
}
5756

@@ -184,6 +183,23 @@ private void updateTopView() {
184183
contentView.removeViewAt(1);
185184
}
186185
LayoutInflater.from(contentView.getContext()).inflate(topLayoutRes, contentView, true);
186+
//on top view click listeners
187+
SparseArray<OnViewInTopClickListener> listeners = param.topViewOnClickListeners;
188+
int onClickListenersSize = listeners.size();
189+
for (int i = 0; i < onClickListenersSize; i++) {
190+
int idRes = listeners.keyAt(i);
191+
final OnViewInTopClickListener<Object> listener = listeners.valueAt(i);
192+
View view = contentView.findViewById(idRes);
193+
if (null == view) {
194+
throw new NullPointerException("the target view was not find in the top view, check your setTopView layout res first");
195+
}
196+
view.setOnClickListener(new View.OnClickListener() {
197+
@Override
198+
public void onClick(View v) {
199+
listener.onClick(v, GuideDialogFragment.this);
200+
}
201+
});
202+
}
187203
}
188204

189205
}

curtain/src/main/java/com/qw/curtain/lib/IGuide.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,34 @@
1010
*/
1111
public interface IGuide {
1212

13+
/**
14+
* you can refresh the hollow fields when it showed
15+
*
16+
* @param hollows the hollow fields
17+
*/
1318
void updateHollows(HollowInfo... hollows);
1419

20+
/**
21+
* you can refresh the top view
22+
*
23+
* @param layoutId the view res
24+
*/
1525
void updateTopView(@LayoutRes int layoutId);
1626

27+
/**
28+
* if you want do more operate in top view (onClickListener or onTouchListener)
29+
* you can find it by this method
30+
* if you just need an onclick listener in top view you can use Curtain.addOnTopViewClickListener instead
31+
*
32+
* @param id
33+
* @param <T>
34+
* @see #Curtain.addOnTopViewClickListener()
35+
*/
1736
<T extends View> T findViewByIdInTopView(@IdRes int id);
1837

38+
/**
39+
* dismiss the curtain
40+
*/
1941
void dismissGuide();
2042

2143
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.qw.curtain.lib;
2+
3+
import android.view.View;
4+
5+
public interface OnViewInTopClickListener<T> {
6+
7+
/**
8+
* the onclick listener of view in the top
9+
*
10+
* @param current the clicked view
11+
* @param currentHost the host curtain or curtainFlow
12+
*/
13+
void onClick(View current, T currentHost);
14+
15+
}

0 commit comments

Comments
 (0)