Tiago Oliveira Tiago Oliveira - 1 year ago 91
Android Question

RecyclerView notifyDataSetChange scroling all the way up, not working as i want it to

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

and this works but not the way i want


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
i followed this How to disable RecyclerView scrolling?

public static void onItemClick(int position){
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
photoBag.set(k, new PhotoBag(photoBag.get(k).getPhoto(), false));//Here im setting unselecting all the other photos



//Log.d("FacebookPicker", "position " + grid.findFirstCompletelyVisibleItemPosition());

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



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) {
photo = (ImageView) view.findViewById(R.id.photoItem);

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

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

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

public void onBindViewHolder(final PickerAdapterHolder holder, final int position) {
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);
}else {

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.drawBitmap(bmp, borderSize, borderSize, null);
return bmpWithBorder;

Answer Source

remove those 2 lines from onItemClick


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