为什么在EditText上输入值交换的位置,同时在RecyclerView滚动? [英] Why does the input value in EditText swaps its position while scrolling in a RecyclerView?

查看:540
本文介绍了为什么在EditText上输入值交换的位置,同时在RecyclerView滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

把输入的EditText上,如果向上或向下滚动之后非常快的输入值交换其在其他位置的EditText在RecyclerView。结果
滚动前的数据是在第一个的EditText。结果
滚动后和向下的第一的EditText的值而改变其现在的位置到第4一个和交换是随机的。是否有任何变通以稳定的数据。

下面是一个简单的截图

下面是模型类:

 公共类产品{
    公众诠释PID;
    公共字符串PNAME;
    公共双UNIT_PRICE;
    公共双重优惠;
}

下面是适配器类:

 公共类ProductAdapter扩展RecyclerView.Adapter< ProductAdapter.ProductListHolder> {
上下文语境;
清单<产品与GT; productList的;公共ProductAdapter(上下文C,列表与LT;产品> LP){
    this.context = C;
    this.productList = LP;
}公共类ProductListHolder扩展RecyclerView.ViewHolder {
    TextView的tvName;
    TextView的tvPrice;
    TextView的tvDiscount;
    TextView的tvTotal;
    的EditText etQuantity;
    公共ProductListHolder(查看ItemView控件){
        超(ItemView控件);
        tvName =(TextView中)itemView.findViewById(R.id.tvName);
        tvPrice =(TextView中)itemView.findViewById(R.id.tvPrice);
        tvDiscount =(TextView中)itemView.findViewById(R.id.tvDiscount);
        tvTotal =(TextView中)itemView.findViewById(R.id.tvTotal);
        etQuantity =(EditText上)itemView.findViewById(R.id.etQuantity);
    }
}@覆盖
公共ProductListHolder onCreateViewHolder(ViewGroup中ViewGroup中,int i)以{
    视图V = LayoutInflater.from(viewGroup.getContext())膨胀(R.layout.single_row,ViewGroup中,假的)。
    ProductListHolder ph值=新ProductListHolder(ⅴ);
    返回pH值;
}@覆盖
公共无效onBindViewHolder(最终ProductListHolder productListHolder,最终int i)以{
    productListHolder.tvName.setText(productList.get㈠.pName);
    productListHolder.tvPrice.setText(将String.valueOf(productList.get㈠.unit_price));
    productListHolder.tvDiscount.setText(将String.valueOf(productList.get㈠.discount));    productListHolder.etQuantity.addTextChangedListener(新TextWatcher(){
        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){        }        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
            如果(!s.toString()。等于()){
                双totalPrice =(productList.get㈠.unit_price-productList.get(ⅰ).discount)*(Double.valueOf(s.toString()));
                productListHolder.tvTotal.setText(将String.valueOf(totalPrice));
            }
        }        @覆盖
        公共无效afterTextChanged(编辑S){        }
    });
}@覆盖
公众诠释getItemCount(){
    返回productList.size();
}

}

下面是在MainActivity:

 公共类MainActivity扩展AppCompatActivity {清单<产品与GT; productList的;
RecyclerView recyclerView;
RecyclerView.Adapter mAdapter;的String [] =名称{A,B,C,D};
双[] =价格1000 {2000,3000,100};
双[]打折= {10,20,30,2};@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    initRecyclerView(); //初始化回收站查看
    新MyTask()执行();
}公共无效initRecyclerView(){
    recyclerView =(RecyclerView)findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(真);
    recyclerView.setLayoutManager(新LinearLayoutManager(MainActivity.this));
}私有类MyTask扩展的AsyncTask<太虚,太虚,列表与LT;产品与GT;> {    @覆盖
    保护列表与LT;产品与GT; doInBackground(虚空...... PARAMS){
        INT SZ = 24;
        productList的=新的ArrayList<产品及GT;();
        的for(int i = 0; I< SZ;我++){
            产品p =新产品();
            p.pId = I%4;
            p.pName =名称[I%4]。
            p.unit_price价格= [I%4]。
            p.discount =折扣[I%4]。
            productList.add(P);
        }        返回productList的;
    }    @覆盖
    保护无效onPostExecute(列表<产品与GT;产品){
        super.onPostExecute(产品);
        mAdapter =新ProductAdapter(MainActivity.this,productList的);
        recyclerView.setAdapter(mAdapter);
    }
  }
}


