gpt4 book ai didi

java - 自定义卡片 View 适配器 : notify data set changed not working

转载 作者:行者123 更新时间:2023-11-30 10:00:06 25 4
gpt4 key购买 nike

我设置了自定义卡片 View slider 适配器,启动时卡片 View 加载正常,但在数组列表中添加/替换项目并调用通知适配器方法后,内容没有发生任何变化。

我已经尝试通过公共(public)方法直接从适配器添加项目并且项目被添加但是,它总是放在最后一个位置。

这是我的适配器:

static class UploadImageAdapter extends CardSliderAdapter<ImageObject>
{
private Context context;
private ArrayList<ImageObject> arrayList;

private OnUploadButtonClick onUploadButtonClick;

public interface OnUploadButtonClick{
void upload(int position);
}

UploadImageAdapter(Context context, ArrayList<ImageObject> items) {
super(items);
this.context = context;
this.arrayList = items;
}

static class ViewHolder{
ImageView image;
ImageButton addImage;
Button removeImage;
View layout;
}

@Override
public void bindView(int position, @NotNull View itemContentView, ImageObject imageObject) {
final ViewHolder holder = new ViewHolder();

holder.image = itemContentView.findViewById(R.id.card_image);
holder.addImage = itemContentView.findViewById(R.id.upload_image_btn);
holder.removeImage = itemContentView.findViewById(R.id.card_remove_btn);
holder.layout = itemContentView.findViewById(R.id.card_image_layout);

if (imageObject.hasImage) {
Glide.with(context)
.load(Uri.parse(imageObject.getImage_path()))
.placeholder(R.color.offsetWhiteBackground)
.centerCrop()
.transition(withCrossFade())
.into(holder.image);

holder.addImage.setVisibility(View.GONE);
holder.layout.setVisibility(View.VISIBLE);
holder.removeImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});
}
else {
holder.layout.setVisibility(View.GONE);
holder.addImage.setVisibility(View.VISIBLE);
holder.addImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onUploadButtonClick.upload(position);
}
});
}
}

@Override
public ImageObject getItem(int position) {
return arrayList.get(position);
}

void setOnUploadButtonClick(OnUploadButtonClick onUploadButtonClick){
this.onUploadButtonClick = onUploadButtonClick;
}

@Override
public int getItemContentLayout(int position) {
//TODO return the item layout of every position
return R.layout.upload_image_card;
}
}

下面是我实例化的地方:

private void setCardSlider() {
upload = new ArrayList<>();

for(int i = 0; i < 3; i++){
upload.add(new ImageObject(i,false,""));
}

adapter = new UploadImageAdapter(getContext(), upload);
CardSliderViewPager cardSliderViewPager = rootView.findViewById(R.id.image_upload_slider);
cardSliderViewPager.setAdapter(adapter);

uploadDialog = new UploadDialog(getActivity(), getContext());

adapter.setOnUploadButtonClick((int position) -> {
uploadDialog.callUploadDialog();
mPosition = position;
});
}

public void uploadImage(){
uploadDialog.callUploadDialog();
}

public void setImage(Uri uri){
upload.get(mPosition).setHasImage(true);
upload.get(mPosition).setImage_path(uri.toString());
adapter.notifyDataSetChanged();
}

最佳答案

我建议使用 notifyItemChange反而。在那里你使用一个位置作为参数,它不需要重绘一切。

关于java - 自定义卡片 View 适配器 : notify data set changed not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58343238/

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