Android - 滚动RecyclerView时为什么会丢失值EditText?

我正在使用波纹管代码将文本转换为货币格式,但当我滚动recyclerView时,我丢失了真实数据。我能做些什么:Android - 滚动RecyclerView时为什么会丢失值EditText?

public class ManagePriceProductsAdapter extends RecyclerView.Adapter<ManagePriceProductsAdapter.ViewHolder> { 

private List<ManagePriceProductsModel.DataValue> managePriceProductsModelList;

private Context context;

private getListDiscountInterface getListDiscountInterface;

private int vendorId = -1;

public ManagePriceProductsAdapter(Context context,

List<ManagePriceProductsModel.DataValue> managePriceProductsModelList,

getListDiscountInterface getListDiscountInterface,

int vendorId) {

this.context = context;

this.managePriceProductsModelList = managePriceProductsModelList;

this.getListDiscountInterface = getListDiscountInterface;

this.vendorId = vendorId;

}

@Override

public ManagePriceProductsAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

View view = LayoutInflater.from(context)

.inflate(R.layout.list_item_manage_price_products, viewGroup, false);

return new ManagePriceProductsAdapter.ViewHolder(view,

new DiscountNumberTextWatcherWithSeperator(),

new OriginalNumberTextWatcherWithSeperator());

}

public void getListDiscount() {

getListDiscountInterface.sendGetListDiscountToActivity(managePriceProductsModelList);

}

public void backButton() {

getListDiscountInterface.backButtonForListDiscount(managePriceProductsModelList);

}

public void resetAdapter() {

this.managePriceProductsModelList.clear();

notifyDataSetChanged();

}

@Override

public void onBindViewHolder(final ManagePriceProductsAdapter.ViewHolder viewHolder, int position) {

viewHolder.discountNumberTextWatcherWithSeperator.updatePosition(viewHolder.getAdapterPosition(),

viewHolder.discountPriceEdittext,

viewHolder.originalPriceEdittext);

viewHolder.originalNumberTextWatcherWithSeperator.updatePosition(viewHolder.getAdapterPosition(),

viewHolder.originalPriceEdittext);

try {

if (managePriceProductsModelList.get(viewHolder.getAdapterPosition()).getTitle() != null) {

if (!TextUtils.isEmpty(managePriceProductsModelList.get(viewHolder.getAdapterPosition()).getTitle().trim())) {

String productName = managePriceProductsModelList.get(viewHolder.getAdapterPosition()).getTitle().trim();

viewHolder.titleTextview.setText(productName);

} else {

viewHolder.titleTextview.setText("----");

}

} else {

viewHolder.titleTextview.setText("----");

}

} catch (Exception ex) {

if (vendorId != -1) {

Throwable t = new Throwable(ex + ", vendorId: " + vendorId).fillInStackTrace();

FirebaseCrash.report(t);

} else {

Throwable t = new Throwable(ex + ", vendorId: empty").fillInStackTrace();

FirebaseCrash.report(t);

}

}

try {

if (managePriceProductsModelList.get(viewHolder.getAdapterPosition()).getPrice() != null) {

Long productPrice = managePriceProductsModelList.get(viewHolder.getAdapterPosition()).getPrice();

if (productPrice != null && productPrice != 0 && productPrice > 0) {

viewHolder.originalPriceEdittext.setText(String.valueOf(productPrice));

} else {

viewHolder.originalPriceEdittext.setText("");

}

}

} catch (Exception ex) {

if (vendorId != -1) {

Throwable t = new Throwable(ex + ", vendorId: " + vendorId).fillInStackTrace();

FirebaseCrash.report(t);

} else {

Throwable t = new Throwable(ex + ", vendorId: empty").fillInStackTrace();

FirebaseCrash.report(t);

}

}

try {

if (managePriceProductsModelList.get(viewHolder.getAdapterPosition()).getDiscountedPrice() != null) {

Long discountPrice = managePriceProductsModelList.get(viewHolder.getAdapterPosition()).getDiscountedPrice();

if (discountPrice != null && discountPrice != 0 && discountPrice > 0) {

viewHolder.discountPriceEdittext.setText(String.valueOf(discountPrice));

} else {

viewHolder.discountPriceEdittext.setText("");

}

}

} catch (Exception ex) {

if (vendorId != -1) {

Throwable t = new Throwable(ex + ", vendorId: " + vendorId).fillInStackTrace();

FirebaseCrash.report(t);

} else {

Throwable t = new Throwable(ex + ", vendorId: empty").fillInStackTrace();

FirebaseCrash.report(t);

}

}

}

