我正在使用#include
和RecyclerView
。我想将CardView
附加到每张卡片上。我尝试了stackoverflow上提供的许多答案,但它们对我不起作用。到目前为止,我已经尝试过 -
OnClickListner
正如我们所看到的,我已经public class SubjectAdapter extends RecyclerView.Adapter<SubjectAdapter.ViewHolder> implements View.OnClickListener,
View.OnLongClickListener{
private static final String LOGCAT = "SubjectAdapter";
private final Context mContext;
List<Subject> SubjectsList;
public SubjectAdapter(Context context) {
super();
this.mContext = context;
SQLiteDatabase.loadLibs(mContext);
DBHelper myDbHelper = new DBHelper(mContext);
SubjectsList = new ArrayList<Subject>();
SubjectsList = myDbHelper.getAllSubjects();
myDbHelper.close();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.subject_cardview_row, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
// Below two lines are NOT working
viewHolder.tvSubjectName.setOnClickListener(this);
//viewHolder.setOnClickListener(this);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
Subject subject = SubjectsList.get(i);
viewHolder.tvSubjectName.setText(subject.getSubject_Name());
viewHolder.tvCounts.setText(String.valueOf(subject.getSubject_Number_of_Questions()));
// Below two lines are NOT working
viewHolder.tvSubjectName.setOnClickListener(this);
//viewHolder.setOnClickListener(this);
}
@Override
public int getItemCount() {
return SubjectsList.size();
}
@Override
public void onClick(View v) {
// It's not working either
ViewHolder holder = (ViewHolder) v.getTag();
int position = holder.getPosition();
if (v.getId() == holder.tvSubjectName.getId()){
Log.d(LOGCAT, "tvSubjectName onClick at" + position);
//Toast.makeText(mContext, "tvSubjectName onClick at" + position, Toast.LENGTH_LONG).show();
} else {
Log.d(LOGCAT, "RecyclerView Item onClick at " + position);
//Toast.makeText(mContext, "RecyclerView Item onClick at " + position, Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onLongClick(View v) {
return false;
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvSubjectName;
public TextView tvCounts;
public ViewHolder(View itemView) {
super(itemView);
tvSubjectName = (TextView) itemView.findViewById(R.id.tv_subject_name);
tvCounts = (TextView) itemView.findViewById(R.id.tv_text_counts);
}
}
}
同时使用了setOnClickListener
和onCreateViewHolder
,也作为单独的onBindViewHolder
,但它们似乎都不适合我。所以,我想知道,如何将OnClickListner添加到CardView?
答案 0 :(得分:9)
onClick返回的视图不一定对应于回收器视图的View行层次结构。 我认为您应该使用
修改onBindViewHolder
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
final Subject subject = SubjectsList.get(i);
viewHolder.tvSubjectName.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Log.d(LOGCAT, "tvSubjectName onClick at" + i);
// etc
}
});
...
}
如果您使用长按,则将View.OnClickListener
替换为View.OnLongClickListener
,将onClick
替换为onLongClick
。
答案 1 :(得分:4)
这对我有用! 将 setOnClickListener-method 放在ViewHolder类的构造函数中。
class ViewHolder extends RecyclerView.ViewHolder
{
public TextView tvSubjectName;
public TextView tvCounts;
public ViewHolder(View itemView)
{
super(itemView);
tvSubjectName = (TextView) itemView.findViewById(R.id.tv_subject_name);
tvCounts = (TextView) itemView.findViewById(R.id.tv_text_counts);
itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//write here the code for wathever you want to do with a card
//...
}
});
}
}
答案 2 :(得分:1)
查看Lucas Rocha的新TwoWayView API。
TwoWayLayoutManager是一个位于LayoutManager之上的简单API,可以为您完成所有繁重的工作,因此您可以专注于如何测量,放置和分离子视图。
Follow his site并且API的实现相对简单,可以帮助您解决RecyclerView和CardView带来的一些复杂困难。
答案 3 :(得分:0)
在RecyclerView.ViewHolder构造函数中将OnClickListener设置为itemView,您也可以使用getAdapterPosition()
方法获取cardView的位置,这可以帮助您使用putExtra intent方法将数据传递给新活动。 doc for getAdapterPosition method
`
class ViewHolder extends RecyclerView.ViewHolder
{
public TextView tvSubjectName;
public TextView tvCounts;
public ViewHolder(View itemView)
{
super(itemView);
tvSubjectName = (TextView) itemView.findViewById(R.id.tv_subject_name);
tvCounts = (TextView) itemView.findViewById(R.id.tv_text_counts);
itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int position = getAdapterPosition();
// use position to put extra data
}
});
}
`
答案 4 :(得分:0)
如果您真的想要实现onclick侦听器,请执行以下操作
2.setOnClickListener到您在Adapter类中声明的视图对象(在ViewHolder中)onBindViewHolder
这适用于整个cardView