我是 android 开发的新手,我想创建一个具有两种 View 类型的回收器 View ,一种用于视频,另一种用于照片,但是是否有必要为此创建 2 个 Viewholder,我们不能只使用一个 Viewholder 吗??因为我面临很多问题,比如我需要为 2 个 viewholder 声明此函数两次
用于相片夹
private void addNewlike(final PhotoHolder holder){
Log.d(TAG, "addNewlike: adding new like ");
String newLikeID = mReference.push().getKey();
Likes likes = new Likes();
likes.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
mReference.child(mContext.getString(R.string.dbname_photos))
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(holder.photo.getUser_id())
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
holder.heart.toggleLike();
HashMap<String ,String> notificationData = new HashMap<>();
notificationData.put("from",FirebaseAuth.getInstance().getCurrentUser().getUid());
notificationData.put("type","likes");
notificationData.put("photo_desc",holder.photo.getDescription());
holder.mNotification.child(holder.photo.getUser_id()).setValue(notificationData).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
getLikesString(holder);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
getLikesString(holder);
}
});
}
FOR VideoHolder
private void addNewlike(final VideoHolder holder){
Log.d(TAG, "addNewlike: adding new like ");
String newLikeID = mReference.push().getKey();
Likes likes = new Likes();
likes.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
mReference.child(mContext.getString(R.string.dbname_photos))
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(holder.photo.getUser_id())
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
holder.heart.toggleLike();
HashMap<String ,String> notificationData = new HashMap<>();
notificationData.put("from",FirebaseAuth.getInstance().getCurrentUser().getUid());
notificationData.put("type","likes");
notificationData.put("photo_desc",holder.photo.getDescription());
holder.mNotification.child(holder.photo.getUser_id()).setValue(notificationData).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
getLikesString(holder);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
getLikesString(holder);
}
});
}
最佳答案
我建议使用一个自定义 ViewHolder,在该 viewholder 中设置一个变量,然后当您使用 viewholder 进行绘图时,只需将照片调整包装在 if(isPhoto) {//全部处理照片 } else {//全部处理视频
关于android - 可以为两种 View 类型使用一个 viewholder 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52644539/