Open In App

How to Access any Component Outside RecyclerView from RecyclerView in Android?

Last Updated : 09 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The title may be a little confusing but what we want to say is, suppose in an Android App, there’s a RecyclerView, and outside that there’s a TextView. In the RecyclerView there’s a list of clickable components, said Button. Now what I want is, for different button clicks, the text in the TextView will be different. So this is basically controlling a component outside RecyclerView, from a RecyclerView.

As per the picture, there’s a RecyclerView with four Buttons (1 hidden at the left) and a TextView. For different Button clicks different texts are showing up in the TextView. In this article, the same concept will be implemented with a little real-world touch. So the final app will show a list and a sub-list of the Tutorials offered by GEEKS FOR GEEKS. Here’s a demo GIF of the final app:

Prerequisites:

Step 1: Working with the activity_main.xml file

  • Go to the res -> layout -> activity_main.xml file.
  • Here is the code of that XML file:

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:orientation="vertical"
        android:padding="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
  
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
  
            <TextView
                style="@style/MaterialAlertDialog.MaterialComponents.Title.Icon"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="GEEKS FOR GEEKS TUTORIALS"
                android:textAlignment="center"
                android:textSize="25sp"
                android:textStyle="bold" />
  
  
        </LinearLayout>
  
        <!-- The horizontal recyclerView where the 
             Main-List items will be showed -->
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerViewOne"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_marginTop="20dp"
            android:clipToPadding="false"
            android:foregroundGravity="center"
            android:orientation="horizontal" />
  
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:gravity="center">
  
            <!-- The TextView where the Main-List item 
                 title text will be showed -->
            <TextView
                android:id="@+id/algorithmTitleText"
                style="@style/ShapeAppearance.MaterialComponents.MediumComponent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:textSize="22sp" />
  
        </LinearLayout>
  
        <!-- The vertical recyclerView where the Sub-List 
             items will be showed -->
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerViewTwo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="20dp"
            android:clipToPadding="false"
            android:foregroundGravity="center" />
  
    </LinearLayout>
  
</androidx.constraintlayout.widget.ConstraintLayout>


  • Here, two RecyclerView is used, one horizontal (recyclerViewOne) and one vertical (recyclerViewTwo). The first one shows the main list and the second one shows the sub-list of the main list item.
  • A TextView is also used to show the main list-item text.

Step 2: Add single_card_layout.xml for the main list items

  • Go to the res -> layout
  • Right Click on the layout folder
  • Go to New -> Layout Resource File
  • Type the file name (single_card_layout.xml for me)
  • Change the root element to RelativeLayout
  • Click OK

  • Here is the code of this XML file:

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
  
    <androidx.cardview.widget.CardView
        android:id="@+id/singleItemCardView"
        android:layout_width="145dp"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:elevation="10dp"
        app:cardCornerRadius="30dp"
        android:clickable="true"
        android:focusable="true"
        android:foreground="?android:attr/selectableItemBackground">
  
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_gravity="center">
  
            <TextView
                android:id="@+id/singleItemTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingStart="10dp"
                android:paddingEnd="10dp"
                android:text="TEST TEXT"
                android:gravity="center"
                android:textAlignment="center"
                android:textColor="@android:color/black"
                android:textSize="15sp"
                android:textAllCaps="true"
                android:letterSpacing="0.2" />
  
        </RelativeLayout>
    </androidx.cardview.widget.CardView>
</RelativeLayout>


  • This is basically the layout of the items shown in the recyclerViewOne or the main list or the horizontal RecyclerView.

Step 3: Add single_card_layout_vertical.xml for the sub-list items

  • Go to the res -> layout
  • Right Click on the layout folder
  • Go to New -> Layout Resource File
  • Type the file name (single_card_layout_vertical.xml as per this example)
  • Change the root element to RelativeLayout
  • Click OK
  • Here is the code of this XML file:

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">
  
    <androidx.cardview.widget.CardView
        android:id="@+id/singleItemCardView"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:clickable="true"
        android:elevation="10dp"
        android:focusable="true"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardCornerRadius="5dp">
  
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center_vertical"
            android:padding="5dp">
  
            <TextView
                android:id="@+id/singleItemTextViewRVTwo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:letterSpacing="0.2"
                android:paddingStart="10dp"
                android:paddingEnd="10dp"
                android:text="TEST TEXT"
                android:textAlignment="inherit"
                android:textAllCaps="true"
                android:textColor="@android:color/black"
                android:textSize="15sp" />
  
        </RelativeLayout>
  
    </androidx.cardview.widget.CardView>
