可以将一个视图持有者用于两种视图类型吗?

时间:2018-10-04 10:32:29

标签: android android-recyclerview adapter

我是Android开发的新手,我想创建一个回收器视图,其中两个视图类型一个用于视频,另一个视图用于照片,但是是否有必要为此创建2个Viewholder,我们不能仅为此使用一个Viewholder吗? ??原因我面临很多问题,例如我需要为2个viewholder两次声明此函数

用于PhotoHolder

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);


        }
    });

}

2 个答案:

答案 0 :(得分:1)

我建议您使用一个自定义ViewHolder,在该Viewholder中设置一个变量,为其指定“类型”,然后在使用Viewholder进行绘制时,只需将照片调整内容包装在if(isPhoto){/ /全部与照片一起使用} else {//全部与视频一起使用

答案 1 :(得分:0)

为什么不声明一个通用的View Holder类,而又将此类扩展为photoHolder和VideoHolder?

您需要一个普通班级:

public class MediaHolder extends RecyclerView.ViewHolder{

// common properties

}

,然后为您的photoHolder和VideoHolder扩展此类。

然后您应该像这样声明您的方法:

private void addNewlike(final MediaHolder holder)