android - 可以为两种 View 类型使用一个 viewholder 吗?

标签 android android-recyclerview adapter

我是 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/

相关文章:

android - 如何通过特定像素以编程方式滚动 RecyclerView?

android - RecyclerView 滚动到顶部按钮

android-fragments - 使用选项卡式片段单击另一个 recyclerview 片段的特定项目时加载一个 recyclerview 片段?

Android - 使用 jarsigner 签署应用程序

android - 在 webview 数据上滑动动画

android - 通过触摸或点击在 Android 谷歌地图上添加标记

android - 仅使用具有与特定键列表匹配的键的项目填充 Firebase RecyclerView?

android - 在 android 回收器 View 中确定快照结束事件

android - 自定义 ListView 适配器。 TextChangedListener 调用了错误的 EditText

rust - 包装类型时为 "no method found for type T in the current scope"