我在列表視圖中的EditText字段有問題。在我滾動一些設置似乎被重置(selectAllOnFocus)和選擇光標去香蕉。ListView中的EditText滾動後奇怪的選擇行爲
我有一個自定義ArrayAdapter和自定義數據對象的列表視圖。在這種情況下,對象只保存一個字符串(以簡化它)。
我的動態
// 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);
我的適配器
@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) { }
});
}
要解決所有我在其他線程發現我已經設置了focusproblems:
.setItemsCanFocus(true)
在活動XML列表視圖
android:descendantFocusability="beforeDescendants"
android:windowSoftInputMode="adjustPan"
清單中的XML
集中和編輯文本正常工作。當我滾動正確的值舉行,所有這一切似乎工作正常。
在我滾動和我點擊一些EditTexts發生這種情況。 (最後的焦點模糊,點擊一個重點,內容選擇)
後,我向下滾動,再起來,像以前一樣做同樣的點擊,出現這種情況。
http://stackoverflow.com/questions/20406472/edittext-in-listview-loses-focus-when-pressed-on-android-4-x看到這個鏈接 – duggu
根據我的知識在列表視圖中使用編輯文本是一種不好的做法 – duggu
我明白了。因此,使用TextView而不是EditText並將它們替換爲click/blur似乎是最可行的選擇,或者甚至可以重用convertView,但不包括其內的EditText(在getView上重新創建)。我在最後的設置中遇到了更多的怪癖,所以我並不想通過一些不好的做法來破解我的方式。 – user3729954