1

I am writing a photo picker for Facebook inside my app, i have a recyclerview with a grid layout and i want to prevent for scrolling up, i was able to do this by using scrollToPosition and this works but not the way i want

Problem

When i click in a photo on the 2 row that row jumps to the top and becomes the number 1 visible row, if i click the 3 row the samething happens.

I don't want the recycler to move if the view is visible it should remain the same, so if i click on a a photo that is on the last visible row i want the scroll to stay the same, i don't want it to make the last row the first.

Tries to solve it

I tried several things to fix this, i tried calling setNestedScrollingEnabled i followed this How to disable RecyclerView scrolling?

 public static void onItemClick(int position){
    //picker.setNestedScrollingEnabled(false);
    for(int k = 0; k<photoBag.size();k++) {
        if(k == position)
            photoBag.set(position, new PhotoBag(photoBag.get(position).getPhoto(), true)); //Here im marking the photo to selected
        else
            photoBag.set(k, new PhotoBag(photoBag.get(k).getPhoto(), false));//Here im setting unselecting all the other photos
    }

    picker.setAdapter(adapter);
    adapter.notifyDataSetChanged();

    picker.scrollToPosition(position);

    //Log.d("FacebookPicker", "position " + grid.findFirstCompletelyVisibleItemPosition());
    //picker.setNestedScrollingEnabled(true);
}

I thought that maybe disabling the scroll would lock the recyclerview on the corrent position but it didn't jumps right up.

I also tried getting the Vertical offset and set it after calling notifyDataSetChange but i can't find a way to set the offset programmatically

EDIT

Adapter

class PickerAdapter extends RecyclerView.Adapter<PickerAdapter.PickerAdapterHolder>   {
public final String TAG = "PickerAdapter";

private ArrayList<PhotoBag> photoBag;
private Context context;

private OnClickListener onClickListener;


class PickerAdapterHolder extends RecyclerView.ViewHolder implements View.OnClickListener  {

    ImageView photo;
    ImageView imageBorder;

    PickerAdapterHolder(View view) {
        super(view);
        photo = (ImageView) view.findViewById(R.id.photoItem);
        photo.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.photoItem:
                FacebookPhotoPicker.onItemClick(getAdapterPosition()); //i know that there are better ways to get the clicked item from other class but since im still debuging i don't need to worry about performace i just need it to work
                break;
        }
    }
}

PickerAdapter(Context context, ArrayList<PhotoBag> itemList) {
    this.photoBag = itemList;
    this.context = context;
}


@Override
public PickerAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.facebook_picker_item, null);
    return new PickerAdapterHolder(layoutView);
}

@Override
public void onBindViewHolder(final PickerAdapterHolder holder, final int position) {
    if(photoBag.get(position).isSelected()){
        int border = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, context.getResources().getDisplayMetrics()));

        Bitmap photo = photoBag.get(position).getPhoto();
        photo = Bitmap.createScaledBitmap(photo,photo.getWidth() - (border*2), photo.getHeight() - (border*2), false);
        photo = addWhiteBorder(photo,border);
        holder.photo.setImageBitmap(photo);
    }else {
        holder.photo.setImageBitmap(photoBag.get(position).getPhoto());
    }
}

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


private Bitmap addWhiteBorder(Bitmap bmp, int borderSize) {
    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2, bmp.getHeight() + borderSize * 2, bmp.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(bmp, borderSize, borderSize, null);
    return bmpWithBorder;
}
Community
  • 1
  • 1
Tiago Oliveira
  • 1,582
  • 1
  • 16
  • 31
  • correct me if i am wrong, your problem is let you have 10 item in recylerview and now you add new item from fb ,after adding item now your recylerview goes again to first item ? – Hamza Sep 24 '16 at 17:58
  • im not adding items, when i click on a photo i set it as selected, sorry for the misunderstanding i will update my question to make it more clear, it jumps to first when i update it to selected – Tiago Oliveira Sep 24 '16 at 18:00
  • you want highlight selected item on RecyclerView? some thing like thak? – Hamza Sep 24 '16 at 18:03
  • yes i can do that but i don't want the recycler to go up, i will share my recycler class give me a second – Tiago Oliveira Sep 24 '16 at 18:06
  • i added my adapter class – Tiago Oliveira Sep 24 '16 at 18:15
  • try this vidieo it may be help , https://www.youtube.com/watch?v=Am7LgHCdZvI – Hamza Sep 24 '16 at 18:41

1 Answers1

1

remove those 2 lines from onItemClick

picker.setAdapter(adapter);
picker.scrollToPosition(position);

every time you setAdapter it resets position, and now you don't need to set a new position again.

this should work. If it doesn't, check this answer of mine (and their comments) about providing ID How to remain at a scroll position in RecyclerView after adding items at its first index and call notifydatasetchange

Community
  • 1
  • 1
Budius
  • 39,391
  • 16
  • 102
  • 144