gpt4 book ai didi

android - RecyclerView notifyDataSetChanged 一直向上滚动,没有按我想要的那样工作

转载 作者:行者123 更新时间:2023-11-30 00:59:18 27 4
gpt4 key购买 nike

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

问题

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

如果 View 可见,我不希望回收器移动它应该保持不变,所以如果我单击最后可见行上的照片,我希望滚动保持不变,我不希望它使最后一行成为第一行。

尝试解决

我尝试了几种方法来解决这个问题,我尝试调用 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);
}

我认为禁用滚动可能会将回收 View 锁定在当前位置,但它并没有立即跳起来。

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

最佳答案

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 的回答(以及他们的评论)

关于android - RecyclerView notifyDataSetChanged 一直向上滚动,没有按我想要的那样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39678797/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com