如何对嵌入 2 个不同卡片视图的同名回收器视图使用不同的活动?

How to use different activities for same name recycler view embedded in 2 different cardview?

我的程序提供了一个 activity 和 2 个卡片视图,而这些视图又使用一个回收器视图。这是一个学习应用程序,因此两个卡片视图用于 Java 和 HTML,并且它们使用相同的适配器用于回收器视图。 Java 和 HTML 都会有一个介绍页面所以我想打开不同的 activity 用于 java 介绍和 HTML 介绍

我尝试在点击回收站视图时区分卡片视图

holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClickListener(View v, int position) {
                Intent intent;
                switch (v.getId()){
                    case R.id.javacardId :
                        if (models.get(position).getTitle().equals("Introduction")){
                            intent = new Intent(c,anotherActivity.class);
                            c.startActivity(intent);
                        }
                        break;
                    case R.id.htmlcardId :
                        if (models.get(position).getTitle().equals("Introduction")){
                        intent = new Intent(c,html_Introduction.class);
                        c.startActivity(intent);
                    }
                    break;
                }

---java_class for JAVA CardView enabling recycler view
package com.hfad.dashboard_ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;

public class java_prog extends AppCompatActivity {
    String name = "java_prog";
    RecyclerView contentList;
    contentAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_java_prog);
        contentList = findViewById(R.id.courseContentList);
        contentList.setLayoutManager(new LinearLayoutManager(this));
        adapter = new contentAdapter(this, getList());
        contentList.setAdapter(adapter);
    }

    private ArrayList<model> getList(){
        ArrayList<model> models = new ArrayList<>();

        model m = new model();
        m.setTitle("Introduction");
        m.setImage(R.raw.intro);
        models.add(m);

        m = new model();
        m.setTitle("Arrays");
        m.setImage(R.raw.array);
        models.add(m);

        m = new model();
        m.setTitle("Classes and Objects");
        m.setImage(R.raw.objects);
        models.add(m);

        m = new model();
        m.setTitle("Loops");
        m.setImage(R.raw.loop);
        models.add(m);

        m = new model();
        m.setTitle("Exception");
        m.setImage(R.raw.exception);
        models.add(m);

        return models;
    }
}

---

--- html class for HTML CardView enabling recycler view
package com.hfad.dashboard_ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

import java.util.ArrayList;

public class html_prog extends AppCompatActivity {
    RecyclerView contentList;
    contentAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_html_prog);
        contentList = findViewById(R.id.courseContentList);
        contentList.setLayoutManager(new LinearLayoutManager(this));
        adapter = new contentAdapter(this, getList());
        contentList.setAdapter(adapter);
    }
    private ArrayList<model> getList(){
        ArrayList<model> models = new ArrayList<>();

        model m = new model();
        m.setTitle("Introduction");
        m.setImage(R.raw.intro);
        models.add(m);

        m = new model();
        m.setTitle("Arrays");
        m.setImage(R.raw.array);
        models.add(m);

        m = new model();
        m.setTitle("Classes and Objects");
        m.setImage(R.raw.objects);
        models.add(m);

        m = new model();
        m.setTitle("Loops");
        m.setImage(R.raw.loop);
        models.add(m);

        m = new model();
        m.setTitle("Exception");
        m.setImage(R.raw.exception);
        models.add(m);

        return models;
    }
}

---

---Adapter class
package com.hfad.dashboard_ui;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class contentAdapter extends RecyclerView.Adapter<holder> {

    Context c;
    ArrayList<model> models;

    public contentAdapter(Context c, ArrayList<model> models){
        this.c=c;
        this.models = models;
    }

    @NonNull
    @Override
    public holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.content_cardview,parent,false);
        return new holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull holder holder, int position) {
        holder.text.setText(models.get(position).getTitle());
        holder.image.setImageResource(models.get(position).getImage());

        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClickListener(View v, int position) {
                Intent intent;
                switch (v.getId()){
                    case R.id.javacardId :
                        if (models.get(position).getTitle().equals("Introduction")){
                            intent = new Intent(c,anotherActivity.class);
                            c.startActivity(intent);
                        }
                        break;
                    case R.id.htmlcardId :
                        if (models.get(position).getTitle().equals("Introduction")){
                        intent = new Intent(c,html_Introduction.class);
                        c.startActivity(intent);
                    }
                    break;
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return models.size();
    }
}

我希望输出能够区分 Java 和 HTML 的引入回收器视图,以便我可以为它们分配不同的活动

我建议你给adapter加一个参数,代表source。例如:

// Adapter:

public class ContentAdapter extends RecyclerView.Adapter<holder> {

    private Context context;
    private List<Model> models;
    private AdapterType type;

    public ContentAdapter(Context context, List<Model> models, AdapterType type){
        this.context = context;
        this.models = models;
        this.type = type;
    }

   // other code here

    @Override
    public void onBindViewHolder(@NonNull holder holder, int position) {
        holder.text.setText(models.get(position).getTitle());
        holder.image.setImageResource(models.get(position).getImage());

        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClickListener(View v, int position) {
                switch (type) {
                    case AdapterType.JAVA:
                        // Logic
                    case AdapterType.HTML:
                        // Logic
                }
            }
        });
    }
}

// Enum:
public enum AdapterType {
    JAVA, HTML
}

也可以使用整数,比如0代表Java,1代表HTML,2代表Python等等。