@Override

public int getItemCount() {

return managePriceProductsModelList.size();

}

public interface getListDiscountInterface {

void sendGetListDiscountToActivity(List<ManagePriceProductsModel.DataValue> managePriceProductsModelList);

void backButtonForListDiscount(List<ManagePriceProductsModel.DataValue> managePriceProductsModelList);

}

class ViewHolder extends RecyclerView.ViewHolder {

//region ViewBinding

@BindView(R.id.title_textview)

TextView titleTextview;

@BindView(R.id.original_price_edittext)

EditText originalPriceEdittext;

@BindView(R.id.discount_price_edittext)

EditText discountPriceEdittext;

DiscountNumberTextWatcherWithSeperator discountNumberTextWatcherWithSeperator;

OriginalNumberTextWatcherWithSeperator originalNumberTextWatcherWithSeperator;

//endregion

ViewHolder(View view,

DiscountNumberTextWatcherWithSeperator discountNumberTextWatcherWithSeperator,

OriginalNumberTextWatcherWithSeperator originalNumberTextWatcherWithSeperator) {

super(view);

ButterKnife.bind(this, itemView);

this.discountNumberTextWatcherWithSeperator = discountNumberTextWatcherWithSeperator;

this.discountPriceEdittext.addTextChangedListener(discountNumberTextWatcherWithSeperator);

this.originalNumberTextWatcherWithSeperator = originalNumberTextWatcherWithSeperator;

this.originalPriceEdittext.addTextChangedListener(originalNumberTextWatcherWithSeperator);

}

}

private class DiscountNumberTextWatcherWithSeperator implements TextWatcher {

private EditText discountEditText;

private EditText originalEditText;

private int position;

private String getDecimalFormattedString(String value) {

StringTokenizer lst = new StringTokenizer(value, ".");

String str1 = value;

String str2 = "";

if (lst.countTokens() > 1) {

str1 = lst.nextToken();

str2 = lst.nextToken();

}

String str3 = "";

int i = 0;

int j = -1 + str1.length();

if (str1.charAt(-1 + str1.length()) == '.') {

j--;

str3 = ".";

}

for (int k = j; ; k--) {

if (k < 0) {

if (str2.length() > 0)

str3 = str3 + "." + str2;

return str3;

}

if (i == 3) {

str3 = "," + str3;

i = 0;

}

str3 = str1.charAt(k) + str3;

i++;

}

}

public void updatePosition(int position,

EditText discountEditText,

EditText originalEditText) {

this.position = position;

this.discountEditText = discountEditText;

this.originalEditText = originalEditText;

}

@Override

public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override

public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

if (charSequence.length() > 0) {

originalEditText.setPaintFlags(originalEditText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

ManagePriceProductsModel.DataValue dataValue = new ManagePriceProductsModel.DataValue();

dataValue.setTitle(managePriceProductsModelList.get(position).getTitle().trim());

dataValue.setId(managePriceProductsModelList.get(position).getId());

dataValue.setPrice(managePriceProductsModelList.get(position).getPrice());

dataValue.setDiscountedPrice(Long.parseLong(charSequence.toString().replace(",", "")));

managePriceProductsModelList.set(position, dataValue);

} else if (charSequence.length() == 0) {

originalEditText.setPaintFlags(originalEditText.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));

ManagePriceProductsModel.DataValue dataValue = new ManagePriceProductsModel.DataValue();

dataValue.setTitle(managePriceProductsModelList.get(position).getTitle().trim());

dataValue.setId(managePriceProductsModelList.get(position).getId());

dataValue.setPrice(managePriceProductsModelList.get(position).getPrice());

dataValue.setDiscountedPrice(null);

managePriceProductsModelList.set(position, dataValue);

}

}

