📜  如何从 Android 中的 RecyclerView 访问 RecyclerView 之外的任何组件?

📅  最后修改于: 2022-05-13 01:54:33.126000             🧑  作者: Mango

如何从 Android 中的 RecyclerView 访问 RecyclerView 之外的任何组件?

标题可能有点混乱,但我们想说的是,假设在一个 Android 应用程序中,有一个 RecyclerView,而在它之外有一个 TextView。 Button 说,在 RecyclerView 中有一个可点击组件的列表。现在我想要的是,对于不同的按钮点击,TextView 中的文本会有所不同。所以这基本上是从 RecyclerView 控制 RecyclerView 之外的组件。

想要一个更快节奏和更具竞争力的环境来学习 Android 的基础知识吗?
单击此处前往由我们的专家精心策划的指南,旨在让您立即做好行业准备!

根据图片,有一个 RecyclerView 有四个按钮(左侧隐藏 1 个)和一个 TextView。对于不同的 Button 单击,TextView 中会显示不同的文本。在本文中,相同的概念将通过一点点真实世界的接触来实现。因此,最终的应用程序将显示 GEEKS FOR GEEKS 提供的教程列表和子列表。这是最终应用程序的演示 GIF:

先决条件:

  • 如何安装 Android Studio
  • 在 Android Studio 中设置一个新项目
  • 什么是 RecyclerView

步骤 1:使用 activity_main.xml 文件

  • 转到res -> layout -> activity_main.xml文件。
  • 这是该 XML 文件的代码:
XML


  
    
  
        
  
            
  
  
        
  
        
        
  
        
  
            
            
  
        
  
        
        
  
    
  


XML


  
    
  
        
  
            
  
        
    


XML


  
    
  
        
  
            
  
        
  
    


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;
    }
}


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 {
  
    // Main-list item titles will be stored here
      private List tutorialList; 
    
    // Parameterized constructor of this 
      // class to initialize tutorialList
    public RVOneAdapter(List 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);
        }
    }
}


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 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);
    }
}


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


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


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


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


Java
algorithmTitleText = findViewById(R.id.algorithmTitleText);


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


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;
    }
}


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 {
  
       // Sub-list item titles will be stored here
    private List tutorialSubList;
  
    // Parameterized constructor of this class 
      // to initialize tutorialSubList
    public RVTwoAdapter(List 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);
        }
    }
}


Java
// Sub-list item titles will be stored here
private List tutorialSubList;


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);
}


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


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 tutorialList; 
      // Sub-list item titles will be stored here
    private List 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);
    }
}


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 {
  
  // Main-list item titles will be stored here  
  private List 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 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);
    }
}


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;
    }
}


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 {
  
       // Sub-list item titles will be stored here
    private List tutorialSubList;
  
    // Parameterized constructor of this 
      // class to initialize tutorialSubList
    public RVTwoAdapter(List 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);
        }
    }
}


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;
    }
}


  • 这里使用了两个 RecyclerView ,一个是水平的( recyclerViewOne ),一个是垂直的( recyclerViewTwo )。第一个显示主列表,第二个显示主列表项的子列表。
  • TextView还用于显示主列表项文本。

第 2 步:为主列表项添加 single_card_layout.xml

  • 转到res -> 布局
  • 右键单击布局文件夹
  • 转到新建 -> 布局资源文件
  • 输入文件名(我是single_card_layout.xml
  • 将根元素更改为 RelativeLayout
  • 单击确定

  • 这是此 XML 文件的代码:

XML



  
    
  
        
  
            
  
        
    

  • 这基本上是在recyclerViewOne或主列表或水平RecyclerView中显示的项目的布局。

第三步:为子列表项添加single_card_layout_vertical.xml

  • 转到res -> 布局
  • 右键单击布局文件夹
  • 转到新建 -> 布局资源文件
  • 输入文件名(本例中的single_card_layout_vertical.xml
  • 将根元素更改为 RelativeLayout
  • 单击确定
  • 这是此 XML 文件的代码:

XML



  
    
  
        
  
            
  
        
  
    

  • 这是在 recyclerViewTwo 或子列表或垂直 RecyclerView 中显示的项目的布局。

第 4 步:处理水平 RecyclerView 或主列表

  • 转到Java ->com.wheic.recyclergetsout (您的可能有所不同)
  • 在 com.wheic.recyclergetsout 下创建一个目录(我将其命名为 RV1)。从 Android Studio 创建目录很奇怪,过程中有很多错误(至少对我而言)。因此,您可以按照以下步骤操作:

  • 右键单击 com.wheic.recyclergetsout(您的可能有所不同)
  • com.wheic.recyclergetsout -> 新建 -> 示例数据目录

  • 转到构建 -> 重建项目

  • 将在 app 文件夹下看到一个sampledata文件夹。

  • 右键单击它。转到重构 -> 重命名

  • 给出文件夹的名称。 (这里我们将其命名为 RV1)。
  • 单击重构

  • 将文件夹拖到 com.wheic.recyclergetsout
  • 在弹出的窗口中点击重构

  • 转到Build -> Rebuild Project
  • 或者,如果不想做所有这些,只需直接转到资源管理器并在那里创建一个目录。为此,右键单击com.wheic.recyclergetsout -> Show in Explorer 。然后在那里手动创建一个文件夹。

开始处理相应的模型类:

  1. 右键单击刚刚创建的文件夹(RV1) -> New -> Java Class 。给出班级名称。我将其命名为RVOneModel
  2. 所以基本上我使用了两个变量,一个字符串类型和一个整数。 String 变量将用于存储列表项的标题和整数变量以区分每个项目。然后为这两个变量创建一个包含变量和仅 Getter 函数的构造函数。
  3. 这是模型类的Java代码:

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;
    }
}