</RelativeLayout>


  • This is the layout of the items shown in the recyclerViewTwo or the sub list or the vertical RecyclerView.

Step 4: Working on the horizontal RecyclerView or the main list

  • Go to java ->com.wheic.recyclergetsout (Your’s may differ)
  • Make a directory under com.wheic.recyclergetsout (I named it RV1). Making a directory from Android Studio is weird, there’s a lot of bugs in the process (at least for me). So you can follow these steps:

  • Right-click on com.wheic.recyclergetsout (Your’s may differ)
  • com.wheic.recyclergetsout -> New -> Sample Data Directory

  • Go to Build -> Rebuild Project

  • A sampledata folder will be seen under the app folder.

  • Right-click on it. Go to Refactor -> Rename.

  • Give the name of the folder. (Here we named it RV1).
  • Click Refactor.

  • Drag the folder on to com.wheic.recyclergetsout
  • Click Refactor in the pop-up window.

  • Go to Build -> Rebuild Project.
  • OR, If don’t wanna do all these, just directly go to the Explorer and make a directory there. For this right-click on com.wheic.recyclergetsout -> Show in Explorer. Then create a folder there manually.

Start working on the corresponding model class:

  1. Right-click on the just created folder (RV1) -> New -> Java Class. Give the name of the class. I named it RVOneModel.
  2. So basically I am using two variables, one String type, and one integer. The String variable will be used to store the title of the list item and the integer variable to differentiate each item. Then a constructor with both the variables and only the Getter functions are made for these two variables.
  3. Here’s the java code of the model class:

Java




public class RVOneModel {
       // this variable will store main-list item title
    private String name;
         
      // this will differentiate between the main-list items
    private int num;
  
    // parameterized constructor
    public RVOneModel(String name, int num) {
        this.name = name;
        this.num = num;
    }
  
    // getter functions for these two variables
    public String getName() {
        return name;
    }
  
    public int getNum() {
        return num;
    }
}


Start working on the Adapter class for the horizontal RecyclerView:

  1. So, right-click on the just created folder (RV1) -> New -> Java Class. Give the name of the class. I named it RVOneAdapter.
  2. So with the adapter, we also need a ViewHolder class.
  3. The use of these classes and every other method are already clearly described in this GFG link.
  4. Here’s the java code of this Adapter class with the ViewHolder class:

Java




import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.wheic.recyclergetsout.R;
import java.util.List;
  
public class RVOneAdapter extends RecyclerView.Adapter<RVOneAdapter.ViewHolder> {
  
    // Main-list item titles will be stored here
      private List<RVOneModel> tutorialList; 
    
    // Parameterized constructor of this 
      // class to initialize tutorialList
    public RVOneAdapter(List<RVOneModel> tutorialList) {
        this.tutorialList = tutorialList;
  
    }
  
    // Attach the item layout with the proper xml file
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_card_layout, parent, false);
        return new ViewHolder(view);
    }
  
    // It deals with the setting of different data and methods
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final RVOneModel modelItems = tutorialList.get(position);
        holder.setData(tutorialList.get(position).getName());
    }
  
    // It returns the length of the RecyclerView
    @Override
    public int getItemCount() {
        return tutorialList.size();
    }
  
    // The ViewHolder is a java class that stores 
      // the reference to the item layout views
    public class ViewHolder extends RecyclerView.ViewHolder{
  
        public CardView singleItemCardView;
        public TextView singleItemTextView;
  
        //Link up the Main-List items layout 
          // components with their respective id
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            singleItemCardView = itemView.findViewById(R.id.singleItemCardView);
            singleItemTextView = itemView.findViewById(R.id.singleItemTextView);
        }
  
        // setText in Main-List title text
        public void setData(String name){
            this.singleItemTextView.setText(name);
        }
    }
}


Start working on the MainActivity.java to add the first RecyclerView:

  1. The tutorialList is stored with Main-List item title string variables one by one and then the adapter is set to the horizontal RecyclerView.
  2. Here is the code in MainActivity.java to add the first RecyclerView:

Java




import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.wheic.recyclergetsout.RV1.RVOneAdapter;
import com.wheic.recyclergetsout.RV1.RVOneModel;
import java.util.ArrayList;
import java.util.List;
  
public class MainActivity extends AppCompatActivity {
  
      // reference for the Main-List RecyclerView
    private RecyclerView RVOne; 
      // Main-list item titles will be stored here
    private List<RVOneModel> tutorialList; 
      // reference for the RVOneAdapter class
    private RVOneAdapter rvOneAdapter; 
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Linked up with its respective id 
          // used in the activity_main.xml
        RVOne = findViewById(R.id.recyclerViewOne);
        RVTwo = findViewById(R.id.recyclerViewTwo);
  
        // Setting the Main-List RecyclerView horizontally
        RVOne.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
  
        tutorialList = new ArrayList<>();
  
        // Static data are stored one by one in the tutorialList arrayList
        tutorialList.add(new RVOneModel("Algorithms", 1));
        tutorialList.add(new RVOneModel("Data Structures", 2));
        tutorialList.add(new RVOneModel("Languages", 3));
        tutorialList.add(new RVOneModel("Interview Corner", 4));
        tutorialList.add(new RVOneModel("GATE", 5));
        tutorialList.add(new RVOneModel("ISRO CS", 6));
        tutorialList.add(new RVOneModel("UGC NET CS", 7));
        tutorialList.add(new RVOneModel("CS Subjects", 8));
        tutorialList.add(new RVOneModel("Web Technologies", 9));
  
        rvOneAdapter = new RVOneAdapter(tutorialList);
        RVOne.setAdapter(rvOneAdapter);
    }
}


 

Step 5: Set up the clickListener for each item in the horizontal RecyclerView:

  • The next 4 steps are done in RVOneAdapater.java
  • First, an interface is created in RVOneAdapater.java for each item click with an onItemClick abstract method.

Java




// Interface to perform events on Main-List item click
public interface OnItemsClickListener{
    void onItemClick(RVOneModel rvOneModel);
}


  • A reference variable of the interface is created.

Java




// Need this for the Main-list item onClick events
private OnItemsClickListener listener;


  • A method setWhenClickListener is created that will be called from the MainActivity.

Java




// Main-list item clickListener is initialized
// This will be used in MainActivity
public void setWhenClickListener(OnItemsClickListener listener){
    this.listener = listener;
}


  • Now, inside onBindViewHolder, a setOnClickListener is created that will actually call the onItemClick method when a single item in the horizontal RecyclerView is clicked.

Java




holder.singleItemCardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(listener != null){
            listener.onItemClick(modelItems);
        }
    }
});


  • In MainActivity.java, a reference variable is made for the TextView and it is attached with the XML file using the corresponding id.

Java




algorithmTitleText = findViewById(R.id.algorithmTitleText);


  • Now, adapter setOnClickListener is called and the textView text change event will occur when a single list-item is clicked.

Java




rvOneAdapter.setWhenClickListener(new RVOneAdapter.OnItemsClickListener() {
    @Override
    public void onItemClick(RVOneModel rvOneModel) {
        algorithmTitleText.setText(rvOneModel.getName());
        setRVTwoList(rvOneModel.getNum());
    }
});


Step 6: Work on the vertical RecyclerView or the sub-list

  • Go to java -> com.wheic.recyclergetsout (Your’s may differ)
  • Just like the previous one, a directory is made. I named it RV2.

Start working on the model class for the sub-list:

  1. Right-click on the just created folder (RV2) -> New -> Java Class. Give the name of the class. for me it’s RVTwoModel.
  2. As per this app, in this model class, only one String variable is necessary. The String variable will store the title text of the sub-list item. Then just like the previous one a constructor and a getter function are created.
  3. Here’s the java code of the model class.

Java




public class RVTwoModel {
      // this variable will store sub-list item title
    private String name; 
  
    // parameterized constructor
    public RVTwoModel(String name) {
        this.name = name;
    }
  
    // getter function for the name variable
    public String getName() {
        return name;
    }
}


Start working on the Adapter class for the vertical RecyclerView:

  1. Right-click on the just created folder (RV2) -> New -> Java Class. Give the name of the class. I named it RVTwoAdapter.
  2. Just like any other RecyclerView, this adapter class is also not that different.
  3. Here’s the java code of this Adapter class and the ViewHolder class:

Java




import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.wheic.recyclergetsout.R;
import java.util.List;
  
public class RVTwoAdapter extends RecyclerView.Adapter<RVTwoAdapter.ViewHolder> {
  
       // Sub-list item titles will be stored here
    private List<RVTwoModel> tutorialSubList;
  
