最近項(xiàng)目中為了解決按鈕重復(fù)點(diǎn)擊問題后豫,搜索過程中胁勺,發(fā)現(xiàn)Rxjava居然可以實(shí)現(xiàn)這個(gè)功能,但是問題隨之而來怔昨,網(wǎng)上給出的所有Rxjava的解決方案都是基于Rxjava 1.0版本的雀久,而項(xiàng)目工程中使用的Rxjava2。話不多說趁舀,直接上代碼赖捌,各位看官自己看吧。
具體API區(qū)別可以參考之前的文章:http://www.reibang.com/p/d53463e1c3d6
Rxjava1實(shí)現(xiàn)
RxViewHelp.java
package com.hofon.common.util.help;
import android.support.annotation.NonNull;
import android.view.View;
import com.hofon.common.frame.retrofit.subscribers.RxView;
import java.util.List;
import java.util.concurrent.TimeUnit;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
/**
* Created by xfkang on 2017/3/29.
*/
public class RxViewHelp {
public static void clicks(Action1<View> action, @NonNull View... views) {
for (View view : views) {
RxView.clicks(view).throttleFirst(500, TimeUnit.MILLISECONDS).subscribe(action);
}
}
public static Observable<Integer> countDown(int time) {
if (time < 0) time = 0;
final int countTime = time;
return Observable.interval(0, 1, TimeUnit.SECONDS)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.map(new Func1<Long, Integer>() {
@Override
public Integer call(Long increaseTime) {
return countTime - increaseTime.intValue();
}
})
.take(countTime + 1);
}
}
RxView.java
package com.hofon.common.frame.retrofit.subscribers;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Adapter;
import com.hofon.doctor.adapter.common.base.RecyclerAdapter;
import rx.Observable;
import static com.hofon.common.frame.retrofit.subscribers.Preconditions.checkNotNull;
/**
* Created by xfkang on 2017/3/29.
*/
public final class RxView {
/**
* 監(jiān)聽onclick事件防抖動(dòng)
*
* @param view
* @return
*/
@CheckResult
@NonNull
public static Observable<View> clicks(@NonNull View view) {
checkNotNull(view, "view == null");
return Observable.create(new ViewClickOnSubscribe(view));
}
@CheckResult
@NonNull
public static <T extends Adapter> Observable<AdapterViewItemClickEvent> itemClickEvents(
@NonNull RecyclerAdapter<?> view) {
checkNotNull(view, "view == null");
return Observable.create(new AdapterViewItemClickEventOnSubscribe(view));
}
}
Preconditions.java
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hofon.common.frame.retrofit.subscribers;
import android.os.Looper;
public final class Preconditions {
public static void checkArgument(boolean assertion, String message) {
if (!assertion) {
throw new IllegalArgumentException(message);
}
}
public static <T> T checkNotNull(T value, String message) {
if (value == null) {
throw new NullPointerException(message);
}
return value;
}
public static void checkUiThread() {
if (Looper.getMainLooper() != Looper.myLooper()) {
throw new IllegalStateException(
"Must be called from the main thread. Was: " + Thread.currentThread());
}
}
private Preconditions() {
throw new AssertionError("No instances.");
}
}
ViewClickOnSubscribe.java
package com.hofon.common.frame.retrofit.subscribers;
import android.view.View;
import rx.Observable;
import rx.Subscriber;
import rx.android.MainThreadSubscription;
import static com.hofon.common.frame.retrofit.subscribers.Preconditions.checkUiThread;
/**
* onclick事件防抖動(dòng)
* 返回view
*/
final class ViewClickOnSubscribe implements Observable.OnSubscribe<View> {
final View view;
ViewClickOnSubscribe(View view) {
this.view = view;
}
@Override
public void call(final Subscriber<? super View> subscriber) {
checkUiThread();
View.OnClickListener listener = new View.OnClickListener() {
@Override public void onClick(View v) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(view);
}
}
};
view.setOnClickListener(listener);
subscriber.add(new MainThreadSubscription() {
@Override protected void onUnsubscribe() {
view.setOnClickListener(null);
}
});
}
}
具體使用:
@Override
public void initAction() {
RxViewHelp.clicks(this, mTagTv, image, mFinishBtn);
}
@Override
public void call(View view) {
if (view == mTagTv) {
} else if (view == image) {
} else{
}
}
Rxjava2實(shí)現(xiàn)
RxView.java
package com.itbird.utils;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.view.View;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import static com.itbird.utils.Preconditions.checkNotNull;
import static com.itbird.utils.Preconditions.checkUiThread;
/**
* 利用Rxjava防止抖動(dòng) and 重復(fù)點(diǎn)擊
* Created by xfkang on 2018/3/24.
*/
public class RxView {
/**
* 防止重復(fù)點(diǎn)擊
*
* @param target 目標(biāo)view
* @param action 監(jiān)聽器
*/
public static void setOnClickListeners(final Action1<View> action, @NonNull View... target) {
for (View view : target) {
RxView.onClick(view).throttleFirst(500, TimeUnit.MILLISECONDS).subscribe(new Consumer<View>() {
@Override
public void accept(@io.reactivex.annotations.NonNull View view) throws Exception {
action.onClick(view);
}
});
}
}
/**
* 監(jiān)聽onclick事件防抖動(dòng)
*
* @param view
* @return
*/
@CheckResult
@NonNull
private static Observable<View> onClick(@NonNull View view) {
checkNotNull(view, "view == null");
return Observable.create(new ViewClickOnSubscribe(view));
}
// @CheckResult
// @NonNull
// public static <T extends Adapter> Observable<AdapterViewItemClickEvent> itemClickEvents(
// @NonNull RecyclerAdapter<?> view) {
// checkNotNull(view, "view == null");
// return Observable.create(new AdapterViewItemClickEventOnSubscribe(view));
// }
/**
* onclick事件防抖動(dòng)
* 返回view
*/
private static class ViewClickOnSubscribe implements ObservableOnSubscribe<View> {
private View view;
public ViewClickOnSubscribe(View view) {
this.view = view;
}
@Override
public void subscribe(@io.reactivex.annotations.NonNull final ObservableEmitter<View> e) throws Exception {
checkUiThread();
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!e.isDisposed()) {
e.onNext(view);
}
}
};
view.setOnClickListener(listener);
}
}
/**
* A one-argument action. 點(diǎn)擊事件轉(zhuǎn)發(fā)接口
*
* @param <T> the first argument type
*/
public interface Action1<T> {
void onClick(T t);
}
}
Preconditions.java
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.itbird.utils;
import android.os.Looper;
public final class Preconditions {
public static void checkArgument(boolean assertion, String message) {
if (!assertion) {
throw new IllegalArgumentException(message);
}
}
public static <T> T checkNotNull(T value, String message) {
if (value == null) {
throw new NullPointerException(message);
}
return value;
}
public static void checkUiThread() {
if (Looper.getMainLooper() != Looper.myLooper()) {
throw new IllegalStateException(
"Must be called from the main thread. Was: " + Thread.currentThread());
}
}
private Preconditions() {
throw new AssertionError("No instances.");
}
}
具體使用:
1.為多個(gè)控件一起注冊(cè)onClick事件
2.onClick事件具體方法