解决方案

我已经整理出了问题。

问题是,我添加了 addTextChangedListener onBindViewHolder 。所以在EditText上的值总是得到混乱,因为当把重心转移的另一个的 addTextChangedListener 的登记。因此,我所做的是,我实现了另外一个自定义类名为 CustomEtListener 并实施 TextWatcher 。所以,现在我注册的每个的EditText中的 onCreateViewHolder 自定义监听器。并采取字符串的阵列以保存编辑文本的值。和该阵列的大小是在RecyclerView的项数。和方法的 updatePosition 在那里得到重点项目的位置。然后点击 onTextChanged 我只是存放在数组中的EDITTEXT的价值,并在 onBindViewHolder 更新的EditText上的价值。之后我每次滚动的时候,EditText上的价值不会改变。

下面是我的适配器类。

 公共类ProductAdapter扩展RecyclerView.Adapter< ProductAdapter.ProductListHolder> {
上下文语境;
清单<产品与GT; productList的;
的String [] etValArr;
双[]总价值;公共ProductAdapter(上下文C,列表与LT;产品> LP){
    this.context = C;
    this.productList = LP;
    //创建其尺寸编辑文本的数量相匹配的新阵
    etValArr =新的String [productList.size()];
    //以及用于保持每个编辑文本的所有值的数组
    总价值=新的双[productList.size()];
}公共类ProductListHolder扩展RecyclerView.ViewHolder {
    TextView的tvName;
    TextView的tvPrice;
    TextView的tvDiscount;
    TextView的tvTotal;
    的EditText etQuantity;    //自定义编辑文本监听器实例
    公共CustomEtListener myCustomEditTextListener;    公共ProductListHolder(查看ItemView控件,CustomEtListener myLis){
        超(ItemView控件);
        tvName =(TextView中)itemView.findViewById(R.id.tvName);
        tvPrice =(TextView中)itemView.findViewById(R.id.tvPrice);
        tvDiscount =(TextView中)itemView.findViewById(R.id.tvDiscount);
        tvTotal =(TextView中)itemView.findViewById(R.id.tvTotal);
        etQuantity =(EditText上)itemView.findViewById(R.id.etQuantity);
        //分配addTextChangedListener的新实例的每个项目
        myCustomEditTextListener = myLis;
        etQuantity.addTextChangedListener(myCustomEditTextListener);
    }
}@覆盖
公共ProductListHolder onCreateViewHolder(ViewGroup中ViewGroup中,最终int i)以{
    视图V = LayoutInflater.from(viewGroup.getContext())膨胀(R.layout.single_row,ViewGroup中,假的)。
    ProductListHolder ph值=新ProductListHolder(ⅴ,新CustomEtListener());
    返回pH值;
}@覆盖
公共无效onBindViewHolder(最终ProductListHolder productListHolder,int i)以{
    productListHolder.tvName.setText(productList.get㈠.pName);
    productListHolder.tvPrice.setText(将String.valueOf(productList.get㈠.unit_price));
    productListHolder.tvDiscount.setText(将String.valueOf(productList.get㈠.discount));    //更新位置时,焦点变化
    productListHolder.myCustomEditTextListener.updatePosition(ⅰ);
    //设置相应值
    productListHolder.etQuantity.setText(etValArr [I]);
    productListHolder.tvTotal.setText(将String.valueOf(总价值由[i]));}@覆盖
公众诠释getItemCount(){
    返回productList.size();
}/ **
 它实现文本守望*自定义类
 * /
私有类CustomEtListener实现TextWatcher {
    私人诠释的位置;    / **
     *根据onBindViewHolder更新的位置
     *
     * @参数位置 - 聚焦项目的位置
     * /
    公共无效updatePosition(INT位置){
        this.position =位置;
    }    @覆盖
    公共无效beforeTextChanged(CharSequence中的CharSequence,诠释我,诠释I2,I3 INT){
    }    @覆盖
    公共无效onTextChanged(CharSequence中的CharSequence,诠释我,诠释I2,I3 INT){
        //根据位置改变数组的值
        etValArr [位置] = charSequence.toString();
        //做计算
        如果(!etValArr [位置] .equals()){
            总价值[位置] =(productList.get(位置).unit_price - productList.get(位置).discount)*(Double.valueOf(etValArr [位置]));        }其他{
            总价值[位置] = 0.00;
        }
    }    @覆盖
    公共无效afterTextChanged(编辑编辑){
    }
}

}

不过我有一个问题。我无法更新共有列的值。之后,我向上和向下滚动,则该值改变。所以,我不得不实施一个电话backListener得到总的列的值的时候了。我现在有一个接口的 TextCallBackListener ,然后执行它的适配器ProductListHoldersClass。因此,只要在 onTextChanged 被称为它触发了我的 UPDATETEXT 方式来改变行的总价值。如果你不明白的观点,存在问题的屏幕截图。

下面是接口。

 公共接口TextCallBackListener {
      公共无效UPDATETEXT(字符串VAL);

}

下面是我修改过的适配器类。

 公共类ProductAdapter扩展RecyclerView.Adapter< ProductAdapter.ProductListHolder> {
上下文语境;
清单<产品与GT; productList的;
的String [] etValArr;
双[]总价值;公共ProductAdapter(上下文C,列表与LT;产品> LP){
    this.context = C;
    this.productList = LP;
    //创建其尺寸编辑文本的数量相匹配的新阵
    etValArr =新的String [productList.size()];
    //以及用于保持每个编辑文本的所有值的数组
    总价值=新的双[productList.size()];
}
公共类ProductListHolder扩展RecyclerView.ViewHolder实现TextCallBackListener {
    TextView的tvName;
    TextView的tvPrice;
    TextView的tvDiscount;
    TextView的tvTotal;
    的EditText etQuantity;    //自定义编辑文本监听器实例
    公共CustomEtListener myCustomEditTextListener;    公共ProductListHolder(查看ItemView控件,CustomEtListener myLis){
        超(ItemView控件);
        tvName =(TextView中)itemView.findViewById(R.id.tvName);
        tvPrice =(TextView中)itemView.findViewById(R.id.tvPrice);
        tvDiscount =(TextView中)itemView.findViewById(R.id.tvDiscount);
        tvTotal =(TextView中)itemView.findViewById(R.id.tvTotal);
        etQuantity =(EditText上)itemView.findViewById(R.id.etQuantity);
        //分配addTextChangedListener的新实例的每个项目
        myCustomEditTextListener = myLis;
        etQuantity.addTextChangedListener(myCustomEditTextListener);
    }    @覆盖
    公共无效UPDATETEXT(字符串VAL){
        tvTotal.setText(VAL);
    }
}
@覆盖
公共ProductListHolder onCreateViewHolder(ViewGroup中ViewGroup中,最终int i)以{
    视图V = LayoutInflater.from(viewGroup.getContext())膨胀(R.layout.single_row,ViewGroup中,假的)。
    ProductListHolder ph值=新ProductListHolder(ⅴ,新CustomEtListener());
    返回pH值;
}@覆盖
公共无效onBindViewHolder(最终ProductListHolder productListHolder,int i)以{
    productListHolder.tvName.setText(productList.get㈠.pName);
    productListHolder.tvPrice.setText(将String.valueOf(productList.get㈠.unit_price));
    productListHolder.tvDiscount.setText(将String.valueOf(productList.get㈠.discount));    //更新位置时,焦点变化
    productListHolder.myCustomEditTextListener.updatePosition(ⅰ,productListHolder);
    //设置相应值
    productListHolder.etQuantity.setText(etValArr [I]);
    //productListHolder.tvTotal.setText(String.valueOf(totalValue[i]));}
@覆盖
公众诠释getItemCount(){
    返回productList.size();
}/ **
 它实现文本守望*自定义类
 * /
私有类CustomEtListener实现TextWatcher {
    私人诠释的位置;
    TextCallBackListener textCallBackListener;    / **
     *根据onBindViewHolder更新的位置
     *
     * @参数位置 - 聚焦项目的位置
     * @参数PA - ProductListHolder类对象
     * /
    公共无效updatePosition(INT位置,ProductListHolder PA){
        this.position =位置;
        //它分配给CallBackListener实例
        textCallBackListener =(TextCallBackListener)PA;
    }    @覆盖
    公共无效beforeTextChanged(CharSequence中的CharSequence,诠释我,诠释I2,I3 INT){
    }    @覆盖
    公共无效onTextChanged(CharSequence中的CharSequence,诠释我,诠释I2,I3 INT){
        //根据位置改变数组的值
        etValArr [位置] = charSequence.toString();
        //做计算
        如果(!etValArr [位置] .equals()){
            总价值[位置] =(productList.get(位置).unit_price - productList.get(位置).discount)*(Double.valueOf(etValArr [位置]));
            //触发回调函数来更新总
            textCallBackListener.updateText(将String.valueOf(总价值[位置]));
        }其他{
            总价值[位置] = 0.00;
            textCallBackListener.updateText(0.00);
        }
    }    @覆盖
    公共无效afterTextChanged(编辑编辑){    }}}

After putting an input in the EditText, if a scroll up or down very fast the input values swaps its position in another EditText in a RecyclerView.
Before scrolling the data was in the first EditText.
After scrolling up and down the value of the first EditText changed its postion to the 4th one and the swapping is random. Is there any work around to steady the data.

Here is a sample screenshot

Here is the Model Class:

public class Product {    
    public int pId;    
    public String pName;    
    public double unit_price;    
    public double discount;    
}    

Here is the adapter Class:

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductListHolder> {
Context context;
List<Product> productList;

public ProductAdapter(Context c, List<Product> lp){
    this.context = c;
    this.productList = lp;
}

public class ProductListHolder extends RecyclerView.ViewHolder{
    TextView tvName;
    TextView tvPrice;
    TextView tvDiscount;
    TextView tvTotal;
    EditText etQuantity;
    public ProductListHolder(View itemView) {
        super(itemView);
        tvName = (TextView) itemView.findViewById(R.id.tvName);
        tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
        tvDiscount = (TextView) itemView.findViewById(R.id.tvDiscount);
        tvTotal = (TextView) itemView.findViewById(R.id.tvTotal);
        etQuantity = (EditText) itemView.findViewById(R.id.etQuantity);
    }
}

@Override
public ProductListHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_row, viewGroup, false);
    ProductListHolder ph = new ProductListHolder(v);
    return  ph;
}

@Override
public void onBindViewHolder(final ProductListHolder productListHolder, final int i) {
    productListHolder.tvName.setText(productList.get(i).pName);
    productListHolder.tvPrice.setText(String.valueOf(productList.get(i).unit_price));
    productListHolder.tvDiscount.setText(String.valueOf(productList.get(i).discount));

    productListHolder.etQuantity.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!s.toString().equals("")){
                double totalPrice = (productList.get(i).unit_price-productList.get(i).discount)* (Double.valueOf(s.toString()));
                productListHolder.tvTotal.setText(String.valueOf(totalPrice));
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

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

}

Here is the MainActivity:

public class MainActivity extends AppCompatActivity {

List<Product> productList;
RecyclerView recyclerView;
RecyclerView.Adapter mAdapter;

String[] names = {"A", "B", "C","D"};
double[] prices = {1000, 2000, 3000, 100};
double[] discounts = {10, 20, 30, 2};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initRecyclerView(); // Initializing Recycler View
    new MyTask().execute();
}

public void initRecyclerView(){
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
}

private class MyTask extends AsyncTask<Void, Void, List<Product>> {

    @Override
    protected List<Product> doInBackground(Void... params) {
        int sz = 24;
        productList = new ArrayList<Product>();
        for(int i=0; i<sz; i++){
            Product p = new Product();
            p.pId = i%4;
            p.pName = names[i%4];
            p.unit_price = prices[i%4];
            p.discount = discounts[i%4];
            productList.add(p);
        }

        return productList;
    }

