我正在尝试在我的RecyclerView
上实现Onclick。我尝试了许多方法,但是最终导致应用程序崩溃。我也想在单击任何cardView
时出现一个对话框。
我使用fireBase作为后端,对此我很陌生。
这是我的适配器类:
public class BlogRecyclerAdapter extends RecyclerView.Adapter<BlogRecyclerAdapter.ViewHolder> {
public List<BlogPost>blogList;
public Context context;
private FirebaseFirestore firebaseFirestore;
private static final int SECOND_MILLIS = 1000;
private static final int MINUTE_MILLIS = 60 * SECOND_MILLIS;
private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
private static final int DAY_MILLIS = 24 * HOUR_MILLIS;
String dateString;
public BlogRecyclerAdapter(List<BlogPost>blogList){
this.blogList=blogList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_list_item,parent,false);
context=parent.getContext();
firebaseFirestore=FirebaseFirestore.getInstance();
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
String descData=blogList.get(position).getDesc();
holder.setDescText(descData);
String imageUrl=blogList.get(position).getImage_url();
holder.setBlogImage(imageUrl);
final String userId=blogList.get(position).getUser_id();
firebaseFirestore.collection("Users").document(userId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
String username=task.getResult().getString("Name");
holder.setUserData(username);
}
else{
}
}
});
long miliseconds=blogList.get(position).getTimestamp().getTime();
long now = System.currentTimeMillis();
final long diff = now - miliseconds;
if (diff < MINUTE_MILLIS) {
dateString= "just now";
} else if (diff < 2 * MINUTE_MILLIS) {
dateString= "a minute ago";
} else if (diff < 50 * MINUTE_MILLIS) {
dateString= diff / MINUTE_MILLIS + " minutes ago";
} else if (diff < 90 * MINUTE_MILLIS) {
dateString= "an hour ago";
} else if (diff < 24 * HOUR_MILLIS) {
dateString= diff / HOUR_MILLIS + " hours ago";
} else if (diff < 48 * HOUR_MILLIS) {
dateString= "yesterday";
} else {
dateString= diff / DAY_MILLIS + " days ago";
}
// String dateString= DateFormat.format("MM/dd/yyyy",new Date(miliseconds)).toString();
holder.setTime(dateString);
}
@Override
public int getItemCount() {
return blogList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private View mView;
private TextView descView;
private ImageView blogImageView;
private TextView blogDate;
private TextView blogUserName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mView=itemView;
}
public void setUserData(String name){
blogUserName=mView.findViewById(R.id.blogUserName);
blogUserName.setText(name);
}
public void setDescText(String descText){
descView=mView.findViewById(R.id.blogDesc);
descView.setText(descText);
}
public void setBlogImage(String downloaduri){
blogImageView=mView.findViewById(R.id.blogImage);
Glide.with(context).load(downloaduri).apply(RequestOptions.bitmapTransform(new BlurTransformation(30,3))).into(blogImageView);
}
public void setTime(String date){
blogDate=mView.findViewById(R.id.blogDate);
blogDate.setText(date);
}
}
}
我尝试了许多实现方法,但均未成功。
答案 0 :(得分:0)
与ListView相比,将onClickListener添加到RecyclerView是一个痛苦的过程。但是有一种方法可以做到。
首先,将此类添加到适配器。
public static class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onLongItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && mListener != null) {
mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
}
}
});
}
@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
return false;
}
@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
@Override
public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}
第二,将此侦听器添加到您的活动/片段
recyclerView.addOnItemTouchListener(new BlogRecyclerAdapter.RecyclerItemClickListener(getActivity(), recyclerView ,new BlogRecyclerAdapter.RecyclerItemClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
// Do something here...
}
@Override public void onLongItemClick(View view, int position) {
}
})
);
答案 1 :(得分:0)
首先,要将ClickListener应用于项(卡片视图),因为RecyclerView没有默认项,因此您需要执行以下操作: