多重引用,多重视图支架,回收者视图适配器

时间:2019-09-16 14:19:20

标签: android android-recyclerview recycler-adapter

你好,我有多个引用,多个视图持有者,回收者视图适配器

问题:当前实施视图仅显示类型为 TYPE_FOLLOWER 的一项,因为它是 我猜 getItemViewType 中的第一个,所以我需要做些什么才能查看所有持有人

我当前的适配器

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.debug.entity.fit_igk_gym.R;
import com.debug.entity.fit_igk_gym.model.Notification;
import com.squareup.picasso.Picasso;

import java.util.List;

public class NotificationsRecyclerView extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final static int TYPE_FOLLOWER = 1;
    private final static int TYPE_LIKE = 2;
    private final static int TYPE_COMMENT = 3;

    private List<Notification.FollowerNotification> followerNotification;
    private List<Notification.LikeNotification> likeNotification;
    private List<Notification.CommentNotification> commentNotification;

    private LayoutInflater mInflater;
    private ItemClickListener mClickListener;

    public NotificationsRecyclerView(Context context,
                                     List<Notification.FollowerNotification> followerNotification ,
                                     List<Notification.LikeNotification> likeNotification ,
                                     List<Notification.CommentNotification> commentNotification)
    {
        this.mInflater = LayoutInflater.from(context);
        this.followerNotification = followerNotification;
        this.likeNotification = likeNotification;
        this.commentNotification = commentNotification;

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType ) {
            case TYPE_FOLLOWER :  {
                view = mInflater.inflate(R.layout.item_follow_notification, parent, false);
                return new NotificationsViewHolder(view);
            }
            case TYPE_LIKE :  {
                view = mInflater.inflate(R.layout.item_like_notification, parent, false);
                return new LikesViewHolder(view);
            }
            case TYPE_COMMENT :  {
                view = mInflater.inflate(R.layout.item_comment_notification, parent, false);
                return new CommentViewHolder(view);
            }
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        int type = 0;
        if (followerNotification.size()!= 0) {
            type =  TYPE_FOLLOWER;
        } else if (likeNotification.size()!=0 ){
            type =  TYPE_LIKE;
        } else if (commentNotification.size()!=0 ){
            type =  TYPE_LIKE;
        }
        return type;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        switch (getItemViewType(position)) {
            case TYPE_FOLLOWER : {
                ((NotificationsViewHolder) holder).setFollowHolderData(followerNotification.get(position));
                break;
            }

            case TYPE_LIKE : {
                ((LikesViewHolder) holder).setLikeHolderData(likeNotification.get(position));
                break;
            }

            case TYPE_COMMENT : {
                ((CommentViewHolder) holder).setCommentHolderData(commentNotification.get(position));
                break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return followerNotification.size();
    }


    public class NotificationsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView followerProfileImage;
        TextView followerUserName , date;
        Button followBack;

        NotificationsViewHolder(View itemView) {
            super(itemView);
            followerProfileImage = itemView.findViewById(R.id.item_follow_user_following_you_image);
            followerUserName = itemView.findViewById(R.id.item_follow_user_following_you_name_text_view);
            date = itemView.findViewById(R.id.item_follow_action_time_text_view);
            followBack = itemView.findViewById(R.id.item_starting_to_follow_you_follow_back_button);
            itemView.setOnClickListener(this);

        }

        public void setFollowHolderData(Notification.FollowerNotification followerNotification) {
            Picasso.get().load(followerNotification.getFollowerProfilePicture())
                    .into(followerProfileImage);
            followerUserName.setText(followerNotification.getFollowerUserName());
            date.setText(followerNotification.getDate());
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }
    }

    public class LikesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView LikedUserProfileImage , likedImageUri;
        TextView likedUserName , date;

        LikesViewHolder(View itemView) {
            super(itemView);
            LikedUserProfileImage = itemView.findViewById(R.id.item_like_user_following_you_image);
            likedImageUri = itemView.findViewById(R.id.item_like_the_liked_post_image_view);
            likedUserName = itemView.findViewById(R.id.item_like_user_liked_your_post_text_view);
            date = itemView.findViewById(R.id.item_like_action_time_text_view);
            itemView.setOnClickListener(this);

        }

        public void setLikeHolderData(Notification.LikeNotification likeNotification) {
            Picasso.get().load(likeNotification.getLikedPhotoImageUri())
                    .into(LikedUserProfileImage);
            Picasso.get().load(likeNotification.getLikedPhotoImageUri())
                    .into(likedImageUri);
            likedUserName.setText(likeNotification.getLikedUserName());
            date.setText(likeNotification.getDate());
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }
    }

    public class CommentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView commentOwnerProfileImage , commentedImageUri;
        TextView commentOwnerUserName , date;

        CommentViewHolder(View itemView) {
            super(itemView);
            commentOwnerProfileImage = itemView.findViewById(R.id.item_comment_notification_user_following_you_image);
            commentedImageUri = itemView.findViewById(R.id.item_comment_notification_the_comment_post_image_view);
            commentOwnerUserName = itemView.findViewById(R.id.item_comment_notification_user_text_view);
            date = itemView.findViewById(R.id.item_comment_notification_action_time_text_view);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }

        public void setCommentHolderData(Notification.CommentNotification commentNotification) {
            Picasso.get().load(commentNotification.getCommentedUserProfileImageUri())
                    .into(commentOwnerProfileImage);

            Picasso.get().load(commentNotification.getCommentedImageUri())
                    .into(commentedImageUri);
            commentOwnerUserName.setText(commentNotification.getCommentedUserName());
            date.setText(commentNotification.getDate());
        }
    }

    Notification.FollowerNotification getItem(int id) {
        return followerNotification.get(id);
    }

    void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }
}

我成功地将3个数组的列表传递给了适配器,并且我已经单独测试了每个数组并且它可以工作,但是我无法让它们一起工作。

0 个答案:

没有答案