    // Parameterized constructor of this class 
      // to initialize tutorialSubList
    public RVTwoAdapter(List<RVTwoModel> tutorialSubList) {
        this.tutorialSubList = tutorialSubList;
    }
  
    // Attach the item layout with the proper xml file
    @NonNull
    @Override
    public RVTwoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_card_layout_vertical, parent, false);
        return new ViewHolder(view);
    }
  
    // It deals with the setting of different data and methods
    @Override
    public void onBindViewHolder(@NonNull RVTwoAdapter.ViewHolder holder, int position) {
        holder.setData(tutorialSubList.get(position).getName());
    }
  
    // It returns the length of the RecyclerView
    @Override
    public int getItemCount() {
        return tutorialSubList.size();
    }
  
    // The ViewHolder is a java class that stores 
      // the reference to the item layout views
    public class ViewHolder extends RecyclerView.ViewHolder{
  
        public TextView rvTwoText;
  
        // Link up the Sub-List items layout 
          // components with their respective id
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            rvTwoText = itemView.findViewById(R.id.singleItemTextViewRVTwo);
        }
  
        // setText in Sub-List title text
        public void setData(String name){
            this.rvTwoText.setText(name);
        }
    }
}


Let’s finish the code in MainActivity.java:

  1. A second ArrayList tutorialSubList for the sub-list is needed. So a reference variable of a second ArrayList is created.

Java




// Sub-list item titles will be stored here
private List<RVTwoModel> tutorialSubList;


  1. A function is created just for the vertical recyclerView, which takes an integer parameter.
  2. The static data is added in the ArrayList for each main-list item.

Here is the code for the function:

Java




