0

What I have

I have a GridLayoutManager (RecyclerView) that has position 0,2,5,7 fixed with custom images (relative layout converted to bitmaps) & rest positions are filled with server images

My problem

When I scroll the items in the layout changes its position

My code

@Override
    public void onBindViewHolder(ProfilePhotosViewHolder ProfilePhotosViewHolder, final int position) {
        model = list.get(position);

        if (position == 0 || position == 2 || position == 5 || position == 7) {
            if (position == 0) {
                ProfilePhotosViewHolder.relativeBucket.setVisibility(View.VISIBLE);
                ProfilePhotosViewHolder.relativeBucket.setDrawingCacheEnabled(true);
                ProfilePhotosViewHolder.relativeBucket.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                ProfilePhotosViewHolder.relativeBucket.layout(0, 0, ProfilePhotosViewHolder.relativeBucket.getMeasuredWidth(), ProfilePhotosViewHolder.relativeBucket.getMeasuredHeight());
                ProfilePhotosViewHolder.relativeBucket.buildDrawingCache(true);
                Bitmap b = Bitmap.createBitmap(ProfilePhotosViewHolder.relativeBucket.getDrawingCache());
                ProfilePhotosViewHolder.relativeBucket.setDrawingCacheEnabled(false);
                ProfilePhotosViewHolder.imgProfilePhotos.setImageBitmap(b);
            }

            if (position == 2) {
                ProfilePhotosViewHolder.relativeFollowers.setVisibility(View.VISIBLE);
                ......

            }

            if (position == 5) {
                ProfilePhotosViewHolder.relativeFollowing.setVisibility(View.VISIBLE);
               ......
            }

            if (position == 7) {
                ProfilePhotosViewHolder.relativePosts.setVisibility(View.VISIBLE);
                ......
            }

        } else {

            ProfilePhotosViewHolder.relativeBucket.setVisibility(View.GONE);
            ProfilePhotosViewHolder.relativeFollowers.setVisibility(View.GONE);
            ProfilePhotosViewHolder.relativeFollowing.setVisibility(View.GONE);
            ProfilePhotosViewHolder.relativePosts.setVisibility(View.GONE);

            ImageLoader imageLoader = AppController.getInstance().getImageLoader();
            ProfilePhotosViewHolder.imgProfilePhotos.setImageUrl(model.ImagesUri, imageLoader);
            ProfilePhotosViewHolder.imgProfilePhotos.setDefaultImageResId(R.drawable.im_profile_monuments);
            ProfilePhotosViewHolder.imgProfilePhotos.setErrorImageResId(R.drawable.im_profile_monuments);
        }

Screen shot

After scrolling

Rami
  • 7,879
  • 12
  • 36
  • 66
karthik kolanji
  • 2,044
  • 5
  • 20
  • 56

1 Answers1

4

This is because of the recycling mechanism, while you're playing with visibility, you also need to handle the visibility of the other Views in each if block.

eg for position 2:

if (position == 2) {

    ProfilePhotosViewHolder.relativeFollowers.setVisibility(View.VISIBLE);

    ProfilePhotosViewHolder.relativeBucket.setVisibility(View.GONE);
    ProfilePhotosViewHolder.relativeFollowing.setVisibility(View.GONE);
    ProfilePhotosViewHolder.relativePosts.setVisibility(View.GONE);
}

PS: I suggest you to use switch(position) instead of nested if blocks.

Rami
  • 7,879
  • 12
  • 36
  • 66
  • any advantage of switch over if in my case ? – karthik kolanji Feb 17 '16 at 08:24
  • 2
    @karthikkolanji : Refer this : http://stackoverflow.com/questions/767821/is-else-if-faster-than-switch-case. It's about how it's implemented internally. And yes, +1 for prettier code :) – Yash Feb 17 '16 at 08:32