0

I have a RecyclerView with an adapter. In the adapter I fill my views with a list. But in my adapter I inflate two different views. Here is some code:

@Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE_HEADER;
            default:
                return TYPE_LIST_ITEM;
        }
    }

 @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {

            //region User-Data like ProfilePic & Abonnenten
            case TYPE_HEADER: {
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.profile_besties_placeholder, parent, false);
                return new DataObjectHolder(view) {
                };
            }
            case TYPE_LIST_ITEM: {
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.profile_besties_list_item, parent, false);

                return new DataObjectHolder(view) {
                };
            }


        }
        return null;
    }

and now, I try to set data to the views. I have 20 items in my list and I set the position as text for my views. I can see in my RecyclerView that it starts by 1 but it must start by 0. Here is my onBind:

@Override
    public void onBindViewHolder(final DataObjectHolder holder, int position) {
        switch (position) {
            case TYPE_HEADER:
                break;
            default:

                int newPostion = position -1;

                bestiesItem = contents.get(position);
                holder.progressBarBesties.setVisibility(View.VISIBLE);
                //region set data to views
                holder.textViewUsername.setText(bestiesItem.getStrUsername());
                holder.textViewSubscribers.setText(bestiesItem.getStrAbos() + " Abos" + position);
                Glide.with(context)
                        .load(bestiesItem.getStrImageName())
                        .thumbnail(0.1f)
                        .error(R.drawable.error_image)
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .fitCenter()
                        .into(new GlideDrawableImageViewTarget(holder.imageViewProfilePic) {
                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                                super.onResourceReady(resource, animation);
                                //check isRefreshing
                                holder.progressBarBesties.setVisibility(View.GONE);
                                holder.imageViewProfilePic.setImageDrawable(resource);
                            }
                        });
                //endregion

                break;
        }
    }

I tried something like newPosition to subtract the position with 1 but it doesn't work. There are only 19 items but I add 20 to the list. The problem is the first view.. oh I forgot this:

static final int TYPE_HEADER = 0;
static final int TYPE_LIST_ITEM = 1;

That the one view is only inflated by position 0. Because of that the list starts to count at position one so the first item is missing.

How can I get all items?

Saumik Bhattacharya
  • 891
  • 1
  • 12
  • 28
blueman3399
  • 207
  • 3
  • 13
  • why not just add a dummy item to the beginning of the list where the header would be drawn? Then you can go off of the actual return position – tyczj Mar 07 '16 at 17:59
  • or in your getItemCount return the size of the list+1 the you can do your position-1 like you were doing – tyczj Mar 07 '16 at 18:02
  • Mh ok, I guess this would be a solution but this is very unlovely.. – blueman3399 Mar 07 '16 at 18:03
  • I am not addressing the actual problem but in `onBindViewHolder()` you should check against actual viewtype, not the position (which in this particular case is the same). I mean, do `switch (getItemViewType(position))` instead. – Yaroslav Mytkalyk Mar 07 '16 at 18:03
  • well the recyclerview only draws the number of items you tell it to in getItemCount and you cant make it draw more when it does not know how many more to give – tyczj Mar 07 '16 at 18:05
  • @tyczj I prefer your second solution :) thanks – blueman3399 Mar 07 '16 at 18:09
  • @YaroslavMytkalyk No this doesn't solve the problem, I tried it before but thanks though :) – blueman3399 Mar 07 '16 at 18:09
  • Good info about this here: http://stackoverflow.com/questions/26448717/android-5-0-add-header-footer-to-a-recyclerview – Daniel Nugent Mar 07 '16 at 18:39

0 Answers0