private void setRVTwoList(int num){
  
    // Setting the Sub-List RecyclerView vertically
    RVTwo.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
  
    // Previous tutorialSubList will be deleted 
      // and new memory will be allocated
    tutorialSubList = new ArrayList<>();
  
    // Static data are stored one by one in the
      // tutorialSubList arrayList for each Main-List items
    if (num == 1) {
  
        tutorialSubList.add(new RVTwoModel("Searching Algorithms"));
        tutorialSubList.add(new RVTwoModel("Sorting Algorithms"));
        tutorialSubList.add(new RVTwoModel("Graph Algorithms"));
        tutorialSubList.add(new RVTwoModel("Pattern Algorithms"));
        tutorialSubList.add(new RVTwoModel("Geometric Algorithms"));
        tutorialSubList.add(new RVTwoModel("Mathematical"));
        tutorialSubList.add(new RVTwoModel("Randomized Algorithms"));
        tutorialSubList.add(new RVTwoModel("Greedy Algorithms"));
        tutorialSubList.add(new RVTwoModel("Dynamic Programming"));
        tutorialSubList.add(new RVTwoModel("Divide and Conquer"));
        tutorialSubList.add(new RVTwoModel("Backtracking"));
        tutorialSubList.add(new RVTwoModel("Branch and Bound"));
        tutorialSubList.add(new RVTwoModel("All Algorithms"));
  
    } else if (num == 2){
  
        tutorialSubList.add(new RVTwoModel("Arrays"));
        tutorialSubList.add(new RVTwoModel("Linked List"));
        tutorialSubList.add(new RVTwoModel("Stack"));
        tutorialSubList.add(new RVTwoModel("Queue"));
        tutorialSubList.add(new RVTwoModel("Binary Tree"));
        tutorialSubList.add(new RVTwoModel("Binary Search Tree"));
        tutorialSubList.add(new RVTwoModel("Heap"));
        tutorialSubList.add(new RVTwoModel("Hashing"));
        tutorialSubList.add(new RVTwoModel("Graph"));
        tutorialSubList.add(new RVTwoModel("Advanced Data Structure"));
        tutorialSubList.add(new RVTwoModel("Matrix"));
        tutorialSubList.add(new RVTwoModel("Strings"));
        tutorialSubList.add(new RVTwoModel("All Data Structures"));
  
    } else if (num == 3){
  
        tutorialSubList.add(new RVTwoModel("C"));
        tutorialSubList.add(new RVTwoModel("C++"));
        tutorialSubList.add(new RVTwoModel("Java"));
        tutorialSubList.add(new RVTwoModel("Python"));
        tutorialSubList.add(new RVTwoModel("C#"));
        tutorialSubList.add(new RVTwoModel("Javascript"));
        tutorialSubList.add(new RVTwoModel("JQuery"));
        tutorialSubList.add(new RVTwoModel("SQL"));
        tutorialSubList.add(new RVTwoModel("PHP"));
        tutorialSubList.add(new RVTwoModel("Scala"));
        tutorialSubList.add(new RVTwoModel("Perl"));
        tutorialSubList.add(new RVTwoModel("GO Language"));
        tutorialSubList.add(new RVTwoModel("HTML"));
        tutorialSubList.add(new RVTwoModel("CSS"));
        tutorialSubList.add(new RVTwoModel("Kotlin"));
  
    } else if (num == 4){
  
        tutorialSubList.add(new RVTwoModel("Company Preparation"));
        tutorialSubList.add(new RVTwoModel("Top Topics"));
        tutorialSubList.add(new RVTwoModel("Practice Company Questions"));
        tutorialSubList.add(new RVTwoModel("Interview Experiences"));
        tutorialSubList.add(new RVTwoModel("Experienced Interviews"));
        tutorialSubList.add(new RVTwoModel("Internship Interviews"));
        tutorialSubList.add(new RVTwoModel("Competitive Programming"));
        tutorialSubList.add(new RVTwoModel("Design Patterns"));
        tutorialSubList.add(new RVTwoModel("Multiple Choice Quizzes"));
  
    } else if (num == 5){
  
        tutorialSubList.add(new RVTwoModel("GATE CS Notes 2021"));
        tutorialSubList.add(new RVTwoModel("Last Minute Notes"));
        tutorialSubList.add(new RVTwoModel("GATE CS Solved Papers"));
        tutorialSubList.add(new RVTwoModel("GATE CS Original Papers and Official Keys"));
        tutorialSubList.add(new RVTwoModel("GATE 2021 Dates"));
        tutorialSubList.add(new RVTwoModel("GATE CS 2021 Syllabus"));
        tutorialSubList.add(new RVTwoModel("Important Topics for GATE CS"));
        tutorialSubList.add(new RVTwoModel("Sudo GATE 2021"));
  
    } else if (num == 6){
  
        tutorialSubList.add(new RVTwoModel("ISRO CS Solved Papers"));
        tutorialSubList.add(new RVTwoModel("ISRO CS Original Papers and Official Keys"));
        tutorialSubList.add(new RVTwoModel("ISRO CS Syllabus for Scientist/Engineer Exam"));
  
    } else if (num == 7){
        tutorialSubList.add(new RVTwoModel("UGC NET CS Notes Paper II"));
        tutorialSubList.add(new RVTwoModel("UGC NET CS Notes Paper III"));
        tutorialSubList.add(new RVTwoModel("UGC NET CS Solved Papers"));
  
    } else if (num == 8){
  
        tutorialSubList.add(new RVTwoModel("Mathematics"));
        tutorialSubList.add(new RVTwoModel("Operating System"));
        tutorialSubList.add(new RVTwoModel("DBMS"));
        tutorialSubList.add(new RVTwoModel("Computer Networks"));
        tutorialSubList.add(new RVTwoModel("Computer Organization and Architecture"));
        tutorialSubList.add(new RVTwoModel("Theory of Computation"));
        tutorialSubList.add(new RVTwoModel("Compiler Design"));
        tutorialSubList.add(new RVTwoModel("Digital Logic"));
        tutorialSubList.add(new RVTwoModel("Software Engineering"));
  
    } else if (num == 9){
  
        tutorialSubList.add(new RVTwoModel("HTML"));
        tutorialSubList.add(new RVTwoModel("CSS"));
        tutorialSubList.add(new RVTwoModel("Javascript"));
        tutorialSubList.add(new RVTwoModel("jQuery"));
        tutorialSubList.add(new RVTwoModel("PHP"));
  
    }
  
    rvTwoAdapter = new RVTwoAdapter(tutorialSubList);
    RVTwo.setAdapter(rvTwoAdapter);
}


  • Inside onCreate(), the setRVTwo() function is called in the main-list item clickListener. Also, it is called outside because we need to see the sub-list just when the activity gets created.

Java




// The app will show Algorithms Sub-List
// every time the activity starts
algorithmTitleText.setText("Algorithms");
setRVTwoList(1);


 

Here are every final JAVA codes of this project:

MainActivity.java

Java




import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.wheic.recyclergetsout.RV1.RVOneAdapter;
import com.wheic.recyclergetsout.RV1.RVOneModel;
import com.wheic.recyclergetsout.RV2.RVTwoAdapter;
import com.wheic.recyclergetsout.RV2.RVTwoModel;
import java.util.ArrayList;
import java.util.List;
  
