gpt4 book ai didi

java - 如何对嵌入 2 个不同卡片 View 的同名回收器 View 使用不同的 Activity ?

转载 作者:行者123 更新时间:2023-11-30 04:58:39 26 4
gpt4 key购买 nike

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

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

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 的引入回收器 View ,以便我可以为它们分配不同的 Activity

最佳答案

我建议你给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 等等。

关于java - 如何对嵌入 2 个不同卡片 View 的同名回收器 View 使用不同的 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58664839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com