Ticker

6/recent/ticker-posts

How to set onClickListener on RecyclerView

In this blog, we’re going to see how to set click listener in items of recyclerView. But if you’re not familiar with what is RecyclerView? And how to implement it than go through my previous blog. How to implementRecyclerView in Android.



As we already discussed that there are 3 main parts of recyclerView:

  1. The Card Layout: The card layout is an XML layout that will be treated as an item for the list created by the RecyclerView.
  2. The ViewHolder: The ViewHolder is a java class that stores the reference to the card layout views that can be modified during the execution of the program by a list of data obtained either by online databases or added in some another way.
  3. The Data Class: The Data class is a custom java class that acts as a structure for holding the information for every item of the RecyclerView.

 

But here we discuss click listener on recyclerView. Now the main question is why we need to click listener on recyclerView. Click listener is basically used when someone wants to see more about a particular thing because in recyclerView items there is limited spacing for texts or images. For adding more content on a particular item of recyclerView we need another activity or interface for items. For that, we need to set the click listener event on the items of recyclerView.

By using the setOnClickListener() method on items we are going to implement the click listener event. For that, we need to set the click event on the holder of every item on the adapter class that we’re implementing in the previous blog.


Code for the Click Listener

Implementation of the RecyclerView is already done. Go through the previous blog. How to implement RecyclerView in Android.

And make sure we add the RecyclerView dependency in the app gradle file.

activity_details.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".DetailsActivity">
    <LinearLayout
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
android:orientation="vertical"
       
android:layout_centerHorizontal="true"
       
android:gravity="center_horizontal">
        <ImageView
           
android:layout_width="200dp"
           
android:layout_height="150dp"
           
android:id="@+id/image"
           
android:layout_marginTop="100dp"
           
android:src="@mipmap/ic_launcher"/>
        <TextView
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:id="@+id/name"
           
android:text="Name"
           
android:textStyle="bold"
           
android:textSize="22sp"
           
android:layout_marginTop="8dp"
           
android:textColor="#000"/>
        <TextView
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content"
           
android:id="@+id/tag"
           
android:text="Tag"
           
android:textColor="#A5A5A5"
           
android:textSize="18sp"
           
android:textStyle="bold"/>
    </LinearLayout>
</RelativeLayout>

 

DetailsActivity.java

package com.example.recyclerview;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailsActivity extends AppCompatActivity {
    ImageView
imageView;
    TextView
name, tag;
    String
alphaName, alphaTag;
   
int alphaImage;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_details);

       
imageView = findViewById(R.id.image);
       
name = findViewById(R.id.name);
       
tag = findViewById(R.id.tag);
       
//get the intent
       
alphaName = getIntent().getStringExtra("name");
       
alphaTag = getIntent().getStringExtra("tag");
       
alphaImage = getIntent().getIntExtra("image",0);

       
//now set the get values in the respective widgets
       
name.setText(alphaName);
       
tag.setText(alphaTag);
       
imageView.setImageResource(alphaImage);
    }
}

Output



Post a Comment

0 Comments