gpt4 book ai didi

android - 从 recyclerview 中删除所有 cardviews 然后添加新的 cardviews - 添加相同的 cardviews

转载 作者:行者123 更新时间:2023-11-29 18:43:10 29 4
gpt4 key购买 nike

当用户按下 FAB 时,一个 cardview 被添加到 recyclerview。每个卡片 View 内都有一个复选框。我想这样做,以便当用户勾选特定卡片 View 中的复选框时,该特定卡片 View 将被删除。

(仅供引用,每个卡片 View 都有一个微调器和复选框以及一个 TextView 和编辑文本)

我按了 4 次 fab 以将 4 个卡片 View 添加到我的屏幕。然后我从微调器中选择了不同的东西来查看发生了什么。当我勾选复选框时,该卡片 View 会删除。我勾选了所有卡片 View 的复选框,直到我没有留下任何卡片 View 。

但是当我按下 fab 添加新的 cardview 时,我注意到 cardview 具有相同的选定微调器值并且复选框已被选中..?例如,假设我首先有一个带有“土 bean ”的卡片 View ,然后我勾选了它。现在我的屏幕上什么也没有了。然后我按 fab 添加卡片 View 。然后我看到那个 cardview 上面有“土 bean ”作为 wlel,并且勾选了复选框..如果这没有意义,请要求澄清

怎么办?

ProductAdapter.java

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {

private Map<Integer, Integer> mSpinnerSelectedItem = new HashMap<Integer, Integer>();


//this context we will use to inflate the layout
//Remove this..Please
// CheckBox checkBox;

//private Context mCtx;
private SearchableSpinner spinner;

//we are storing all the products in a list
private List<Product> productList;

private Activity create;

public ProductAdapter(Activity activity) {
create = activity;
}


//getting the context and product list with constructor
public ProductAdapter(Activity activity, List<Product> productList) {
// this.mCtx = mCtx;
create = activity;
this.productList = productList;
}

@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating and returning our view holder
LayoutInflater inflater = LayoutInflater.from(create);
View view = inflater.inflate(R.layout.layout_products, null);
return new ProductViewHolder(view);
}

@Override
public void onBindViewHolder(final ProductViewHolder holder, final int position) {
// //getting the product of the specified position


ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(create, R.layout.item_spinner_layout,
Product.getSpinnerItemsList());
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

holder.spinner.setAdapter(spinnerArrayAdapter);

holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int mPosition, long id) {
mSpinnerSelectedItem.put(position, mPosition);

TextView mTextView = view.findViewById(R.id.mSpinnerText);
/* Toast.makeText(create, "Selected Item: " + mTextView.getText().toString(), Toast.LENGTH_LONG).show();
Log.e("***************", "Selected Item: " + mTextView.getText().toString());*/


}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});


//binding the data with the viewholder views
if (mSpinnerSelectedItem.containsKey(position)) {
holder.spinner.setSelection(mSpinnerSelectedItem.get(position));
}


holder.getView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(create);


// set title
alertDialogBuilder.setTitle("Delete Item");

// set dialog message
alertDialogBuilder
.setMessage("Are you sure you want to delete this item?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity


holder.checkBox.setChecked(false);
holder.spinner.setSelection(0);

productList.remove(position);
notifyItemRemoved(position);

Toast.makeText(create, "Item removed.", Toast.LENGTH_LONG).show();


}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();

}
});





}


@Override
public int getItemCount() {
return productList.size();
}


class ProductViewHolder extends RecyclerView.ViewHolder {

SearchableSpinner spinner;
EditText editText;
TextView textView5;
CheckBox checkBox;
LinearLayout linearLayout;
View rootView;


public ProductViewHolder(View itemView) {
super(itemView);

spinner = itemView.findViewById(R.id.spinner);
editText = itemView.findViewById(R.id.editText);
textView5 = itemView.findViewById(R.id.textView5);
checkBox = itemView.findViewById(R.id.checkBox);
rootView = itemView.findViewById(R.id.linearLayout);


checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// makes the set disappear when checkbox is ticked.
if(isChecked){

holder.checkBox.setChecked(false);
holder.spinner.setSelection(0);

productList.remove(getAdapterPosition());
notifyItemRemoved(getAdapterPosition());



Toast.makeText(create, "Done!", Toast.LENGTH_LONG).show();
}

}
});



}

public View getView() {
return rootView;
}

}

}

最佳答案

RecyclerView 回收 ViewHolders 以提高性能。这意味着,如果“ViewHolder A”在您的微调器中选择了“选项 C”,即使您从列表中删除该项目,它也不会被销毁。当您向列表中添加新项目时,您的 RecyclerView 可能会为该项目重用“ViewHolder A”,并且选择与您之前的项目(在删除之前)相同。

为避免这种情况,请在使用 LayoutManagerfindViewByPosition 方法将新项目添加到列表时手动更新 UI。

例子:

// FAB button onClick method:
public void onClick(View v) {
// Add item to RecyclerView list

int itemPosition = myRecyclerViewAdapter.itemList.indexOf(newItem);
ProductViewHolder vh = ((ProductViewHolder)recyclerView.getLayoutManager().findViewByPosition(itemPosition));

// Here, set everything to the default values (unchecked checkbox, etc)
}

如果你想在你的 Adapter 中保留所有 RecyclerView 相关代码(我鼓励你这样做),你可以在你之后将 ViewHolder UI 更新为默认值删除您的项目(而不是在添加项目时),因此您的 ViewHolder 是新鲜的并为下一个项目做好准备。

示例(更好地使用此解决方案):

// Your positive button when deleting item onClick:
public void onClick(DialogInterface dialog, int id) {

productList.remove(position);
notifyItemRemoved(position);

// Set spinner to default
// Set checkbox to unchecked

Toast.makeText(create, "Item removed.", Toast.LENGTH_LONG).show();
}

关于android - 从 recyclerview 中删除所有 cardviews 然后添加新的 cardviews - 添加相同的 cardviews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52586409/

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