Hello Friends, Today I am going share how to use RecyclerView with click effect & click event handling on item in android. I know there is lot of tutorial available to do this but still I am sharing this because, every time I work with RecyclerView, I have to google for click effect and click event on recyclerview item each time. So, basically this code I am sharing specially for me. So, here is the code.
Item.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.zatackcoder.recyclerviewtest; public class Item { private String title; private String description; public Item(String title, String description) { this.title = title; this.description = description; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } |
MyAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package com.zatackcoder.recyclerviewtest; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { private Context context; private ArrayList<Item> items; public MyAdapter(Context context, ArrayList<Item> items) { this.context = context; this.items = items; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false); MyViewHolder mvh = new MyViewHolder(view); return mvh; } @Override public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int position) { myViewHolder.container.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String message = "You clicked on item which has index " + position + " and title " + items.get(position).getTitle() + " and description " + items.get(position).getDescription(); Toast.makeText(context, message, Toast.LENGTH_LONG).show(); } }); myViewHolder.title.setText(items.get(position).getTitle()); myViewHolder.description.setText(items.get(position).getDescription()); } @Override public int getItemCount() { return items.size(); } public static class MyViewHolder extends RecyclerView.ViewHolder { // each data item is just a string in this case View container; TextView title; TextView description; public MyViewHolder(View view) { super(view); container = view; title = view.findViewById(R.id.title); description = view.findViewById(R.id.description); } } } |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package com.zatackcoder.recyclerviewtest; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.lang.reflect.Array; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private RecyclerView.LayoutManager mLayoutManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); mRecyclerView.setHasFixedSize(true); mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); ArrayList items = new ArrayList<Item>(); items.add(new Item("Title 1", "Description 1")); items.add(new Item("Title 2", "Description 2")); items.add(new Item("Title 3", "Description 3")); items.add(new Item("Title 4", "Description 4")); items.add(new Item("Title 5", "Description 5")); items.add(new Item("Title 6", "Description 6")); items.add(new Item("Title 7", "Description 7")); items.add(new Item("Title 8", "Description 8")); items.add(new Item("Title 9", "Description 9")); items.add(new Item("Title 10", "Description 10")); items.add(new Item("Title 11", "Description 11")); items.add(new Item("Title 12", "Description 12")); items.add(new Item("Title 13", "Description 13")); items.add(new Item("Title 14", "Description 14")); items.add(new Item("Title 15", "Description 15")); mAdapter = new MyAdapter(getApplicationContext(), items); mRecyclerView.setAdapter(mAdapter); } } |
list_item.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?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="wrap_content" android:layout_margin="5dp" android:clickable="true" android:orientation="vertical" android:background="#ffffff"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" android:orientation="vertical" android:padding="5dp"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textFontWeight="bold" android:textSize="15dp" /> <TextView android:id="@+id/description" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="12dp" /> </LinearLayout> </LinearLayout> |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:listitem="@layout/list_item"/> </android.support.constraint.ConstraintLayout> |
Screenshots
AndroidStudio Project Download
RecyclerView with Click Effect & Click Event Handling on Item in Android
1 file(s) 12.85 MB
If you find this helpful then please share
Comments