如何在Recyclerview中获得一个项目的位置,当我点击它时?

问题描述 投票:0回答:1

基本上,我已经创建了一个回收视图,并在其中添加了项目,还创建了一个同样的适配器。

这是我的适配器。

 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);

            }



        });
    }

}

我的问题是,基本上我想返回在这个回收视图中点击的项目的位置,这样一旦我得到了位置,根据位置或点击的具体项目,我就可以在一个新的活动中查看它的内容。

android-studio android-recyclerview onclick position recycler-adapter
1个回答
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));

 }.......
  .......

}
});

}
© www.soinside.com 2019 - 2024. All rights reserved.