gpt4 book ai didi

Android 如何在单击 RecyclerView 项目时从 fragment 发送 Intent

转载 作者:行者123 更新时间:2023-11-30 02:08:40 25 4
gpt4 key购买 nike

我的应用使用 fragment 中包含的 RecyclerView 进行编码。我能够使用模拟数据列表成功填充 RecyclerView,但是我不知道如何在单击其中一个项目时将 Intent 发送到另一个 Activity。当我尝试从 fragment 中调用 StartActivity 时,Android Studio 无法解析该方法。

我的 onItemClick 是通过我的 viewholder 类中的接口(interface)实现的。该接口(interface)还会生成错误 inner classes cannot have static declarations。我想知道我是否无法从 fragment 中调用 StartActivity,因为我的 RecyclerView 适配器或 viewholder 类都没有扩展 Activity

单击 RecyclerView 中的项目时发送 Intent 的适当实现是什么?

占位符 fragment

public static class PlaceholderFragment extends Fragment {

Activity mActivity;
RecyclerView mRecyclerView;
TaskAdapter taskAdapter;

public PlaceholderFragment() {
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.mActivity = (Activity) activity;
setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.cardList);
taskAdapter = new TaskAdapter(createList(30));
return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRecyclerView.setAdapter(taskAdapter);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());

//Cannot resolve method
taskAdapter.SetItemClickListener(new TaskAdapter.TaskViewHolder.OnItemClickListener() {

@Override
public void onItemClick(View v, int position) {
Intent detailIntent = new Intent(v.getContext(), DetailActivity.class);
//Cannot resolve method
StartActivity(detailIntent);

}
});

}

private List<Task> createList(int size) {
List<Task> result = new ArrayList<>();
for (int i = 1; i<=size; i++) {
Task task = new Task();
task.title = Task.TITLE_PREFIX + i;
task.updated = Task.UPDATED_PREFIX +i;
task.notes = task.NOTES_PREFIX + i;

result.add(task);

}
return result;
}
}

任务适配器

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskViewHolder> {

private List<Task> taskList;
TaskViewHolder.OnItemClickListener mItemClickListener;

public TaskAdapter(List<Task> taskList) {
this.taskList = taskList;
}

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

@Override
public void onBindViewHolder(TaskViewHolder taskViewHolder, int i) {
Task task = taskList.get(i);
taskViewHolder.mTitle.setText(task.title);
taskViewHolder.mUpdated.setText(task.updated);
taskViewHolder.mStatus.setChecked(false);
}

@Override
public TaskViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

View itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.tasklist_layout, viewGroup, false);

return new TaskViewHolder(itemView);
}

public class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

protected TextView mTitle;
protected TextView mUpdated;
protected CheckBox mStatus;

public TaskViewHolder(View view) {
super(view);
mTitle = (TextView) view.findViewById(R.id.title);
mUpdated = (TextView) view.findViewById(R.id.updated);
mStatus = (CheckBox) view.findViewById(R.id.status);
view.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, getPosition());
}
}
//Inner Classes cannot have static declarations
public interface OnItemClickListener {
void onItemClick(View view , int position);
}

public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
//cannot resolve symbol
this.mItemClickListener = mItemClickListener;
}
}
}

最佳答案

startActivity() 方法以小 s 开头。试试下面的代码行

   //Cannot resolve method 
taskAdapter.TaskViewHolder.SetItemClickListener(new TaskAdapter.TaskViewHolder.OnItemClickListener() {

@Override
public void onItemClick(View v, int position) {
Intent detailIntent = new Intent(v.getContext(), DetailActivity.class);
//Cannot resolve method
getActivity().startActivity(detailIntent);

}
});

SetOnItemClickListener() 应该在 TaskViewHolder 类之外。试试下面的方法,看看是否有帮助。

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskViewHolder> {

private List<Task> taskList;
TaskViewHolder.OnItemClickListener mItemClickListener;

public TaskAdapter(List<Task> taskList) {
this.taskList = taskList;
}

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

@Override
public void onBindViewHolder(TaskViewHolder taskViewHolder, int i) {
Task task = taskList.get(i);
taskViewHolder.mTitle.setText(task.title);
taskViewHolder.mUpdated.setText(task.updated);
taskViewHolder.mStatus.setChecked(false);
}

@Override
public TaskViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

View itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.tasklist_layout, viewGroup, false);

return new TaskViewHolder(itemView);
}

public class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

protected TextView mTitle;
protected TextView mUpdated;
protected CheckBox mStatus;

public TaskViewHolder(View view) {
super(view);
mTitle = (TextView) view.findViewById(R.id.title);
mUpdated = (TextView) view.findViewById(R.id.updated);
mStatus = (CheckBox) view.findViewById(R.id.status);
view.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(v, getPosition());
}
}
//Inner Classes cannot have static declarations
public interface OnItemClickListener {
void onItemClick(View view , int position);
}
}

public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
//cannot resolve symbol
this.mItemClickListener = mItemClickListener;
}
}

关于Android 如何在单击 RecyclerView 项目时从 fragment 发送 Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30412782/

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