Skip to content

Commit

Permalink
[FIX]simplify the onclick in the top view curtain
Browse files Browse the repository at this point in the history
  • Loading branch information
soulqw committed Jun 27, 2021
1 parent 0bbd8aa commit bb48623
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 31 deletions.
35 changes: 7 additions & 28 deletions app/src/main/java/com/qw/curtain/sample/SimpleGuideActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.qw.curtain.lib.Curtain;
import com.qw.curtain.lib.IGuide;
import com.qw.curtain.lib.OnViewInTopClickListener;
import com.qw.curtain.lib.Padding;
import com.qw.curtain.lib.shape.RoundShape;

Expand Down Expand Up @@ -60,21 +61,10 @@ private void showInitGuide() {
.withPadding(findViewById(R.id.btn_shape_custom), Padding.all(10))
.setTopView(R.layout.view_guide_1)
// .setNoCurtainAnimation(true)
.setCallBack(new Curtain.CallBack() {
.addOnTopViewClickListener(R.id.tv_i_know, new OnViewInTopClickListener<IGuide>() {
@Override
public void onShow(final IGuide iGuide) {
iGuide.findViewByIdInTopView(R.id.tv_i_know)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iGuide.dismissGuide();
}
});
}

@Override
public void onDismiss(IGuide iGuide) {
showThirdGuide();
public void onClick(View current, IGuide currentHost) {
currentHost.dismissGuide();
}
}).show();
}
Expand All @@ -84,21 +74,10 @@ private void showThirdGuide() {
new Curtain(SimpleGuideActivity.this)
.with(findViewById(R.id.btn_open_left))
.setTopView(R.layout.view_guide_2)
.setCallBack(new Curtain.CallBack() {
.addOnTopViewClickListener(R.id.tv_i_know, new OnViewInTopClickListener<IGuide>() {
@Override
public void onShow(final IGuide iGuide) {
iGuide.findViewByIdInTopView(R.id.tv_i_know)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iGuide.dismissGuide();
}
});
}

@Override
public void onDismiss(IGuide iGuide) {

public void onClick(View current, IGuide currentHost) {
currentHost.dismissGuide();
}
}).show();
}
Expand Down
20 changes: 18 additions & 2 deletions curtain/src/main/java/com/qw/curtain/lib/Curtain.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.view.View;

import androidx.annotation.ColorRes;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -115,11 +116,24 @@ public Curtain withShape(@NonNull View which, Shape shape) {
}

/**
* set the embellish view of the curtain
* set the describe view of the curtain
*/
public Curtain setTopView(@LayoutRes int layoutId) {
this.buildParams.topLayoutRes = layoutId;
return this;

}

/**
* set the onClick in the top view
* this method base on setTopView
* @param viewId the view in top view
* @param onClickListener the onClickListener
* @see #setTopView(int)
*/
public Curtain addOnTopViewClickListener(@IdRes int viewId, OnViewInTopClickListener<IGuide> onClickListener) {
this.buildParams.topViewOnClickListeners.append(viewId, onClickListener);
return this;
}

/**
Expand Down Expand Up @@ -230,14 +244,16 @@ public static class Param {

int animationStyle = Constance.STATE_NOT_SET;

SparseArray<OnViewInTopClickListener> topViewOnClickListeners = new SparseArray<>();

}

public interface CallBack {

/**
* call when show success
*/
void onShow(IGuide iGuide);
void onShow(IGuide curtain);

/**
* call when dismiss
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public static GuideDialogFragment newInstance(Curtain.Param param) {
}
guideView.setHollowInfo(tobeDraw);
guider.setGuideView(guideView);

return guider;
}

Expand Down Expand Up @@ -184,6 +183,23 @@ private void updateTopView() {
contentView.removeViewAt(1);
}
LayoutInflater.from(contentView.getContext()).inflate(topLayoutRes, contentView, true);
//on top view click listeners
SparseArray<OnViewInTopClickListener> listeners = param.topViewOnClickListeners;
int onClickListenersSize = listeners.size();
for (int i = 0; i < onClickListenersSize; i++) {
int idRes = listeners.keyAt(i);
final OnViewInTopClickListener<Object> listener = listeners.valueAt(i);
View view = contentView.findViewById(idRes);
if (null == view) {
throw new NullPointerException("the target view was not find in the top view, check your setTopView layout res first");
}
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(v, GuideDialogFragment.this);
}
});
}
}

}
22 changes: 22 additions & 0 deletions curtain/src/main/java/com/qw/curtain/lib/IGuide.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,34 @@
*/
public interface IGuide {

/**
* you can refresh the hollow fields when it showed
*
* @param hollows the hollow fields
*/
void updateHollows(HollowInfo... hollows);

/**
* you can refresh the top view
*
* @param layoutId the view res
*/
void updateTopView(@LayoutRes int layoutId);

/**
* if you want do more operate in top view (onClickListener or onTouchListener)
* you can find it by this method
* if you just need an onclick listener in top view you can use Curtain.addOnTopViewClickListener instead
*
* @param id
* @param <T>
* @see #Curtain.addOnTopViewClickListener()
*/
<T extends View> T findViewByIdInTopView(@IdRes int id);

/**
* dismiss the curtain
*/
void dismissGuide();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.qw.curtain.lib;

import android.view.View;

public interface OnViewInTopClickListener<T> {

/**
* the onclick listener of view in the top
*
* @param current the clicked view
* @param currentHost the host curtain or curtainFlow
*/
void onClick(View current, T currentHost);

}

0 comments on commit bb48623

Please sign in to comment.