RecyclerView notifyDataSetChange一直向上滚动,不按我想要的方式工作

时间:2016-09-24 16:57:20

标签: android android-recyclerview

我正在为我的应用程序内的Facebook编写一个照片选择器,我有一个带有网格布局的recyclerview,我想阻止向上滚动,我能够通过使用scrollToPosition来做到这一点,这有效但不是我想要的方式

问题

当我点击第2行的照片时,该行跳到顶部并成为第1行可见行,如果我点击3行,则会发生同样的事情。

如果视图可见,我不希望再循环器移动它应保持不变,所以如果我点击最后一行可见的一张照片,我希望滚动保持不变,我不要#39; t希望它将最后一行作为第一行。

试图解决它

我尝试了几件事来解决这个问题,我尝试拨打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锁定在正确的位置,但它没有向右跳跃。

我也试过获取Vertical偏移并在调用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;
}

1 个答案:

答案 0 :(得分:1)

$IDs = trim($_POST['IDs']); $IDs = explode("\n", $IDs); $IDs = array_map('trim', $IDs); $ID = array_unique($IDs,0); print_r($ID);

中删除这两行
onItemClick

每次picker.setAdapter(adapter); picker.scrollToPosition(position); 它重置位置,现在您不需要再次设置新位置。

这应该有效。如果没有,请查看我的答案(及其评论),提供ID How to remain at a scroll position in RecyclerView after adding items at its first index and call notifydatasetchange