简体   繁体   English

滚动后Listview中的EditText奇怪的选择行为

[英]EditText in Listview Strange selection behavior after scroll

I have a problem with EditText-fields in a listview. 我在列表视图中的EditText字段有问题。 After i scroll some settings seem to be reset (selectAllOnFocus) and the selection cursor goes bananas. 我滚动后,似乎重置了某些设置(selectAllOnFocus),并且选择光标变成了香蕉。

I have a listview with a custom ArrayAdapter and a custom dataobject. 我有一个带有自定义ArrayAdapter和自定义dataobject的listview。 In this case the object only holds one String (to simplify it). 在这种情况下,对象仅包含一个String(为简化起见)。

My Activity 我的活动

    // adapter + content
    List<ListviewObject> listViewContent = new ArrayList<ListviewObject>();
    for(int i = 0; i < 50; i++) {
        listViewContent.add(new ListviewObject("num: " + i));
    }       
    adapter = new CustomListAdapter(AddNewPerson.this, R.layout.list_item, listViewContent);

    // list
    ListView mListView = (ListView)findViewById(R.id.sample_list);
    mListView.setItemsCanFocus(true);
    mListView.setAdapter(adapter);

My Adapter 我的适配器

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    HolderObject holder = null;

    if(convertView == null) {
        convertView = mLayoutInflater.inflate(layoutResourceId, parent, false);
        holder = new HolderObject();
        holder.name = (TextView) convertView.findViewById(R.id.txt);
        convertView.setTag(holder);
    } else {
        holder = (HolderObject) convertView.getTag();
    }

    holder.lvObject = items.get(position);

    setNameTextChangeListener(holder);
// Retrieve the correct value 
    holder.name.setText(holder.lvObject.getName());

    return convertView;
}

public static class HolderObject {
    ListviewObject lvObject;
    TextView name;
}

private void setNameTextChangeListener(final HolderObject holder) {
    holder.name.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
// Update the value
            holder.lvObject.setName(s.toString());
        }

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

        @Override
        public void afterTextChanged(Editable s) { }
    });
}

To fix all the focusproblems I found in other threads I've set: 为了解决我在其他线程中发现的所有焦点问题:
.setItemsCanFocus(true) on the listview .setItemsCanFocus(true)在列表视图上
android:descendantFocusability="beforeDescendants" in the activity XML 活动XML中的android:descendantFocusability="beforeDescendants"
android:windowSoftInputMode="adjustPan" in the manifest XML 清单XML中的android:windowSoftInputMode="adjustPan"

Focussing and editing text works fine. 聚焦和编辑文本效果很好。 When I scroll the correct values are held and all this seems to work fine. 当我滚动时,将保留正确的值,所有这些似乎都可以正常工作。

Before I scroll and I click on some of the EditTexts this happens. 在滚动并单击某些EditText之前,会发生这种情况。 (Last focused blurs, clicked one focuses, content is selected) (最后聚焦模糊,单击一个聚焦,选择内容)

http://imgur.com/eeIKhCv http://imgur.com/eeIKhCv

After I scroll down and up again, and do the same clicks as before, this happens. 当我再次上下滚动并执行与以前相同的单击后,就会发生这种情况。

http://imgur.com/75mjPc3 http://imgur.com/75mjPc3

This is due to listView recycling mechanism. 这是由于listView回收机制。 To know more about listview recycling mechanism you can refer this link 要了解有关Listview回收机制的更多信息,请参考此链接

in your case you avoid the problem by storing a last focused editText and In getView set focus to only last stored integer and skip other position. 在您的情况下,可以通过存储最后一个焦点的editText来避免该问题,并在getView中将焦点设置为仅最后存储的整数,并跳过其他位置。 hope this help you... 希望这对您有帮助...

It's quite easy: 这很容易:

  1. Declare a String[] to keep track each EditText 's input inside the afterTextChanged() of "addTextChangedListener() . 声明一个String[]以跟踪"addTextChangedListener()afterTextChanged()内部的每个EditText的输入。

  2. Becareful of the order: 注意顺序:

     viewHolder.editText.removeTextChangedListener(viewHolder.textWatcher); viewHolder.textWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { mInputs[position] = editable.toString(); //record input viewHolder.editText.setSelection(viewHolder.editText.getText().length()); //set cursor to the end } }; viewHolder.editText.addTextChangedListener(viewHolder.textWatcher); viewHolder.editText.setText(mInputs[position]); 
  3. Add android:windowSoftInputMode="adjustPan" to your Activity in AndroidManifest file. android:windowSoftInputMode="adjustPan"到AndroidManifest文件中的Activity中。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM