Search code examples
androidandroid-recyclerviewandroid-viewandroid-viewholderrecyclerview-layout

onBindViewHolder execute first ViewHolder only


I have three ViewHolders CardViewHolder, TitleViewHolder, GridViewHolder, for switching between it when user choose a specific layout, everything working good expect when I trying to switch to Title and Grid Layout/Views, I see only the "Dummy title and default image" that I put it in it's layout

Here's my PostAdapter class:

public class PostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<Item> items;

    public static final int CARD_LIST = 0;
    public static final int CARD_MAGAZINE = 1;
    public static final int TITLE = 2;
    public static final int GRID = 3;

    private int viewType;

    PostAdapter(Context context, List<Item> items) {
        this.context = context;
        this.items = items;
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
        notifyDataSetChanged();
    }

    public int getViewType() {
        return this.viewType;
    }



    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view;

        if (this.viewType == CARD_LIST) {
            view =  inflater.inflate(R.layout.card_layout, parent, false);
            return new CardViewHolder(view);
        } else if (this.viewType == CARD_MAGAZINE) {
            view =  inflater.inflate(R.layout.card_magazine_layout, parent, false);
            return new CardViewHolder(view);
        } else if(this.viewType == TITLE){
            view = inflater.inflate(R.layout.title_layout, parent, false);
            return new TitleViewHolder(view);
        }else {
            view = inflater.inflate(R.layout.grid_layout,parent,false);
            return new GridViewHolder(view);
        }

    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemType = getItemViewType(position);
        final Item item = items.get(position);
        final Document document = Jsoup.parse(item.getContent());
        final Elements elements = document.select("img");
        if (itemType == CARD_LIST || itemType == CARD_MAGAZINE) {
            if (holder instanceof CardViewHolder) {
                CardViewHolder cardViewHolder = (CardViewHolder) holder;
                cardViewHolder.postTitle.setText(item.getTitle());
                cardViewHolder.postDescription.setText(document.text());
                Log.d("IMAGE", elements.get(0).attr("src"));
                Glide.with(context).load(elements.get(0).attr("src"))
                        .into(cardViewHolder.postImage);

        } else if (itemType == TITLE) {
            if (holder instanceof TitleViewHolder) {
                TitleViewHolder titleViewHolder = (TitleViewHolder) holder;
                titleViewHolder.postTitle.setText(item.getTitle());

                Log.d("IMAGE", elements.get(0).attr("src"));
                Glide.with(context).load(elements.get(0).attr("src"))
                        .into(titleViewHolder.postImage);
            }
        }else {
            GridViewHolder gridViewHolder = (GridViewHolder) holder;
            gridViewHolder.postTitle.setText(item.getTitle());
            Log.d("IMAGE", elements.get(0).attr("src"));
            Glide.with(context).load(elements.get(0).attr("src"))
                    .into(gridViewHolder.postImage);


        }
    }



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

    public class CardViewHolder extends RecyclerView.ViewHolder {

        ImageView postImage;
        TextView postTitle;
        TextView postDescription;

        public CardViewHolder(View itemView) {
            super(itemView);
            postImage = itemView.findViewById(R.id.postImage);
            postTitle = itemView.findViewById(R.id.postTitle);
            postDescription = itemView.findViewById(R.id.postDescription);

        }
    }

    public class TitleViewHolder extends RecyclerView.ViewHolder {
        TextView postTitle;
        MyImageview postImage;


        public TitleViewHolder(@NonNull View itemView) {
            super(itemView);
            postTitle = itemView.findViewById(R.id.postTitle);
            postImage = itemView.findViewById(R.id.postImage);
        }
    }


    public class GridViewHolder extends RecyclerView.ViewHolder {
        TextView postTitle;
        MyImageview postImage;


        public GridViewHolder(@NonNull View itemView) {
            super(itemView);
            postTitle = itemView.findViewById(R.id.postTitle);
            postImage = itemView.findViewById(R.id.postImage);
        }
    }
}

It seems that the first ViewHolder is only implemented in first if block, I tried to find out why the problem was caused by the log, but I don't know why unfortunately.


Solution

  • [Solved]

    The problem was happens because I was checking for the wrong variable getting by method getItemViewType it should be like this

    int itemType = getViewType();