gpt4 book ai didi

android - 如何从 Recyclerview 中删除项目

转载 作者:行者123 更新时间:2023-11-29 15:34:27 25 4
gpt4 key购买 nike

friend ...

在我的项目中,我正在从服务器创建一个图像列表并显示在 Recyclerview 中。

现在我想在单击验证按钮时从列表中删除特定项目。

我试图隐藏 holder.itemview 但当我滚动时它会再次显示。

我只想删除或隐藏已验证一次的项目。

这是我的代码:

1) 主要 Activity

public class MainActivity extends AppCompatActivity {
private static final String url = "www.mysite.com/api/api-id-list.php?action=imgs";

private ProgressDialog mDetailProgress;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main_activity);

LoadRecyclerView();
}

private void LoadRecyclerView() {

mDetailProgress = new ProgressDialog(this);
mDetailProgress.setTitle("Loading images");
mDetailProgress.setMessage("Please Wait...");
mDetailProgress.setCanceledOnTouchOutside(false);
mDetailProgress.show();

final RecyclerView imgList = (RecyclerView) findViewById(R.id.imgList);

imgList.setLayoutManager(new LinearLayoutManager(this));

StringRequest request = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {

GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Image[] images = gson.fromJson(response, Image[].class);

imgList.setAdapter(new ImageAdapter(MainActivity.this, images));
mDetailProgress.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getBaseContext(), "Something went wrong..!", Toast.LENGTH_LONG);
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
}}

2) 图像适配器

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {

private ProgressDialog mDetailProgress;

private Context context;
private Image[] data;

Button btn_ban, btn_verify;
private View view;

public ImageAdapter (Context context, Image[] data) {
this.context = context;
this.data = data;
}

@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.single_item, parent, false);
return new ImageViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final ImageViewHolder holder, final int position) {
final Image imagelist = data[position];

holder.userCode.setText(imagelist.getCode());
Glide.with(holder.userImage.getContext()).load(imagelist.getIdPhoto()).into(holder.userImage);

btn_verify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.itemView.setVisibility(View.GONE);
}
});
}

@Override
public int getItemCount() { return data.length; }

public class ImageViewHolder extends RecyclerView.ViewHolder {
TextView userCode;
ImageView userImage;

public ImageViewHolder(@NonNull View itemView) {
super(itemView);
userImage = (ImageView) itemView.findViewById(R.id.card_iv_img);
userCode = (TextView) itemView.findViewById(R.id.card_tv_code);
btn_ban = (Button) itemView.findViewById(R.id.btn_ban);
btn_verify = (Button) itemView.findViewById(R.id.btn_verify);
}

}}

3) Image.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Image {

@SerializedName("per_ID")
@Expose
private String perID;
@SerializedName("code")
@Expose
private String code;
@SerializedName("first_nm")
@Expose
private String firstNm;
@SerializedName("last_nm")
@Expose
private String lastNm;
@SerializedName("photo_ID")
@Expose
private String photoID;
@SerializedName("id_photo")
@Expose
private String idPhoto;

public String getPerID() {
return perID;
}

public void setPerID(String perID) {
this.perID = perID;
}

public Image withPerID(String perID) {
this.perID = perID;
return this;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public Image withCode(String code) {
this.code = code;
return this;
}

public String getFirstNm() {
return firstNm;
}

public void setFirstNm(String firstNm) {
this.firstNm = firstNm;
}

public Image withFirstNm(String firstNm) {
this.firstNm = firstNm;
return this;
}

public String getLastNm() {
return lastNm;
}

public void setLastNm(String lastNm) {
this.lastNm = lastNm;
}

public Image withLastNm(String lastNm) {
this.lastNm = lastNm;
return this;
}

public String getPhotoID() {
return photoID;
}

public void setPhotoID(String photoID) {
this.photoID = photoID;
}

public Image withPhotoID(String photoID) {
this.photoID = photoID;
return this;
}

public String getIdPhoto() {
return idPhoto;
}

public void setIdPhoto(String idPhoto) {
this.idPhoto = idPhoto;
}

public Image withIdPhoto(String idPhoto) {
this.idPhoto = idPhoto;
return this;
}}

最佳答案

首先让数据不是array,而是List,这样很容易删除item;

其次在 ViewHolder 和 OnClick 中设置验证按钮的监听器删除项目并通知适配器。

 btn_verify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
data.remove(position);
notifyItemRemoved(position);
}
});

注意:在 ViewHolder 中设置 OnClickListener 可确保通过使用 getAdapterPosition() 获取正确的位置只移除正确的项目。onBindViewHolder 提供的位置在插入新项目后可能无效。

关于android - 如何从 Recyclerview 中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54460744/

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