public class MainActivity extends AppCompatActivity {
    
      // reference for the Main-List RecyclerView
    private RecyclerView RVOne; 
      // reference for the Sub-List RecyclerView
    private RecyclerView RVTwo; 
      // Main-list item titles will be stored here
    private List<RVOneModel> tutorialList; 
      // Sub-list item titles will be stored here
    private List<RVTwoModel> tutorialSubList; 
      // reference for the RVOneAdapter class
    private RVOneAdapter rvOneAdapter; 
      // reference for the RVTwoAdapter class
    private RVTwoAdapter rvTwoAdapter; 
      // TextView to show the title of the clicked Main-List item
    private TextView algorithmTitleText; 
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Linked up with its respective id used in the activity_main.xml
        RVOne = findViewById(R.id.recyclerViewOne);
        RVTwo = findViewById(R.id.recyclerViewTwo);
        algorithmTitleText = findViewById(R.id.algorithmTitleText);
  
        // Setting the Main-List RecyclerView horizontally
        RVOne.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
  
        tutorialList = new ArrayList<>();
  
        // Static data are stored one by one in the tutorialList arrayList
        tutorialList.add(new RVOneModel("Algorithms", 1));
        tutorialList.add(new RVOneModel("Data Structures", 2));
        tutorialList.add(new RVOneModel("Languages", 3));
        tutorialList.add(new RVOneModel("Interview Corner", 4));
        tutorialList.add(new RVOneModel("GATE", 5));
        tutorialList.add(new RVOneModel("ISRO CS", 6));
        tutorialList.add(new RVOneModel("UGC NET CS", 7));
        tutorialList.add(new RVOneModel("CS Subjects", 8));
        tutorialList.add(new RVOneModel("Web Technologies", 9));
  
        // The app will show Algorithms Sub-List every time the activity starts
        algorithmTitleText.setText("Algorithms");
        setRVTwoList(1);
  
        rvOneAdapter = new RVOneAdapter(tutorialList);
        RVOne.setAdapter(rvOneAdapter);
  
