当我在 Recyclerview 中单击某个项目时如何获取该项目的位置?
How to get the position of an item when i click on it, in a Recyclerview?
所以基本上我已经创建了一个 recyclerview 并向其中添加了项目,还为相同的项目创建了一个适配器
这是我的适配器:
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public ExampleViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView2);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position);
}
}
}
});
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v, mListener);
return evh;
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getmImageResource());
holder.mTextView1.setText(currentItem.getmText1());
holder.mTextView2.setText(currentItem.getmText2());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
这是我的主要activity:
private RecyclerView mRecyclerView;
private ExampleAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<ExampleItem> exampleList = new ArrayList<>();
exampleList.add(new ExampleItem(R.drawable.ic_android, "Line 1", "Line 2"));
exampleList.add(new ExampleItem(R.drawable.ic_audio, "Line 3", "Line 4"));
exampleList.add(new ExampleItem(R.drawable.ic_sun, "Line 5", "Line 6"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Line 7", "Line 8"));
exampleList.add(new ExampleItem(R.drawable.ic_audio, "Line 9", "Line 10"));
exampleList.add(new ExampleItem(R.drawable.ic_sun, "Line 11", "Line 12"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Line 13", "Line 14"));
exampleList.add(new ExampleItem(R.drawable.ic_audio, "Line 15", "Line 16"));
exampleList.add(new ExampleItem(R.drawable.ic_sun, "Line 17", "Line 18"));
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true); // set it as true if you know that your recyclerview will not change in size
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
exampleList.get(position);
}
});
}
}
我的问题基本上是我想 return 在这个 recyclerview 中点击的项目的位置,
这样一旦我得到位置,根据位置或单击的特定项目我可以在新的 activity.
中查看它的内容
在你的ExampleViewHolder
:
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
//add this
View view;
public ExampleViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
//set it here
view = itemView;
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
.......
.......
在你的 adapter
中创建一个接受 context
的构造函数
public class ExampleAdapter extends . ........
private Context context;
......
......
//constructor
public ExampleAdapter(.....,Context context){
.........
this.context = context;
}
将上下文从您的 activity
传递给构造函数
//pass the context of the activity here
mAdapter = new ExampleAdapter(exampleList,getApplicationContext());
......
.....
在onBindViewHolder
中使用context
根据位置打开新的activity:
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getmImageResource());
holder.mTextView1.setText(currentItem.getmText1());
holder.mTextView2.setText(currentItem.getmText2());
//on click item
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(position == 0){
//first item clicked
context.startActivity(new Intent(context, SomeActivity.class));
}else if(position == 1){
//second item clicked
context.startActivity(new Intent(context, SomeActivity.class));
}else if(position == 2){
//third item clicked
context.startActivity(new Intent(context, SomeActivity.class));
}.......
.......
}
});
}
所以基本上我已经创建了一个 recyclerview 并向其中添加了项目,还为相同的项目创建了一个适配器
这是我的适配器:
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public ExampleViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView2);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position);
}
}
}
});
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v, mListener);
return evh;
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getmImageResource());
holder.mTextView1.setText(currentItem.getmText1());
holder.mTextView2.setText(currentItem.getmText2());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
这是我的主要activity:
private RecyclerView mRecyclerView;
private ExampleAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<ExampleItem> exampleList = new ArrayList<>();
exampleList.add(new ExampleItem(R.drawable.ic_android, "Line 1", "Line 2"));
exampleList.add(new ExampleItem(R.drawable.ic_audio, "Line 3", "Line 4"));
exampleList.add(new ExampleItem(R.drawable.ic_sun, "Line 5", "Line 6"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Line 7", "Line 8"));
exampleList.add(new ExampleItem(R.drawable.ic_audio, "Line 9", "Line 10"));
exampleList.add(new ExampleItem(R.drawable.ic_sun, "Line 11", "Line 12"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Line 13", "Line 14"));
exampleList.add(new ExampleItem(R.drawable.ic_audio, "Line 15", "Line 16"));
exampleList.add(new ExampleItem(R.drawable.ic_sun, "Line 17", "Line 18"));
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true); // set it as true if you know that your recyclerview will not change in size
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
exampleList.get(position);
}
});
}
}
我的问题基本上是我想 return 在这个 recyclerview 中点击的项目的位置, 这样一旦我得到位置,根据位置或单击的特定项目我可以在新的 activity.
中查看它的内容在你的ExampleViewHolder
:
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
//add this
View view;
public ExampleViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
super(itemView);
//set it here
view = itemView;
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
.......
.......
在你的 adapter
中创建一个接受 context
public class ExampleAdapter extends . ........
private Context context;
......
......
//constructor
public ExampleAdapter(.....,Context context){
.........
this.context = context;
}
将上下文从您的 activity
//pass the context of the activity here
mAdapter = new ExampleAdapter(exampleList,getApplicationContext());
......
.....
在onBindViewHolder
中使用context
根据位置打开新的activity:
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getmImageResource());
holder.mTextView1.setText(currentItem.getmText1());
holder.mTextView2.setText(currentItem.getmText2());
//on click item
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(position == 0){
//first item clicked
context.startActivity(new Intent(context, SomeActivity.class));
}else if(position == 1){
//second item clicked
context.startActivity(new Intent(context, SomeActivity.class));
}else if(position == 2){
//third item clicked
context.startActivity(new Intent(context, SomeActivity.class));
}.......
.......
}
});
}