gpt4 book ai didi

java - 如何处理 RecyclerView 项目 onClick,如果它对多个 Activity 使用单个适配器类

转载 作者:行者123 更新时间:2023-11-29 23:31:32 25 4
gpt4 key购买 nike

**我正在为两个不同的 Activity (VocabularyActivity 和 PracticeActivity)使用单个 MyAdapter 类。我想为两个 Activity 中定义的不同项目 onClick 打开不同的 Intent。我也附上了我的代码。我正在尝试为项目 onClick 使用 Activity (VocabularyActivity 和 PracticeActivity)的上下文,但无法实现它。请指导我完成这个。 **

 //MyAdapter.java

package com.example.pkjha.recyclerview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private List<ListItem> listItems;
private Context context;

public MyAdapter(List<ListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_recyclerview,parent,false);
return new ViewHolder(v);
}



@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {

ListItem listItem=listItems.get(position);
viewHolder.textViewHeader.setText(listItem.getFirstText());
viewHolder.textViewDescription.setText(listItem.getSecondText());
}




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




public class ViewHolder extends RecyclerView.ViewHolder
{
public TextView textViewHeader,textViewDescription;
public ViewHolder(@NonNull View itemView) {
super(itemView);

textViewDescription=itemView.findViewById(R.id.firstText);
textViewHeader=itemView.findViewById(R.id.secondText);
}
}
}

//VocabularyActivity.java


package com.example.pkjha.recyclerview;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class VocabularyActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItem> listItemList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vocabulary);

recyclerView=(RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);

recyclerView.setLayoutManager(new LinearLayoutManager(this));
listItemList=new ArrayList<>();

setNewData();

adapter=new MyAdapter(listItemList,this);
recyclerView.setAdapter(adapter);
}
private void setNewData() {

ListItem listItem=new ListItem("Bird","1");
listItemList.add(listItem);
listItem=new ListItem("Animal","2");
listItemList.add(listItem);
listItem=new ListItem("Body","3");
listItemList.add(listItem);

}`
}

//PracticeActivity.java



package com.example.pkjha.recyclerview;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class PracticeActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItem> listItemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practice);
recyclerView=(RecyclerView) findViewById(R.id.recyclerView);

recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listItemList=new ArrayList<>();

setNewData();

adapter=new MyAdapter(listItemList,this);
recyclerView.setAdapter(adapter);
}

private void setNewData() {

ListItem listItem=new ListItem("Chair","1");
listItemList.add(listItem);
listItem=new ListItem("Table","2");
listItemList.add(listItem);
listItem=new ListItem("Bed","3");
listItemList.add(listItem);



}

}

最佳答案

您可以创建一个接口(interface)并在两个 Activity 中实现它,然后为两个 Activity 编写您的 Intent 代码。然后,您可以使用在适配器的构造函数中传递的上下文从适配器调用接口(interface)方法。

关于java - 如何处理 RecyclerView 项目 onClick,如果它对多个 Activity 使用单个适配器类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52585695/

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