【问题标题】:Edittext value disappear when scrolling recyclerview in Android在Android中滚动recyclerview时Edittext值消失
【发布时间】:2018-03-16 17:20:27
【问题描述】:

我调用了recyclerview,其中我的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);
        }
    }

【问题讨论】:

标签: android android-recyclerview android-edittext position scrollview


【解决方案1】:

对于那些仍在寻找解决方案的人,这里是他们的解决方案

步骤:

  1. 创建一个类似这样的哈希图 - HashMapeditedMapList = new HashMap();

  2. 在基于textchanged的onBind方法中,根据位置在hashmap中添加改变的项

  3. 当向下滚动行将重新创建时,您在适配器的 onBind 方法中再次设置了类似的值。 //用于回收物品更新 if (editedMapList.containsKey(position)) { ((NetworkItemViewHolder) 持有者).etUsername.setText(editedMapList.get(position).getUsername()); }

注意:这里的逻辑是当你向下滚动行时将重新创建这意味着 onBind 方法将被调用,所以你要做的就是根据编辑后的 ​​hashmap 中的位置更新行 edittext。

希望对您有所帮助! 快乐编码...

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      问题:如果您的 edittext 值在滚动回收站视图时为 0

      解决方案:然后在 textwatcher 方法 afterTextchange() 中使用下面的代码来编辑文本

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-14
        • 1970-01-01
        • 1970-01-01
        • 2017-04-09
        • 1970-01-01
        相关资源
        最近更新 更多