不能在 RecyclerView 中使用 getLayoutPosition() 或 getAdapterPosition()
Can't use getLayoutPosition() or getAdapterPosition() in RecyclerView
我真的不知道为什么我不能在 class.
中使用 getLayoutPosition();
或 getAdapterPosition();
方法
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private ImageView image;
private TextView title;
private TextView price;
public MyViewHolder(View itemView) {
super(itemView);
image = (ImageView)itemView.findViewById(R.id.horizontal_list_image);
title = (TextView)itemView.findViewById(R.id.horizontal_list_title);
price = (TextView)itemView.findViewById(R.id.horizontal_list_price);
image.setOnClickListener(this);
title.setOnClickListener(this);
price.setOnClickListener(this);
}
public ImageView getImage() {
return image;
}
public TextView getTitle() {
return title;
}
public TextView getPrice() {
return price;
}
@Override
public void onClick(View v) {
Toast.makeText(context, "CLICK", Toast.LENGTH_SHORT).show();
getLayoutPosition(); //no method like that
}
}
我可以使用 getPosition()
但已弃用,建议使用上述方法。 documentation
getLayoutPosition()
和 getAdapterPosition()
是支持库修订版 22 的一部分。您无法解决它,因为您仍在使用版本 21。更改
'com.android.support:recyclerview-v7:21.0.+'
到
'com.android.support:recyclerview-v7:22.0.+'
同步 gradle,然后重试
我真的不知道为什么我不能在 class.
中使用getLayoutPosition();
或 getAdapterPosition();
方法
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private ImageView image;
private TextView title;
private TextView price;
public MyViewHolder(View itemView) {
super(itemView);
image = (ImageView)itemView.findViewById(R.id.horizontal_list_image);
title = (TextView)itemView.findViewById(R.id.horizontal_list_title);
price = (TextView)itemView.findViewById(R.id.horizontal_list_price);
image.setOnClickListener(this);
title.setOnClickListener(this);
price.setOnClickListener(this);
}
public ImageView getImage() {
return image;
}
public TextView getTitle() {
return title;
}
public TextView getPrice() {
return price;
}
@Override
public void onClick(View v) {
Toast.makeText(context, "CLICK", Toast.LENGTH_SHORT).show();
getLayoutPosition(); //no method like that
}
}
我可以使用 getPosition()
但已弃用,建议使用上述方法。 documentation
getLayoutPosition()
和 getAdapterPosition()
是支持库修订版 22 的一部分。您无法解决它,因为您仍在使用版本 21。更改
'com.android.support:recyclerview-v7:21.0.+'
到
'com.android.support:recyclerview-v7:22.0.+'
同步 gradle,然后重试