所以基本上我已经创建了一个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();
}
这是我的主要活动:
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);
}
});
}
}
我的问题基本上是我想返回在此recyclerview中单击的物品的位置, 这样,一旦我获得职位,就可以根据职位或单击的特定项目查看其在新活动中的内容。
答案 0 :(得分:1)
在您的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
打开新活动:
@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));
}.......
.......
}
});
}