使用RecyclerView的任何人都找到了一种方法来将onClickListener设置为 RecyclerView中的项目?我想为每个听众设置一个听众 每个项目的布局,但我似乎有点麻烦 确保RecyclerView有一种方法可以监听onClick 事件,但我不太清楚。
ViewHolder.java
import android.content.Context;
import android.graphics.Typeface;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
public class ViewHolder extends RecyclerView.ViewHolder {
LinearLayout linearLayout;
View mView;
public ViewHolder(View itemView) {
super(itemView);
this.mView = itemView;
}
public void setDetails(Context ctx, String title, String desc, String image) {
Typeface typeface = Typeface.createFromAsset(ctx.getAssets(), "Cairo.ttf");
Typeface typeface1;
typeface1 = Typeface.createFromAsset(ctx.getAssets(), "semibold.ttf");
TextView txt_title = mView.findViewById(R.id.txt_title);
ImageView imageView = mView.findViewById(R.id.image_card);
TextView txt_desc = mView.findViewById(R.id.txt_desc);
txt_title.setText(title);
txt_desc.setText(desc);
Picasso.get().load(image).into(imageView);
txt_title.setTypeface(typeface1);
txt_desc.setTypeface(typeface);
}
}
答案 0 :(得分:3)
如果要单击整个项目,则只需这样做。
recyclerView.addOnItemTouchListener(new RecysclerViewClick(getActivity(), recyclerView, new RecysclerViewClick.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
//Your Code here
}
@Override
public void onLongItemClick(View view, int position) {
// do whatever
}
})
);
但是,如果要在onBindViewHolder中将它们初始化后,单击特定按钮,textView或image setClicklistener
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});