        // Setting up the events that will occur on each Main-List item click
        rvOneAdapter.setWhenClickListener(new RVOneAdapter.OnItemsClickListener() {
            @Override
            public void onItemClick(RVOneModel rvOneModel) {
                algorithmTitleText.setText(rvOneModel.getName());
                setRVTwoList(rvOneModel.getNum());
            }
        });
  
    }
  
    private void setRVTwoList(int num){
  
        // Setting the Sub-List RecyclerView vertically
        RVTwo.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
  
        // Previous tutorialSubList will be deleted and new memory will be allocated
        tutorialSubList = new ArrayList<>();
  
        // Static data are stored one by one in the tutorialSubList arrayList for each Main-List items
        if (num == 1) {
  
            tutorialSubList.add(new RVTwoModel("Searching Algorithms"));
            tutorialSubList.add(new RVTwoModel("Sorting Algorithms"));
            tutorialSubList.add(new RVTwoModel("Graph Algorithms"));
            tutorialSubList.add(new RVTwoModel("Pattern Algorithms"));
            tutorialSubList.add(new RVTwoModel("Geometric Algorithms"));
            tutorialSubList.add(new RVTwoModel("Mathematical"));
            tutorialSubList.add(new RVTwoModel("Randomized Algorithms"));
            tutorialSubList.add(new RVTwoModel("Greedy Algorithms"));
            tutorialSubList.add(new RVTwoModel("Dynamic Programming"));
            tutorialSubList.add(new RVTwoModel("Divide and Conquer"));
            tutorialSubList.add(new RVTwoModel("Backtracking"));
            tutorialSubList.add(new RVTwoModel("Branch and Bound"));
            tutorialSubList.add(new RVTwoModel("All Algorithms"));
  
        } else if (num == 2){
  
            tutorialSubList.add(new RVTwoModel("Arrays"));
            tutorialSubList.add(new RVTwoModel("Linked List"));
            tutorialSubList.add(new RVTwoModel("Stack"));
            tutorialSubList.add(new RVTwoModel("Queue"));
            tutorialSubList.add(new RVTwoModel("Binary Tree"));
            tutorialSubList.add(new RVTwoModel("Binary Search Tree"));
            tutorialSubList.add(new RVTwoModel("Heap"));
            tutorialSubList.add(new RVTwoModel("Hashing"));
            tutorialSubList.add(new RVTwoModel("Graph"));
            tutorialSubList.add(new RVTwoModel("Advanced Data Structure"));
            tutorialSubList.add(new RVTwoModel("Matrix"));
            tutorialSubList.add(new RVTwoModel("Strings"));
            tutorialSubList.add(new RVTwoModel("All Data Structures"));
  
        } else if (num == 3){
  
            tutorialSubList.add(new RVTwoModel("C"));
            tutorialSubList.add(new RVTwoModel("C++"));
            tutorialSubList.add(new RVTwoModel("Java"));
            tutorialSubList.add(new RVTwoModel("Python"));
            tutorialSubList.add(new RVTwoModel("C#"));
            tutorialSubList.add(new RVTwoModel("Javascript"));
            tutorialSubList.add(new RVTwoModel("JQuery"));
            tutorialSubList.add(new RVTwoModel("SQL"));
            tutorialSubList.add(new RVTwoModel("PHP"));
            tutorialSubList.add(new RVTwoModel("Scala"));
            tutorialSubList.add(new RVTwoModel("Perl"));
            tutorialSubList.add(new RVTwoModel("GO Language"));
            tutorialSubList.add(new RVTwoModel("HTML"));
            tutorialSubList.add(new RVTwoModel("CSS"));
            tutorialSubList.add(new RVTwoModel("Kotlin"));
  
        } else if (num == 4){
  
            tutorialSubList.add(new RVTwoModel("Company Preparation"));
            tutorialSubList.add(new RVTwoModel("Top Topics"));
            tutorialSubList.add(new RVTwoModel("Practice Company Questions"));
            tutorialSubList.add(new RVTwoModel("Interview Experiences"));
            tutorialSubList.add(new RVTwoModel("Experienced Interviews"));
            tutorialSubList.add(new RVTwoModel("Internship Interviews"));
            tutorialSubList.add(new RVTwoModel("Competitive Programming"));
            tutorialSubList.add(new RVTwoModel("Design Patterns"));
            tutorialSubList.add(new RVTwoModel("Multiple Choice Quizzes"));
  
        } else if (num == 5){
  
            tutorialSubList.add(new RVTwoModel("GATE CS Notes 2021"));
            tutorialSubList.add(new RVTwoModel("Last Minute Notes"));
            tutorialSubList.add(new RVTwoModel("GATE CS Solved Papers"));
            tutorialSubList.add(new RVTwoModel("GATE CS Original Papers and Official Keys"));
            tutorialSubList.add(new RVTwoModel("GATE 2021 Dates"));
            tutorialSubList.add(new RVTwoModel("GATE CS 2021 Syllabus"));
            tutorialSubList.add(new RVTwoModel("Important Topics for GATE CS"));
            tutorialSubList.add(new RVTwoModel("Sudo GATE 2021"));
  
        } else if (num == 6){
  
            tutorialSubList.add(new RVTwoModel("ISRO CS Solved Papers"));
            tutorialSubList.add(new RVTwoModel("ISRO CS Original Papers and Official Keys"));
            tutorialSubList.add(new RVTwoModel("ISRO CS Syllabus for Scientist/Engineer Exam"));
  
        } else if (num == 7){
            tutorialSubList.add(new RVTwoModel("UGC NET CS Notes Paper II"));
            tutorialSubList.add(new RVTwoModel("UGC NET CS Notes Paper III"));
            tutorialSubList.add(new RVTwoModel("UGC NET CS Solved Papers"));
  
        } else if (num == 8){
  
            tutorialSubList.add(new RVTwoModel("Mathematics"));
            tutorialSubList.add(new RVTwoModel("Operating System"));
            tutorialSubList.add(new RVTwoModel("DBMS"));
            tutorialSubList.add(new RVTwoModel("Computer Networks"));
            tutorialSubList.add(new RVTwoModel("Computer Organization and Architecture"));
            tutorialSubList.add(new RVTwoModel("Theory of Computation"));
            tutorialSubList.add(new RVTwoModel("Compiler Design"));
            tutorialSubList.add(new RVTwoModel("Digital Logic"));
            tutorialSubList.add(new RVTwoModel("Software Engineering"));
  
        } else if (num == 9){
  
            tutorialSubList.add(new RVTwoModel("HTML"));
            tutorialSubList.add(new RVTwoModel("CSS"));
            tutorialSubList.add(new RVTwoModel("Javascript"));
            tutorialSubList.add(new RVTwoModel("jQuery"));
            tutorialSubList.add(new RVTwoModel("PHP"));
  
        }
  
        rvTwoAdapter = new RVTwoAdapter(tutorialSubList);
        RVTwo.setAdapter(rvTwoAdapter);
    }
}


 

RVOneAdapter.java

Java




import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import com.wheic.recyclergetsout.R;
import java.util.List;
  
public class RVOneAdapter extends RecyclerView.Adapter<RVOneAdapter.ViewHolder> {
  
  // Main-list item titles will be stored here  
  private List<RVOneModel> tutorialList; 
  
  // Need this clickListener for the Main-list item onClick events 
  private OnItemsClickListener listener; 
  
    // Parameterized constructor of this class to initialize tutorialList
    public RVOneAdapter(List<RVOneModel> tutorialList) {
        this.tutorialList = tutorialList;
  
    }
  
    // Main-list item clickListener is initialized
    // This will be used in MainActivity
    public void setWhenClickListener(OnItemsClickListener listener){
        this.listener = listener;
    }
  
    // Attach the item layout with the proper xml file
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_card_layout, parent, false);
        return new ViewHolder(view);
    }
  
    // It deals with the setting of different data and methods
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final RVOneModel modelItems = tutorialList.get(position);
        holder.setData(tutorialList.get(position).getName());
        holder.singleItemCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onItemClick(modelItems);
                }
            }
        });
    }
  
    // It returns the length of the RecyclerView
    @Override
    public int getItemCount() {
        return tutorialList.size();
    }
  
    // The ViewHolder is a java class that stores 
      // the reference to the item layout views
    public class ViewHolder extends RecyclerView.ViewHolder{
  
        public CardView singleItemCardView;
        public TextView singleItemTextView;
  
        // Link up the Main-List items layout components with their respective id
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            singleItemCardView = itemView.findViewById(R.id.singleItemCardView);
            singleItemTextView = itemView.findViewById(R.id.singleItemTextView);
        }
  
        // setText in Main-List title text
        public void setData(String name){
            this.singleItemTextView.setText(name);
        }
    }
  
    // Interface to perform events on Main-List item click
    public interface OnItemsClickListener{
        void onItemClick(RVOneModel rvOneModel);
    }
}


 

RVOneModel.java

Java




public class RVOneModel {
      // this variable will store main-list item title
    private String name; 
       // this will help differentiate between the main-list items
    private int num;
  
    // parameterized constructor
    public RVOneModel(String name, int num) {
        this.name = name;
        this.num = num;
    }
  
    // getter functions for these two variables
    public String getName() {
        return name;
    }
  
    public int getNum() {
        return num;
    }
}


 

RVTwoAdapter.java

Java




import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.wheic.recyclergetsout.R;
import java.util.List;
  
public class RVTwoAdapter extends RecyclerView.Adapter<RVTwoAdapter.ViewHolder> {
  
       // Sub-list item titles will be stored here
    private List<RVTwoModel> tutorialSubList;
  
    // Parameterized constructor of this 
      // class to initialize tutorialSubList
    public RVTwoAdapter(List<RVTwoModel> tutorialSubList) {
        this.tutorialSubList = tutorialSubList;
    }
  
    // Attach the item layout with the proper xml file
    @NonNull
    @Override
    public RVTwoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_card_layout_vertical, parent, false);
        return new ViewHolder(view);
    }
  
    // It deals with the setting of different data and methods
    @Override
    public void onBindViewHolder(@NonNull RVTwoAdapter.ViewHolder holder, int position) {
        holder.setData(tutorialSubList.get(position).getName());
    }
  
    // It returns the length of the RecyclerView
    @Override
    public int getItemCount() {
        return tutorialSubList.size();
    }
  
    // The ViewHolder is a java class that stores 
      // the reference to the item layout views
    public class ViewHolder extends RecyclerView.ViewHolder{
  
        public TextView rvTwoText;
  
        // Link up the Sub-List items layout components with their respective id
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            rvTwoText = itemView.findViewById(R.id.singleItemTextViewRVTwo);
        }
  
        // setText in Sub-List title text
        public void setData(String name){
            this.rvTwoText.setText(name);
        }
    }
}


 

RVTwoModel.java

Java




public class RVTwoModel {
       // this variable will store sub-list item title
    private String name;
  
    // parameterized constructor
    public RVTwoModel(String name) {
        this.name = name;
    }
  
    // getter function for the name variable
    public String getName() {
        return name;
    }
}


Output:

You can check this project from this GitHub link.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads