2016-09-24 94 views
1

我正在写我的应用程序的Facebook的照片选择器,我有一个网格布局recyclerview,我想防止滚动,我能通过使用scrollToPosition做到这一点,这工作,但没有办法,我想RecyclerView notifyDataSetChange一直滚动,不工作,因为我想它

问题

当我在照片点击2行该行跳转到顶部,并成为数1可见行,如果我点击3行发生相同的事情。

我不希望回收商移动,如果视图是可见的,它应该保持不变,所以如果我点击最后一个可见行上的照片,我希望滚动保持不变,不想让它成为最后一排。

试图解决它

我试过几件事情要解决这个问题,我试着打电话setNestedScrollingEnabled我跟着这个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); 
} 

我想,也许禁用滚动将锁定在了recyclerview但是它并没有跳起来。

我也试图让垂直偏移量,调用notifyDataSetChange后成立,但我不能找到一种方法来设置程序偏移

编辑

适配器

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; 
} 
+0

纠正我,如果我错了,你的问题是让你有10项在recylerview,现在你从fb添加新项目,之后现在添加项目您的recylerview再次到第一个项目? – Hamza

+0

即时消息不添加项目,当我点击一张照片,我把它设置为选定,抱歉的误解我会更新我的问题,使其更清楚,它跳转到第一次,当我更新到选定的 –

+0

你想要突出选定的项目RecyclerView?有些东西像泰克? – Hamza

回答

1

删除那些2行onItemClick

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

每次你setAdapter它重置位置,现在你不需要再次设置一个新的位置。

这应该工作。如果它不,请检查我的回答(和他们的意见)关于提供ID How to remain at a scroll position in RecyclerView after adding items at its first index and call notifydatasetchange

+0

修复了这个问题非常感谢,我认为我需要重置适配器 –