gpt4 book ai didi

android - 处理 RecyclerView 上的项目 OnClick 方法

转载 作者:行者123 更新时间:2023-11-29 20:16:44 25 4
gpt4 key购买 nike

我正在尝试制作一个包含卡片 View 的传记应用程序。当我单击 RecyclerView 上的每个项目时,我无法实现,必须打开包含详细信息的第二个 Activity 。这是我的代码。我哪里做错了?非常感谢...

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

private static final String TAG = "debug";
private Context mContext;
List<Person> list = new ArrayList<>();

public SimpleRecyclerAdapter(Context mContext, List<Person>list){
this.list=list;
}

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

public TextView person_name;
public TextView person_year;
public ImageView person_img;
public CardView card_view;

public ViewHolder(final View view) {
super(view);

card_view = (CardView) view.findViewById(R.id.card_view);
person_name = (TextView) view.findViewById(R.id.person_name);
person_year = (TextView) view.findViewById(R.id.person_year);
person_img = (ImageView) view.findViewById(R.id.person_photo);

view.setOnClickListener(this);

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "inside viewholder position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(v.getContext(), DetailActivity.class);

v.getContext().startActivity(intent);
}
});

}

@Override
public void onClick(View v) {

}

} // Closing inner class: Viewholder

@Override
public SimpleRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_layout, parent, false);
// create ViewHolder
ViewHolder view_holder = new ViewHolder(v);
return view_holder;
}


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

holder.person_name.setText(list.get(position).getName());
holder.person_year.setText(list.get(position).getYear());
holder.person_img.setImageResource(list.get(position).getPhoto_id());
}

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


@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
} // Closing outer class: SimpleRecyclerAdapter

最佳答案

您可能忘记了将 clickable 属性添加到您的父行项目布局中。尝试像这样添加它

android:clickable="true"

还尝试删除 View 上的 onClickMethods 之一,因为您设置了两次。

view.setOnClickListener(this);

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "inside viewholder position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(v.getContext(), DetailActivity.class);

v.getContext().startActivity(intent);
}
});

关于android - 处理 RecyclerView 上的项目 OnClick 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33713092/

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