@Override

public void afterTextChanged(Editable editable) {

try {

discountEditText.removeTextChangedListener(this);

String value = discountEditText.getText().toString();

if (!value.equals("")) {

if (value.startsWith(".")) {

discountEditText.setText("0.");

}

if (value.startsWith("0") && !value.startsWith("0.")) {

discountEditText.setText("");

}

String str = discountEditText.getText().toString().replaceAll(",", "");

if (!value.equals(""))

discountEditText.setText(getDecimalFormattedString(str));

discountEditText.setSelection(discountEditText.getText().toString().length());

}

discountEditText.addTextChangedListener(this);

} catch (Exception ex) {

ex.printStackTrace();

discountEditText.addTextChangedListener(this);

}

}

}

private class OriginalNumberTextWatcherWithSeperator implements TextWatcher {

private EditText editText;

private int position;

private String getDecimalFormattedString(String value) {

StringTokenizer lst = new StringTokenizer(value, ".");

String str1 = value;

String str2 = "";

if (lst.countTokens() > 1) {

str1 = lst.nextToken();

str2 = lst.nextToken();

}

String str3 = "";

int i = 0;

int j = -1 + str1.length();

if (str1.charAt(-1 + str1.length()) == '.') {

j--;

str3 = ".";

}

for (int k = j; ; k--) {

if (k < 0) {

if (str2.length() > 0)

str3 = str3 + "." + str2;

return str3;

}

if (i == 3) {

str3 = "," + str3;

i = 0;

}

str3 = str1.charAt(k) + str3;

i++;

}

}

public void updatePosition(int position,

EditText editText) {

this.position = position;

this.editText = editText;

}

@Override

public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override

public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

if (charSequence.length() > 0) {

ManagePriceProductsModel.DataValue dataValue = new ManagePriceProductsModel.DataValue();

dataValue.setTitle(managePriceProductsModelList.get(position).getTitle().trim());

dataValue.setId(managePriceProductsModelList.get(position).getId());

dataValue.setPrice(Long.parseLong(charSequence.toString().replace(",", "")));

dataValue.setDiscountedPrice(managePriceProductsModelList.get(position).getDiscountedPrice());

managePriceProductsModelList.set(position, dataValue);

} else if (charSequence.length() == 0) {

ManagePriceProductsModel.DataValue dataValue = new ManagePriceProductsModel.DataValue();

dataValue.setTitle(managePriceProductsModelList.get(position).getTitle().trim());

dataValue.setId(managePriceProductsModelList.get(position).getId());

dataValue.setPrice(null);

dataValue.setDiscountedPrice(managePriceProductsModelList.get(position).getDiscountedPrice());

managePriceProductsModelList.set(position, dataValue);

}

}

@Override

public void afterTextChanged(Editable editable) {

try {

editText.removeTextChangedListener(this);

String value = editText.getText().toString();

if (!value.equals("")) {

if (value.startsWith(".")) {

editText.setText("0.");

}

if (value.startsWith("0") && !value.startsWith("0.")) {

editText.setText("");

}

String str = editText.getText().toString().replaceAll(",", "");

if (!value.equals(""))

editText.setText(getDecimalFormattedString(str));

editText.setSelection(editText.getText().toString().length());

}

editText.addTextChangedListener(this);

} catch (Exception ex) {

ex.printStackTrace();

editText.addTextChangedListener(this);

}

}

}

}

回答:

如上的意见,不建议在recyclerview使用的EditText。

如果你必须这样做,你可以尝试使用

viewHolder.setIsRecyclable(false); 

onBindViewHolder方法

以上是 Android - 滚动RecyclerView时为什么会丢失值EditText? 的全部内容, 来源链接: utcz.com/qa/261209.html

回到顶部