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 50b8aed commit 51a5558
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 8 additions & 1 deletion app/src/main/java/com/luhuan/provider/Main2Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,20 @@ protected void onCreate(Bundle savedInstanceState) {
RxAdapter<Integer> adapter=new RxAdapter<Integer>(list,this) {
@Override
public RxViewHolder<Integer> getHolder(ViewGroup parent) {
View view=View.inflate(Main2Activity.this,R.layout.item2_adpter,parent);
View view=View.inflate(Main2Activity.this,R.layout.item2_adpter,null);
return new ViewHoder(view);
}
};
recyclerView.addItemDecoration(itemDec);
recyclerView.setAdapter(adapter);
RecyclerPagerHelper.getIntance().getPosition(recyclerView,null);
adapter.setOnItemClickListener(new RxAdapter.OnItemClickLitener<Integer>() {
@Override
public void onItemClick(int position, Integer integer) {
RxToast.show(position+"");
}
});

}

private class ItemDec extends RecyclerView.ItemDecoration{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;

/**
* Created by luhuan on 2017/3/21.
Expand Down Expand Up @@ -47,14 +48,16 @@ public RxViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) {
public void onBindViewHolder(RxViewHolder<T> holder, final int position) {
holder.setData(list.get(position));
RxListener.click(holder.itemView)
.filter(new Predicate<Object>() {
@Override
public boolean test(@NonNull Object o) throws Exception {
return onItemClickListener != null;
}
})
.subscribe(new Consumer<Object>() {
@Override
public void accept(@NonNull Object o) throws Exception {
if (onItemClickListener == null) {
Log.d(TAG, "OnItemClickLitener==null,您没有设置监听");
} else {
onItemClickListener.onItemClick(position, list.get(position));
}
onItemClickListener.onItemClick(position, list.get(position));
}
});
}
Expand Down

0 comments on commit 51a5558

Please sign in to comment.