在Android中滚动recyclelerview时,Edittext值消失

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Android中滚动recyclelerview时,Edittext值消失相关的知识,希望对你有一定的参考价值。

我已经调用了recyclelerview,其中我的arraylist与适配器连接。当适配器有自己的行文件时,我已经采取了一个编辑文本并手动输入数量。

但问题是当我滚动recyclerview时,编辑文本值已经消失。所以无法看到我输入的值。

适配器类

public class AddSecondarySalesOrderProductDetailsAdapter extends RecyclerView.Adapter {

    ArrayList<SecondarySalesProductDetailsModel> mArrSecondarySalesProductDetailsModel;
    ArrayList<String> mArrProductQuantity;
    private int mViewItem = 1;
    private Activity mActivity;
    private LayoutInflater mLayoutInflater;
    ImageView mImageDeleteProduct;
    private String mProductName, mProductAvaQuantity, mProductSKU, mOrderedQuantity;

    /**
     * Adapter contains the data to be displayed
     */
    public AddSecondarySalesOrderProductDetailsAdapter(Activity mActivity, ArrayList<SecondarySalesProductDetailsModel>
            mArrSecondarySalesProductDetailsModel) {
        this.mActivity = mActivity;
        this.mArrSecondarySalesProductDetailsModel = mArrSecondarySalesProductDetailsModel;
        mArrProductQuantity = new ArrayList<>();
        mLayoutInflater = LayoutInflater.from(mActivity);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.raw_add_secondary_sales_order_product_details, parent,
                false);
        return new CustomViewHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        return mArrSecondarySalesProductDetailsModel.get(position) != null ? mViewItem : 0;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        holder.setIsRecyclable(false);
        final SecondarySalesProductDetailsModel mAllProductDetailsModel = mArrSecondarySalesProductDetailsModel.get(position);
        ((CustomViewHolder) holder).mImageDeleteProduct.setTag(position);

        mProductName = mAllProductDetailsModel.getProductName();
        mProductAvaQuantity = String.valueOf(mAllProductDetailsModel.getAvalQty());
        mProductSKU = mAllProductDetailsModel.getSku();

        ((CustomViewHolder) holder).mTextProductName.setText(mProductName);
        ((CustomViewHolder) holder).mTextProductAvaQuantity.setText(mProductAvaQuantity);
        ((CustomViewHolder) holder).mTextProductSKU.setText(mProductSKU);

        ((CustomViewHolder) holder).mImageDeleteProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mArrSecondarySalesProductDetailsModel.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, mArrSecondarySalesProductDetailsModel.size());

                if (mArrSecondarySalesProductDetailsModel.size() == 0) {
                    AddSecondarySalesOrderProductDetailFragment.mRelativeNoRecords.setVisibility(View.VISIBLE);
                }
            }
        });
        OrderedQuantityTextWatcher textWatcher = new OrderedQuantityTextWatcher(holder, ((CustomViewHolder) holder).mEditOrderedQuantity, position);
        ((CustomViewHolder) holder).mEditOrderedQuantity.addTextChangedListener(textWatcher);
        ((CustomViewHolder) holder).mEditOrderedQuantity.setTag(position);
    }

    private class OrderedQuantityTextWatcher implements TextWatcher {
        private EditText editText;
        private int position;
        private RecyclerView.ViewHolder holder;

        public OrderedQuantityTextWatcher(RecyclerView.ViewHolder holder, EditText editText, int
                pos) {
            this.holder = holder;
            this.editText = editText;
            this.position = pos;
        }

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
//            mOrderedQuantity = mEditOrderedQuantity.getText().toString().trim();
        }

        @Override
        public void afterTextChanged(Editable editable) {
//            int position = holder.getAdapterPosition();
            int position = (int) editText.getTag();

            Common.insertLog("Position: " + position);
            Common.insertLog("Value: " + editable);

            SecondarySalesProductDetailsModel secondarySalesProductDetailsModel = new
                    SecondarySalesProductDetailsModel();
            secondarySalesProductDetailsModel.setId(mArrSecondarySalesProductDetailsModel.get(position).getId());
            secondarySalesProductDetailsModel.setProductName
                    (mArrSecondarySalesProductDetailsModel.get(position).getProductName());
            secondarySalesProductDetailsModel.setAvalQty(mArrSecondarySalesProductDetailsModel
                    .get(position).getAvalQty());
            secondarySalesProductDetailsModel.setOrderedQty(editable.toString().trim());
            secondarySalesProductDetailsModel.setSku(mArrSecondarySalesProductDetailsModel.get
                    (position).getSku());
            secondarySalesProductDetailsModel.setProductId(mArrSecondarySalesProductDetailsModel
                    .get(position).getProductId());
            secondarySalesProductDetailsModel.setSelected(mArrSecondarySalesProductDetailsModel
                    .get(position).getSelected());
//            mArrSecondarySalesProductDetailsModel.add(secondarySalesProductDetailsModel);

            Intent intent = new Intent(AppConstants.BROADCAST_SEND_SECONDARY_PRODUCTS);
            intent.putExtra(AppConstants.BUNDLE_BROADCAST_ORDERED_QUANTITY,
                    secondarySalesProductDetailsModel);
            LocalBroadcastManager.getInstance(mActivity).sendBroadcast(intent);
        }
    }

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

    /**
     * Initialization of components
     */
    public class CustomViewHolder extends RecyclerView.ViewHolder {

        TextView mTextProductName, mTextProductAvaQuantity, mTextProductSKU;
        EditText mEditOrderedQuantity;
        ImageView mImageDeleteProduct;

        public CustomViewHolder(final View itemView) {
            super(itemView);
            this.mImageDeleteProduct = (ImageView) itemView.findViewById(R.id
                    .image_raw_secondary_sales_order_delete);
            this.mTextProductName = (TextView) itemView.findViewById(R.id.text_raw_secondary_sales_order_product_name);
            this.mTextProductAvaQuantity = (TextView) itemView.findViewById(R.id
                    .text_raw_secondary_sales_order_ava_quantity);
            this.mTextProductSKU = (TextView) itemView.findViewById(R.id
                    .text_raw_secondary_sales_order_product_sku);
            this.mEditOrderedQuantity = (EditText) itemView.findViewById(R.id
                    .edit_raw_secondary_sales_order_quantity);
        }
    }
答案

我在模型类中再创建一个字段并在对象创建时设置为空,设置为在onBindViewHolder()中编辑文本,对文本更改更新该对象也是如此。

另一答案

问题:如果滚动recyclelerview时您的edittext值为0

解决方案:然后使用下面的代码到textWatcher方法中的Edittext afterTextchange()

Code : yourEditTextName.setSelection(yourEditTextName.getText().length());       

以上是关于在Android中滚动recyclelerview时,Edittext值消失的主要内容,如果未能解决你的问题,请参考以下文章

Android - 在 ListView 中滚动时滚动出/折叠元素

android中怎样隐藏滚动条

在 Android 中滚动“突出的顶部应用栏”的应用中的延迟滚动行为

android 怎么在类中添加滚动条

Android在滚动时修复了滚动视图中的背景图像

Android ScrollView在滚动中调用scrollTo(0,0)回到顶部无效