ListView setSelection 无效问题
来源:互联网 发布:网络编程socket 编辑:程序博客网 时间:2024/06/05 00:23
一、api
/** * Sets the currently selected item. If in touch mode, the item will not be selected * but it will still be positioned appropriately. If the specified selection position * is less than 0, then the item at position 0 will be selected. * * @param position Index (starting at 0) of the data item to be selected. * 手动设置列表中被选择的项目。在触摸模式下,定位到被选择的项目(列表太长有滚动条,使视界外项目可见。) */ @Override public void setSelection(int position) { setSelectionFromTop(position, 0); }
二、无效情况
在有其他ui操作或某些操作使ListView发生变化时,setSelection失效。
例如:
上面是个ListView,下面是EditText和Button。点击发送,EditView内容加入到ListVIew中。使用setSelection定位到最后一项。
关键逻辑:
a:取出EditText内容
b:清空EditText
c:添加到ListView
d:调用adapter.notifyDataSetChanged();
e:调用setSelection
EditText内容大于1行时,发现问题。
(1)一开始点击发送后,不清空 EditText。正常。
(2) 清空 EditText。不正常。最后一项没有显示。猜测是清空EditText导致界面发生变化,使setSelection失效。
三、解决方法
(1)
//获得焦点listView.requestFocusFromTouch();listView.setSelection(list.size() - 1);//EditText获得焦点editText.requestFocus();
(2)
//使用消息队列listView.post(new Runnable() { @Override public void run() { listView.setSelection(list.size()-1); }});
(3)
//不使用setSelection而使用另一个方法listView.smoothScrollToPosition(list.size()-1);
smoothScrollToPosition api
/** * Smoothly scroll to the specified adapter position. The view will * scroll such that the indicated position is displayed. * @param position Scroll to this adapter position. */ @android.view.RemotableViewMethod public void smoothScrollToPosition(int position) { super.smoothScrollToPosition(position); }
0 0
- ListView setSelection 无效问题
- listview.setSelection ()无效的问题
- ListView setSelection(0)无效
- listview setselection 无效解决办法
- ListView setSelection() 无效
- ListView setSelection()无效
- 为什么ListView的setSelection无效了?
- ListView的setItemChecked()或者setSelection()无效是怎么回事?
- ListView设置setSelection无效的解决方案
- Android ListView setSelection()无效没作用
- ListView设置选中指定的条目无效,listView.setSelection(position);
- 解决listview setSelection不生效问题
- 解决 listview 人工设置选择值 setSelection(position) 无效
- 关于BUG的那些事儿-ListView的setSelection()无效
- 关于Listview、Girdview的setSelection无效的解决方法与解析
- 解决scrollview嵌套listview后,listview的setSelection失效问题
- ListView的setSelection用法
- ListView的setSelection()方法
- Velocity宏定义的坑与解决办法
- 游戏中的脚本语言原理与发展
- Minimum Path Sum
- Tiny Formater
- go语言defer使用 .
- ListView setSelection 无效问题
- http://blog.csdn.net/lenotang/article/details/2823230
- JSON详解
- Leetcode[66]-Plus One
- Tiny之Web工程构建
- 开源框架Tiny之内容组成
- 第一章,listview的简易用法(Android)
- TinyUI组件开发示例
- 虚函数与多态