这是我用来获取recyclerview物品的位置并执行以下操作的准确方法:
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
}
我将imagebutton放在膨胀我的recyclerview的文件中,如下图所示:
我的图像按钮位于播放视频的活动中,如下图所示。 [![图片] 这是我的适配器:
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
private List<Data> data= Collections.emptyList();
Data current;
private int currentPos=0;
// create constructor to innitilize context and data sent from MainActivity
public Adapter(Context context, List<Data> data){
this.context=context;
inflater= LayoutInflater.from(context);
this.data=data;
}
// Inflate the layout when viewholder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.container, parent,false);
MyHolder holder=new MyHolder(view);
return holder;
}
// Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
// Obtenir la position actuelle de l'élément dans recyclerview pour lier les données et affecter des valeurs à partir de la liste
MyHolder myHolder= (MyHolder) holder;
Data current = data.get(position);
myHolder.textFishName.setText(current.catName);
myHolder.textSize.setText(current.sizeName);
myHolder.textSize.setVisibility(View.GONE);
myHolder.textType.setText(current.fishName);
myHolder.id.setText(""+current.idServer+"");
myHolder.textPrice.setText("" + current.price + "");
myHolder.textPrice.setVisibility(View.GONE);
myHolder.textPrice.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
// load image into imageview using glide
Glide.with(context).load("http://192.168.43.196/vibe2/images/" + current.fishImage)
.placeholder(R.drawable.ic_menu_camera)
.error(R.drawable.ic_menu_camera)
.into(myHolder.ivFish);
}
// return total item from List
@Override
public int getItemCount() {
return data.size();
}
class MyHolder extends RecyclerView.ViewHolder {
TextView textFishName;
ImageView ivFish;
TextView textSize;
TextView textType;
TextView textPrice;
TextView counter;
TextView id;
// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
textFishName = itemView.findViewById(R.id.textFishName);
ivFish = itemView.findViewById(R.id.ivFish);
textSize = itemView.findViewById(R.id.textSize);
textType = itemView.findViewById(R.id.textType);
textPrice = itemView.findViewById(R.id.textPrice);
counter = itemView.findViewById(R.id.counter);
id = itemView.findViewById(R.id.id);
}
}
}
答案 0 :(得分:0)
可以使用从活动到RecyclerView的ViewHolder的界面来存档。在ViewHolder中,您可以实现onClickListener,在其中您还可以从ViewHolder中传递点击平均数和所需信息。
public class Adapter extends RecyclerView.Adapter<MyHolder> {
private Context context;
private LayoutInflater inflater;
private List<Data> data= Collections.emptyList();
Data current;
private int currentPos=0;
// Custom listener to relay to selected data from ViewHolder to Activity
private AdapterListener adapterListener;
// create constructor to innitilize context and data sent from MainActivity
public Adapter(Context context, List<Data> data, AdapterListener adapterListener){
this.context=context;
inflater= LayoutInflater.from(context);
this.data=data;
this.adapterListener = adapterListener;
}
// Inflate the layout when viewholder created
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.container, parent,false);
MyHolder holder=new MyHolder(view, adapterListener);
return holder;
}
// Bind data
@Override
public void onBindViewHolder(MyHolder holder, int position) {
// Obtenir la position actuelle de l'élément dans recyclerview pour lier les données et affecter des valeurs à partir de la liste
MyHolder myHolder= (MyHolder) holder;
myHolder.setData(data.get(position));
}
// return total item from List
@Override
public int getItemCount() {
return data.size();
}
}
public class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView textFishName;
ImageView ivFish;
TextView textSize;
TextView textType;
TextView textPrice;
TextView counter;
TextView id;
Data data;
// Adapter listener
private AdapterListener adapterListener;
// create constructor to get widget reference
public MyHolder(View itemView, AdapterListener adapterListener) {
super(itemView);
textFishName = itemView.findViewById(R.id.textFishName);
ivFish = itemView.findViewById(R.id.ivFish);
textSize = itemView.findViewById(R.id.textSize);
textType = itemView.findViewById(R.id.textType);
textPrice = itemView.findViewById(R.id.textPrice);
counter = itemView.findViewById(R.id.counter);
id = itemView.findViewById(R.id.id);
this.adapterListener = adapterListener;
}
public void setData(Data data){
this.data = data;
textFishName.setText(current.catName);
textSize.setText(current.sizeName);
textSize.setVisibility(View.GONE);
textType.setText(current.fishName);
id.setText(""+current.idServer+"");
textPrice.setText("" + current.price + "");
textPrice.setVisibility(View.GONE);
textPrice.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
// load image into imageview using glide
Glide.with(context).load("http://192.168.43.196/vibe2/images/" + current.fishImage)
.placeholder(R.drawable.ic_menu_camera)
.error(R.drawable.ic_menu_camera)
.into(ivFish);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.ivFish:
adapterListener.onClickImageView(getAdapterPosition(), data);
break;
}
}
}
public interface AdapterListener {
void onClickImageView(int position, Data data);
}
最后,在创建适配器的类中实现AdapterListener接口。
答案 1 :(得分:0)
尝试此代码。 使接口进入适配器类并处理click事件,如下面的代码。
OnItemClick onItemClick;
public void setOnItemClick(OnItemClick onItemClick) {
this.onItemClick = onItemClick;
}
public interface OnItemClick {
void getPosition(int pos); //pass any things
}
然后……。
@Override
public void onBindViewHolder(ItemViewHolder holder, final int position) {
// below code handle click event on recycler view item.
String data = mStringList.get(position);
holder.textView.setText(data);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClick.getPosition(position);
}
});
}
在活动适配器中将其绑定到listview或recycler视图之后,然后在调用以下代码后,它意味着adaper不为空。.
adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() {
@Override
public void getPosition(int pos) {
Toast.makeText(getApplicationContext(),"Click Item Postion::"+pos,Toast.LENGTH_SHORT).show();
}
});
答案 2 :(得分:0)
您应该在ViewHolder
内而不是onBindViewHolder
内进行此操作,因为每次重新绘制项目时都会调用它。
...
// create constructor to get widget reference
public MyHolder(View itemView) {
super(itemView);
...
ivFish = itemView.findViewById(R.id.ivFish);
...
ivFish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(getAdapterPosition() != -1) { // Avoid Exception
// use getAdapterPosition() to get the current position
}
}
});
}
您还可以将接口传递给RecyclerView适配器,在ClickListener中调用此接口,然后在“片段/活动”中执行所需的操作。