我在RecyclerView的適配器中。通過「位置」執行RecyclerView項目上的點擊事件
從onBindViewHolder()我能夠執行RecyclerView行(ViewHolder的項目)onClick事件。
是否可以使用Position來調用onClick()?
我在RecyclerView的適配器中。通過「位置」執行RecyclerView項目上的點擊事件
從onBindViewHolder()我能夠執行RecyclerView行(ViewHolder的項目)onClick事件。
是否可以使用Position來調用onClick()?
你可以做這樣的事情
適配器:
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);
}
}
這裏是例子:
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();
}
}
只需設置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()
}
};)
}
它不推薦,因爲你必須聲明它final
直接使用position
。因此,在提及onClick
時,請使用holder.getAdapterPosition()
代替。
是的,這是可能的。 –
請出示您的適配器。 –
@PatelJaimin你能幫我嗎? RecyclerView.ViewHolder的任何特定方法? –