我在RecyclerView的适配器中。
从onBindViewHolder()我可以为RecyclerView的行(ViewHolder的Item)执行onClick事件。
是否可以使用Position调用onClick()?
答案 0 :(得分:1)
你可以做这样的事情
适配器:
public class OffersAdapter extends RecyclerView.Adapter<OffersAdapter.ViewHolder> {
private List<Offer> mDataset;
private OffersListner mListener;
public OffersAdapter(List<Offer> response) {
mDataset = response;
}
public void setListener(OffersListner listener) {
mListener = listener;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row_offers, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final int pos = getItemViewType(position);
holder.mLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onOfferChoice(mDataset.get(pos));
}
});
}
@Override
public int getItemCount() {
return mDataset.size();
}
public interface OffersListner {
void onOfferChoice(Offer offer);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.item_offers_layout)
LinearLayout mLayout;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
的活动:
public class OffersSelectActivity extends BaseEventActivity implements OffersAdapter.OffersListner {
.
.
.
private void setOffers(List<Offer> offers) {
OffersAdapter mAdapter = new OffersAdapter(offers);
mAdapter.setListener(this);
mOffers.setHasFixedSize(true);
mOffers.setLayoutManager(new LinearLayoutManager(this));
mOffers.setAdapter(mAdapter);
}
@Override
public void onOfferChoice(Offer offer) {
Intent intent = new Intent(this, OfferActivity.class);
intent.putExtra(Constants.EXTRA_OFFER_ID, offer.id);
intent.putExtra(Constants.EXTRA_OFFER_NAME, offer.name);
startActivity(intent);
}
}
答案 1 :(得分:0)
以下是示例:
public class NavigationDrawerAdapter extends RecyclerView.Adapter<NavigationDrawerAdapter.MyViewHolder> {
List<Pojo_menu> data;
public NavigationDrawerAdapter(Context mContext, List<Pojo_menu> data) {
this.data = data;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView title, tv_count;
RelativeLayout mRelativeLayout;
private SparseBooleanArray selectedItems = new SparseBooleanArray();
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.item_name);
tv_count = (TextView) itemView.findViewById(R.id.tv_count);
mRelativeLayout = (RelativeLayout) itemView.findViewById(R.id.my_nav_menu);
}
}
public NavigationDrawerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.nav_drawer_row, parent, false);
return new NavigationDrawerAdapter.MyViewHolder(itemView);
}
public void onBindViewHolder(NavigationDrawerAdapter.MyViewHolder holder, int position) {
Pojo_menu current = data.get(position);
String count = current.getCount();
holder.tv_count.setText(count);
holder.title.setText(current.getName());
holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public int getItemCount() {
return data.size();
}
}
答案 2 :(得分:0)
只需将onClickListener
设置为持有人
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setOnClickListner(new View.OnClickListener() {
@Override
public void onClick(View view) {
// to the thing you want refer to the position by holder.getAdapterPosition()
}
};)
}
不建议直接使用position
,因为您必须声明它final
。因此,在引用onClick
内的位置时,请改用holder.getAdapterPosition()
。