From the source code point of view to achieve RecyclerView Item Click Event, recyclerviewitem
Reprinted please indicate the source: http://www.cnblogs.com/cnwutianhao/p/6758373.html
As an alternative product of ListView and GridView, RecyclerView is believed to be widely used in the Android industry.
RecyclerView does not have a click event similar to that of ListView, but knows and uses it in two cases. We will analyze the RecyclerView click event from the source code perspective along with me.
First, let's take a look at Google's introduction to the family chart of ListView:
It can be seen that ListView is a View.
Let's take a look at Google's description of the OnItemClickListener interface:
The source code summary is comprehensive and detailed.
Next, let's take a look at the event where ListView calls onItemClick.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO }});
In fact, the RecyclerView can also use the onItemClick event. Please refer to the following link:
Example image (Material Design + Butter Knife + Snackbar ):
Implement RecyclerView click events
1. Import necessary Libraries
In the age of Material Design, Android requires the support of various libraries.
UI class library
compile 'com.android.support:appcompat-v7:25.3.1'compile 'com.android.support:cardview-v7:25.3.1'compile 'com.android.support:recyclerview-v7:25.3.1'compile 'com.android.support:design:25.3.1'
Butter Knife)
compile 'com.jakewharton:butterknife:8.5.1'annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
2. layout files (divided into the main interface and the Item layout Interface)
RecyclerView)
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" android:scrollbars="none" /></RelativeLayout>
Item layout interface (CardView)
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="80dp" android:layout_margin="10dp" android:background="@drawable/recycler_item_selector" app:cardBackgroundColor="@color/colorPrimary" app:cardCornerRadius="10dp" app:cardElevation="10dp"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:text="item" android:textColor="@android:color/white" android:textSize="24sp" /></android.support.v7.widget.CardView>
3. Custom Adapter class
Public class DemoAdapter extends RecyclerView. adapter <DemoAdapter. viewHolder> {private onRecyclerViewItemClick mOnRvItemClick; private Context mContext; String [] strings; public DemoAdapter (Context ctx, String [] strings, onRecyclerViewItemClick onRvItemClick) {mContext = ctx; this. mOnRvItemClick = onRvItemClick; this. strings = strings;} @ Override public ViewHolder onCreateViewHolder (ViewGroup pa Rent, int viewType) {View view = LayoutInflater. from (mContext ). inflate (R. layout. activity_main_item, parent, false); return new ViewHolder (view) ;}@ Override public void onBindViewHolder (ViewHolder holder, int position) {holder. setData (position) ;}@ Override public int getItemCount () {return 20;} public class ViewHolder extends RecyclerView. viewHolder implements View. onClickListener {@ BindView (R. id. textView) TextView textView; public ViewHolder (View itemView) {super (itemView); ButterKnife. bind (this, itemView); itemView. setOnClickListener (this);} public void setData (int position) {textView. setText ("+" + position + "") ;}@ Override public void onClick (View view) {if (mOnRvItemClick! = Null) mOnRvItemClick. onItemClick (view, getAdapterPosition () ;}/ *** item Click interface */public interface onRecyclerViewItemClick {void onItemClick (View v, int position );}}
4. Main class (onItemClick + Butter Knife + Snackbar)
Public class MainActivity extends AppCompatActivity {@ BindView (R. id. recycler_view) RecyclerView mRecyclerView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ButterKnife. bind (this); DemoAdapter myAdapter = new DemoAdapter (this, null, new DemoAdapter. onRecyclerViewItemClick () {@ Override public void onItemClick (View v, int position) {Snackbar. make (v, "nth" + position + "row", Snackbar. LENGTH_SHORT ). show () ;}}); mRecyclerView. setLayoutManager (new LinearLayoutManager (this, LinearLayoutManager. VERTICAL, false); mRecyclerView. setAdapter (myAdapter );}}
Demo: RecyclerView's Item Click Event
Follow my Sina Weibo account and check for the yellow V certification to obtain the latest Android development information.
Focus on science and technology critics, learn about science and technology, innovation, education, and maximize human wisdom and imagination!