    @Override
    protected void onPostExecute(List<Product> products) {
        super.onPostExecute(products);
        mAdapter = new ProductAdapter(MainActivity.this, productList);
        recyclerView.setAdapter(mAdapter);
    }
  }    
}

解决方案

I've sorted out the problem.

The problem was that i added the addTextChangedListener in onBindViewHolder. So the values in the EditText always got jumbled, cause when the focus shifted another addTextChangedListener was registered. So what i did is that, i implemented another Custom Class called CustomEtListener and implemented the TextWatcher. So now i registered the custom listener in onCreateViewHolder for each EditText. And took an array of string to store the values of the edit text. And the size of the array was the number of items in the RecyclerView. And the method updatePosition was there to get the position of the focused item. Then onTextChanged i just stored the value of the editText in the array, and updated the value of that EditText in onBindViewHolder. After that every time i scroll, the value of the EditText won't change.

Here is my adapter class.

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductListHolder> {
Context context;
List<Product> productList;
String[] etValArr;
double[] totalValue;

public ProductAdapter(Context c, List<Product> lp) {
    this.context = c;
    this.productList = lp;
    // Create a new array which size matches the number of Edit Texts
    etValArr = new String[productList.size()];
    // and an array for holding all the values of each edit text
    totalValue = new double[productList.size()];
}

public class ProductListHolder extends RecyclerView.ViewHolder {
    TextView tvName;
    TextView tvPrice;
    TextView tvDiscount;
    TextView tvTotal;
    EditText etQuantity;

    // Instance of a Custom edit text listener
    public CustomEtListener myCustomEditTextListener;

    public ProductListHolder(View itemView, CustomEtListener myLis) {
        super(itemView);
        tvName = (TextView) itemView.findViewById(R.id.tvName);
        tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
        tvDiscount = (TextView) itemView.findViewById(R.id.tvDiscount);
        tvTotal = (TextView) itemView.findViewById(R.id.tvTotal);
        etQuantity = (EditText) itemView.findViewById(R.id.etQuantity);
        // assign a new instance of addTextChangedListener for each item
        myCustomEditTextListener = myLis;
        etQuantity.addTextChangedListener(myCustomEditTextListener);
    }
}

@Override
public ProductListHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_row, viewGroup, false);
    ProductListHolder ph = new ProductListHolder(v, new CustomEtListener());
    return ph;
}

@Override
public void onBindViewHolder(final ProductListHolder productListHolder, int i) {
    productListHolder.tvName.setText(productList.get(i).pName);
    productListHolder.tvPrice.setText(String.valueOf(productList.get(i).unit_price));
    productListHolder.tvDiscount.setText(String.valueOf(productList.get(i).discount));

    // Update the position when focus changes
    productListHolder.myCustomEditTextListener.updatePosition(i);
    // Setting the values accordingly
    productListHolder.etQuantity.setText(etValArr[i]);
    productListHolder.tvTotal.setText(String.valueOf(totalValue[i]));

}

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

/**
 * Custom class which implements Text Watcher
 */
private class CustomEtListener implements TextWatcher {
    private int position;