开始处理水平 RecyclerView 的 Adapter 类:

  1. 因此,右键单击刚刚创建的文件夹(RV1) -> New -> Java Class 。给出班级名称。我将其命名为RVOneAdapter
  2. 所以对于适配器,我们还需要一个ViewHolder类。
  3. 这个GFG 链接中已经清楚地描述了这些类和所有其他方法的使用。
  4. 这是带有 ViewHolder 类的 Adapter 类的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 {
  
    // Main-list item titles will be stored here
      private List tutorialList; 
    
    // Parameterized constructor of this 
      // class to initialize tutorialList
    public RVOneAdapter(List 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);
        }
    }
}

开始处理 MainActivity。 Java添加第一个 RecyclerView:

  1. tutorialList与 Main-List 项标题字符串变量一一存储,然后将适配器设置为水平 RecyclerView。
  2. 这是 MainActivity 中的代码。 Java添加第一个 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 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);
    }
}

第五步:为水平RecyclerView中的每一项设置clickListener:

  • 接下来的 4 个步骤在RVOneAdapater 中完成。Java
  • 首先,在RVOneAdapater 中创建一个接口。 Java为每个项目单击一个onItemClick抽象方法。

Java

// Interface to perform events on Main-List item click
public interface OnItemsClickListener{
    void onItemClick(RVOneModel rvOneModel);
}
  • 创建接口的引用变量。

Java

// Need this for the Main-list item onClick events
private OnItemsClickListener listener;
  • 创建了一个方法setWhenClickListener ,它将从 MainActivity 调用。

Java

// Main-list item clickListener is initialized
// This will be used in MainActivity
public void setWhenClickListener(OnItemsClickListener listener){
    this.listener = listener;
}
  • 现在,在 onBindViewHolder 中,创建了一个 setOnClickListener,当单击水平 RecyclerView 中的单个项目时,它实际上会调用onItemClick方法。

Java

holder.singleItemCardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(listener != null){
            listener.onItemClick(modelItems);
        }
    }
});
  • 在主活动中。 Java,为 TextView 创建了一个引用变量,并使用相应的 id 将其附加到 XML 文件中。

Java

algorithmTitleText = findViewById(R.id.algorithmTitleText);
  • 现在,调用适配器 setOnClickListener 并且单击单个列表项时将发生 textView 文本更改事件。

Java

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

第 6 步:处理垂直 RecyclerView 或子列表

  • 转到Java -> com.wheic.recyclergetsout (您的可能有所不同)
  • 就像上一个一样,创建了一个目录。我将其命名为 RV2。

开始处理子列表的模型类:

  1. 右键单击刚刚创建的文件夹(RV2) -> New -> Java Class 。给出班级名称。对我来说是RVTwoModel
  2. 根据这个应用程序,在这个模型类中,只需要一个 String 变量。 String 变量将存储子列表项的标题文本。然后就像前一个一样,创建了一个构造函数和一个 getter函数。
  3. 这是模型类的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;
    }
}

开始处理垂直 RecyclerView 的 Adapter 类:

  1. 右键单击刚刚创建的文件夹(RV2) -> New -> Java Class 。给出班级名称。我将其命名为RVTwoAdapter
  2. 就像任何其他 RecyclerView 一样,这个适配器类也没有什么不同。
  3. 下面是这个 Adapter 类和 ViewHolder 类的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 {
  
       // Sub-list item titles will be stored here
    private List tutorialSubList;
  
    // Parameterized constructor of this class 
      // to initialize tutorialSubList
    public RVTwoAdapter(List 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);
        }
    }
}

让我们完成 MainActivity 中的代码。Java:

  1. 需要子列表的第二个 ArrayList tutorialSubList 。因此创建了第二个 ArrayList 的引用变量。

Java

// Sub-list item titles will be stored here
private List tutorialSubList;
  1. 只为垂直 recyclerView 创建了一个函数,它接受一个整数参数。
  2. 为每个主列表项在 ArrayList 中添加静态数据。

这是函数的代码:

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);
}
  • onCreate()中,在主列表项 clickListener 中调用setRVTwo()函数。此外,它被称为外部,因为我们需要在创建活动时查看子列表。

Java

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

以下是该项目的每个最终Java代码:

主要活动。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 tutorialList; 
      // Sub-list item titles will be stored here
    private List 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 {
  
  // Main-list item titles will be stored here  
  private List 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 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 {
  
       // Sub-list item titles will be stored here
    private List tutorialSubList;
  
    // Parameterized constructor of this 
      // class to initialize tutorialSubList
    public RVTwoAdapter(List 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;
    }
}

输出:

您可以从此GitHub链接查看此项目。