Android RecyclerView 中的 EditText 重排问题
创始人
2024-08-15 06:00:38
0

在Android RecyclerView中,当包含EditText的列表项(Item)被重用时,会出现EditText内容错乱或乱序的问题。这是因为RecyclerView的重用机制会导致EditText的内容丢失或乱序。

为了解决这个问题,可以在RecyclerView的Adapter中使用以下方法:

  1. 创建一个HashMap来保存EditText的内容和位置信息。
private HashMap editTextValues = new HashMap<>();
  1. 在RecyclerView的Adapter中,在绑定ViewHolder时,为EditText设置文本,并保存其位置信息。
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // 获取当前位置上的EditText的内容
    String text = editTextValues.get(position);

    // 设置EditText的文本
    holder.editText.setText(text);
    
    // 设置EditText文本变化监听器
    holder.editText.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) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            // 保存EditText的文本和位置信息
            editTextValues.put(position, s.toString());
        }
    });
}
  1. 在RecyclerView的Adapter中,重写getItemId方法并返回具体的position,以确保RecyclerView正确保存文本的位置信息。
@Override
public long getItemId(int position) {
    return position;
}

这样,每次RecyclerView的列表项被重用时,会根据位置信息正确地设置EditText的文本,并在文本变化时保存其内容。这样就能解决Android RecyclerView中的EditText重排问题。

相关内容

热门资讯

透视资料!wejoker内置辅... 透视资料!wejoker内置辅助,wepoker究竟有没有透视(透视)确实有挂(哔哩哔哩)1、wep...
透视策略!aapoker免费透... 透视策略!aapoker免费透视脚本,We poker辅助器下载(透视)总是真的是有挂(哔哩哔哩)1...
透视项目!aapoker透视怎... 透视项目!aapoker透视怎么用,wepoker透视是真的吗(透视)真是存在有挂(哔哩哔哩)1、w...
透视窍要!wepoker有辅助... 您好,wepoker私人局有透视吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【4852750...
透视操作!pokerworld... 透视操作!pokerworld辅助器,pokemmo手机版脚本(透视)果然是真的透视技巧(哔哩哔哩)...
透视手段!pokemmo手机版... 透视手段!pokemmo手机版修改器,菠萝辅助器免费版的特点(透视)都是有挂(哔哩哔哩)1、进入到菠...
透视方针!aapoker透视怎... 透视方针!aapoker透视怎么用,aapoker脚本(透视)一直有透视技巧(哔哩哔哩)1、在aap...
透视方式!hhpoker有没有... 透视方式!hhpoker有没有外开挂,hhpoker作必弊码怎么用(透视)一直真的有透视脚本(哔哩哔...
透视要领!德扑HHpoker有... 透视要领!德扑HHpoker有挂吗,hhpoker有后台操控吗(透视)真是真的是有透视教程(哔哩哔哩...
透视演示!pokermaste... 透视演示!pokermaster修改器,hhpoker作必弊码(透视)其实是真的挂(哔哩哔哩)1、这...