    /**
     * Updates the position according to onBindViewHolder
     *
     * @param position - position of the focused item
     */
    public void updatePosition(int position) {
        this.position = position;
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        // Change the value of array according to the position
        etValArr[position] = charSequence.toString();
        // Doing the calculation
        if (!etValArr[position].equals("")) {
            totalValue[position] = (productList.get(position).unit_price - productList.get(position).discount) * (Double.valueOf(etValArr[position]));

        } else {
            totalValue[position] = 0.00;
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {
    }
}

}

Still i had a problem. I wasn't able to update the Total column's values. After i scroll up and down, then the value changed. So i had to implement a call backListener to get the value of the Total column right away. I have now an interface TextCallBackListener, and implemented it on the Adapters ProductListHoldersClass. So whenever the onTextChanged was called it triggered my updateText method to change the value of that rows Total. if you don't understand the view, there is a screenshot in the question.

Here is the interface.

public interface TextCallBackListener {
      public void updateText(String val);

}

Here is my changed adapter class.

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductListHolder> {
Context context;
List<Product> productList;
String[] etValArr;
double[] totalValue;

public ProductAdapter(Context c, List<Product> lp) {
    this.context = c;
    this.productList = lp;
    // Create a new array which size matches the number of Edit Texts
    etValArr = new String[productList.size()];
    // and an array for holding all the values of each edit text
    totalValue = new double[productList.size()];
}


public class ProductListHolder extends RecyclerView.ViewHolder implements TextCallBackListener {
    TextView tvName;
    TextView tvPrice;
    TextView tvDiscount;
    TextView tvTotal;
    EditText etQuantity;

    // Instance of a Custom edit text listener
    public CustomEtListener myCustomEditTextListener;

    public ProductListHolder(View itemView, CustomEtListener myLis) {
        super(itemView);
        tvName = (TextView) itemView.findViewById(R.id.tvName);
        tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
        tvDiscount = (TextView) itemView.findViewById(R.id.tvDiscount);
        tvTotal = (TextView) itemView.findViewById(R.id.tvTotal);
        etQuantity = (EditText) itemView.findViewById(R.id.etQuantity);
        // assign a new instance of addTextChangedListener for each item
        myCustomEditTextListener = myLis;
        etQuantity.addTextChangedListener(myCustomEditTextListener);
    }

    @Override
    public void updateText(String val) {
        tvTotal.setText(val);
    }
}


@Override
public ProductListHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.single_row, viewGroup, false);
    ProductListHolder ph = new ProductListHolder(v, new CustomEtListener());
    return ph;
}

@Override
public void onBindViewHolder(final ProductListHolder productListHolder, int i) {
    productListHolder.tvName.setText(productList.get(i).pName);
    productListHolder.tvPrice.setText(String.valueOf(productList.get(i).unit_price));
    productListHolder.tvDiscount.setText(String.valueOf(productList.get(i).discount));

    // Update the position when focus changes
    productListHolder.myCustomEditTextListener.updatePosition(i, productListHolder);
    // Setting the values accordingly
    productListHolder.etQuantity.setText(etValArr[i]);
    //productListHolder.tvTotal.setText(String.valueOf(totalValue[i]));

}


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

/**
 * Custom class which implements Text Watcher
 */
private class CustomEtListener implements TextWatcher {
    private int position;
    TextCallBackListener textCallBackListener;

    /**
     * Updates the position according to onBindViewHolder
     *
     * @param position - position of the focused item
     * @param pa       - object of ProductListHolder Class
     */
    public void updatePosition(int position, ProductListHolder pa) {
        this.position = position;
        // assigning it to the instance of the CallBackListener
        textCallBackListener = (TextCallBackListener) pa;
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        // Change the value of array according to the position
        etValArr[position] = charSequence.toString();
        // Doing the calculation
        if (!etValArr[position].equals("")) {
            totalValue[position] = (productList.get(position).unit_price - productList.get(position).discount) * (Double.valueOf(etValArr[position]));
            // triggering the callback function to update the total
            textCallBackListener.updateText(String.valueOf(totalValue[position]));
        } else {
            totalValue[position] = 0.00;
            textCallBackListener.updateText("0.00");
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

}}

这篇关于为什么在EditText上输入值交换的位置,同时在RecyclerView滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