请帮助我解决问题。问题是我需要获得卡位置以将此位置传达给另一个活动。当我按下卡上的按钮时,我需要转移这个位置。如何获得卡位?
RVAdapter.java
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
private final static String myLog = "myLog";
List<ItemsOfRow> items;
private Context context;
RVAdapter(List<ItemsOfRow> items, Context context) {
this.items = items;
this.context = context;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_view, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
personViewHolder.imgGroupImage.setImageBitmap(items.get(i).getTitleImage());
personViewHolder.txtTitleGroup.setText(items.get(i).getTitleName());
personViewHolder.txtContentGroup.setText(items.get(i).getTitleContent());
personViewHolder.btnShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, items.get(????).getTitleContent());
sendIntent.setType("text/plain");
context.startActivity(sendIntent);
}
});
personViewHolder.btnLearnMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, QuoteActivity.class);
intent.putExtra("title",items.get(?????).getTitleName());
intent.putExtra("positionGroup", ?????? + "");
view.getContext().startActivity(intent);
}
});
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class PersonViewHolder extends RecyclerView.ViewHolder{
CardView cv;
TextView txtTitleGroup;
TextView txtContentGroup;
ImageView imgGroupImage;
Button btnShare;
Button btnLearnMore;
PersonViewHolder(final View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
imgGroupImage = (ImageView) itemView.findViewById(R.id.img_group_image);
txtTitleGroup = (TextView) itemView.findViewById(R.id.txt_title_group);
txtContentGroup = (TextView) itemView.findViewById(R.id.txt_content_group);
btnShare = (Button) itemView.findViewById(R.id.btn_share_group);
btnLearnMore = (Button) itemView.findViewById(R.id.btn_learn_more);
}
}
}
答案 0 :(得分:5)
您可以使用viewHolder的方法getAdapterPosition()
答案 1 :(得分:0)
int i
的第二个参数onBindViewHolder()
实际上是要创建的卡片的位置。您只需将i
传递给您的意图。
在onBindViewHolder
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
/* rest of your code */
personViewHolder.btnLearnMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, QuoteActivity.class);
intent.putExtra("title",items.get(i).getTitleName());
intent.putExtra("positionGroup", i);
view.getContext().startActivity(intent);
}
});
}
答案 2 :(得分:0)
recyclerView.getChildLayoutPosition(child)
这将返回Recycler视图中视图的位置。 这里child是注册click事件的View。
完整点击侦听器代码可以类似于:
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildLayoutPosition(child));
}
}
});