ViewHolder 方法中的 ButterKnife onClick 侦听器
ButterKnife onClick listener inside ViewHolder method
在此方法中将 Butter Knife 的 @OnClick
添加到视图的正确方法是什么?
private List<Foo> foos;
private RecyclerView recyclerView;
private MapActivity mapView;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_foo, parent,
false);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedPosition = recyclerView.getChildAdapterPosition(v);
mapView.onFooSelected(foos.get(selectedPosition));
}
});
return new FooInfoViewHolder(view);
}
这个问题的不同之处在于我想将 @OnClick
绑定到 FooInfoViewHolder
本身。
使用 ViewHolder class 并且在其中 class 我们可以处理黄油刀的点击。
下面举个例子方便理解
public class YourAdapter extends RecyclerView.Adapter<YourAdapter.ViewHolder> {
private List<Data> list;
public YourAdapter(List<Data> list) {
this.list = list;
}
@Override
public YourAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return new ViewHolder(inflater.inflate(R.layout.list_item_layout, parent, false));
}
@Override
public void onBindViewHolder(YourAdapter.ViewHolder holder, int position) {
Data item = list.get(position);
holder.tvTitle1.setText(item.getTitle1());
}
@Override
public int getItemCount() {
int count = 0;
if (null != list) {
count = list.size();
}
return count;
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.tvVRValue1)
TextView tvTitle1;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
@OnClick(R.id.tvVRValue1)
public void onClickYourFunction(){
//Here your onclick method goes
}
}
}
希望对您有所帮助。干杯!!!
我知道出了什么问题。实际上,我认为这与可能的重复答案略有不同。
关键是在 ViewHolder ITSELF 上实现 @OnClick
(不像在另一个问题中那样,在视图的子级上实现):
private List<Foo> foos;
private RecyclerView recyclerView;
private MapActivity mapView;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_foo, parent,
false);
return new FooInfoViewHolder(view);
}
然后是 FooInfoViewHolder:
public class FooInfoViewHolder extends RecyclerView.ViewHolder {
@OnClick
public void onClick(View v) {
int selectedPosition = recyclerView.getChildAdapterPosition(v);
mapView.onFooSelected(foos.get(selectedPosition));
}
public FooInfoViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
在此方法中将 Butter Knife 的 @OnClick
添加到视图的正确方法是什么?
private List<Foo> foos;
private RecyclerView recyclerView;
private MapActivity mapView;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_foo, parent,
false);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedPosition = recyclerView.getChildAdapterPosition(v);
mapView.onFooSelected(foos.get(selectedPosition));
}
});
return new FooInfoViewHolder(view);
}
这个问题的不同之处在于我想将 @OnClick
绑定到 FooInfoViewHolder
本身。
使用 ViewHolder class 并且在其中 class 我们可以处理黄油刀的点击。
下面举个例子方便理解
public class YourAdapter extends RecyclerView.Adapter<YourAdapter.ViewHolder> {
private List<Data> list;
public YourAdapter(List<Data> list) {
this.list = list;
}
@Override
public YourAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return new ViewHolder(inflater.inflate(R.layout.list_item_layout, parent, false));
}
@Override
public void onBindViewHolder(YourAdapter.ViewHolder holder, int position) {
Data item = list.get(position);
holder.tvTitle1.setText(item.getTitle1());
}
@Override
public int getItemCount() {
int count = 0;
if (null != list) {
count = list.size();
}
return count;
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.tvVRValue1)
TextView tvTitle1;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
@OnClick(R.id.tvVRValue1)
public void onClickYourFunction(){
//Here your onclick method goes
}
}
}
希望对您有所帮助。干杯!!!
我知道出了什么问题。实际上,我认为这与可能的重复答案略有不同。
关键是在 ViewHolder ITSELF 上实现 @OnClick
(不像在另一个问题中那样,在视图的子级上实现):
private List<Foo> foos;
private RecyclerView recyclerView;
private MapActivity mapView;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_foo, parent,
false);
return new FooInfoViewHolder(view);
}
然后是 FooInfoViewHolder:
public class FooInfoViewHolder extends RecyclerView.ViewHolder {
@OnClick
public void onClick(View v) {
int selectedPosition = recyclerView.getChildAdapterPosition(v);
mapView.onFooSelected(foos.get(selectedPosition));
}
public FooInfoViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}