Have a look at a custom callback

    private OnRecyclerItemClickListener mOnItemClickListener;

    public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public interface OnRecyclerItemClickListener {
        void onRecyclerItemClick(int position);
    }
Copy the code
/ / add RecyclerView dependent package implementation 'androidx. RecyclerView: RecyclerView: 1.1.0'Copy the code
package com.enjoy.leo_recyclerview; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.LinearLayout; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<Bean> data = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); for (int i = 9000; i < 20000; i++) { if (i % 4 ! = 0) { continue; } Bean bean = new Bean(); Bean.setname (" enjoy learning "+ I); data.add(bean); } RecyclerView recyclerView = findViewById(R.id.rv); // LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); // recyclerView.setLayoutManager(linearLayoutManager); GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3); recyclerView.setLayoutManager(gridLayoutManager); // StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, // LinearLayout.VERTICAL); // recyclerView.setLayoutManager(staggeredGridLayoutManager); MyAdapter myAdapter = new MyAdapter(data, this); recyclerView.setAdapter(myAdapter); myAdapter.setRecyclerItemClickListener(new MyAdapter.OnRecyclerItemClickListener() { @Override public void onRecyclerItemClick(int position) { Log.e("leo", "onRecyclerItemClick: " + position); }}); }}Copy the code
package com.enjoy.leo_recyclerview; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { private List<Bean> data; private Context context; public MyAdapter(List<Bean> data, Context context) { this.data = data; this.context = context; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.recyclerview_item, null); return new MyViewHolder(view); } @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { holder.tv.setText(data.get(position).getName()); } @Override public int getItemCount() { return data == null ? 0 : data.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { private TextView tv; public MyViewHolder(@NonNull View itemView) { super(itemView); tv = itemView.findViewById(R.id.tv); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mOnItemClickListener ! = null) { mOnItemClickListener.onRecyclerItemClick(getAdapterPosition()); }}}); } } private OnRecyclerItemClickListener mOnItemClickListener; public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener) { mOnItemClickListener = listener; } public interface OnRecyclerItemClickListener { void onRecyclerItemClick(int position); }}Copy the code
package com.enjoy.leo_listview; public class Bean { String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}Copy the code
<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" /> </LinearLayout>Copy the code
<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>Copy the code