RecycleView中EditText输入框

在RecycleView列表的Item中若有EditText输入框时,因为ViewHolder复用,所以输入一个Item的数值后,滑动List会在下一个位置也重复出现此数值。

解决方法参考如下:

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

public void setDataList(ArrayList<RiLiaoBean> dataList) {
    this.dataList = dataList;
    notifyDataSetChanged();
}

private ArrayList<RiLiaoBean> dataList;

public HaoLiaoAdapter() {
    dataList = new ArrayList<>();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.haoliao_item_layout, parent, false);
    return new ViewHolder(v, new CustomTextWatcher());
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    RiLiaoBean riLiaoBean = dataList.get(position);
    if (riLiaoBean.getType() == 0) {//未上报
        holder.inHaoLiaoEditText.setVisibility(View.VISIBLE);
        holder.haoliaoTextView.setVisibility(View.GONE);
        holder.customTextWatcher.updatePosition(position);
        if(riLiaoBean.getCfhaoqty()>0) {
            holder.inHaoLiaoEditText.setText(riLiaoBean.getCfhaoqty() + "");
        }else {
            holder.inHaoLiaoEditText.setText(null);
        }
    } else {//已上报
        holder.inHaoLiaoEditText.setVisibility(View.GONE);
        holder.haoliaoTextView.setVisibility(View.VISIBLE);
        holder.haoliaoTextView.setText(riLiaoBean.getCfhaoqty() + "包");
    }
    holder.dateTitleTextView.setText(riLiaoBean.getHaoliaodate() + "(" + riLiaoBean.getRiling() + "日龄)");
    holder.biaozhunTextView.setText("标准日耗:" + riLiaoBean.getBzrlh() + "包");

}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.dateTitleTextView)
    TextView dateTitleTextView;
    @BindView(R.id.biaozhunTextView)
    TextView biaozhunTextView;
    @BindView(R.id.haoliaoTextView)
    TextView haoliaoTextView;
    @BindView(R.id.inHaoLiaoEditText)
    EditText inHaoLiaoEditText;
    CustomTextWatcher customTextWatcher;

    public ViewHolder(View view, CustomTextWatcher customTextWatcher) {
        super(view);
        ButterKnife.bind(this, view);
        this.customTextWatcher = customTextWatcher;
        inHaoLiaoEditText.addTextChangedListener(customTextWatcher);
    }
}

private class CustomTextWatcher implements TextWatcher {
    private int position;

    public void updatePosition(int position) {
        this.position = position;
    }

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

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if(s.toString()!=null&&!s.toString().equals("")) {
            dataList.get(position).setCfhaoqty(Double.parseDouble(s.toString()));
        }
    }
}

}

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 168,026评论 26 707
  • 最近做了一个Android UI相关开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库...
    OpenDigg阅读 16,791评论 6 224
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 5,616评论 0 17
  • 我有故事,你有酒吗我有酒,你有故事吗拿往事下酒,当痛饮此杯,再浪迹天涯,风慕尘马远,你我结缘欢,天地久情长… 天地...
    南溪向南北歌流海阅读 126评论 0 0
  • 时间悄然到夏 夏是插秧的季节 收获麦子的季节 更是果蔬怒放的生命 在农田在山间 一片片赤橙黄绿 姑娘啊 你身穿白纱...
    爱上一叶浮萍阅读 206评论 2 4