Skip to content

Commit

Permalink
修复RxAdapter不设置监听的时候,点击item崩溃的控指针问题
Browse files Browse the repository at this point in the history
  • Loading branch information
luhuan committed Apr 14, 2017
1 parent 76b7878 commit 50b8aed
Showing 1 changed file with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.ViewGroup;

import com.luhuan.rxprovider.RxListener;
Expand All @@ -16,21 +17,22 @@
*/

public abstract class RxAdapter<T> extends RecyclerView.Adapter<RxViewHolder<T>> {
private static final String TAG = "RxAdapter";
protected List<T> list;
protected Context mContext;
protected OnItemClickLitener<T> onItemClickListener;

public RxAdapter(List<T> list ,Context context) {
public RxAdapter(List<T> list, Context context) {
this.list = list;
mContext=context;
mContext = context;
}

public void setOnItemClickListener(OnItemClickLitener<T> onItemClickListener){
this.onItemClickListener=onItemClickListener;
public void setOnItemClickListener(OnItemClickLitener<T> onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}

public void refresh(List<T> list){
this.list=list;
public void refresh(List<T> list) {
this.list = list;
notifyDataSetChanged();
}

Expand All @@ -39,7 +41,7 @@ public RxViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) {
return getHolder(parent);
}

public abstract RxViewHolder<T> getHolder(ViewGroup parent) ;
public abstract RxViewHolder<T> getHolder(ViewGroup parent);

@Override
public void onBindViewHolder(RxViewHolder<T> holder, final int position) {
Expand All @@ -48,14 +50,18 @@ public void onBindViewHolder(RxViewHolder<T> holder, final int position) {
.subscribe(new Consumer<Object>() {
@Override
public void accept(@NonNull Object o) throws Exception {
onItemClickListener.onItemClick(position,list.get(position));
if (onItemClickListener == null) {
Log.d(TAG, "OnItemClickLitener==null,您没有设置监听");
} else {
onItemClickListener.onItemClick(position, list.get(position));
}
}
});
}

@Override
public int getItemCount() {
return list==null?0:list.size();
return list == null ? 0 : list.size();
}

public interface OnItemClickLitener<T> {
Expand Down

0 comments on commit 50b8aed

Please sign in to comment.