まずは, 入れ子なしでばらして書かせたほうがいいような気がするのです.
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public ImageView image;
public OnClickPartsListener listener;
public MyViewHolder(View itemView, OnClickPartsListener listener) {
super(itemView);
this.listener = listener;
text = (TextView) itemView.findViewById(R.id.text);
image = (ImageView) itemView.findViewById(R.id.image);
text.setOnClickListener(new View.OnClickListener {
listener.onClickText("Text Clicked!!");
});
image.setOnClickListener(new View.OnClickListener {
listener.onClickImage("Image Clicked!!");
});
}
public interface OnClickPartsListener {
void onClickText(String message);
void onClickImage(String message);
}
}
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
private MyViewHolder.OnClickPartsListener listener;
public MyAdapter(MyViewHolder.OnClickPartsListener listener) {
this.listener = listener;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//..
return new MyViewHolder(view, listener);
}
}
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
MyViewHolder.OnClickPartsListener listener =
new MyViewHolder.OnClickPartsListener() {
public void onClickText(String message) {
Toast.makeText(this, message, LENGTH_SHORT).show();
}
public void onClickImage(String message) {
Toast.makeText(this, message, LENGTH_SHORT).show();
}
};
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
adapter = new MyAdapter(listener) {
recyclerView.setAdapter(adapter);
}
}
めんどくさいので,
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
private final PublishSubject<String> onClickSubject = PublishSubject.create();
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final Item item = items.get(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onClickSubject.onNext(item);
}
});
}
public Observable<String> getPositionClicks(){
return onClickSubject.asObservable();
}
}
または, イベントバス..
まとめ
親や祖父母側で実装した子や孫のインターフェースをコンストラクタ経由で渡し返す.
tag を上手に使うとよい.