Android ListView求解

剑酒酒 2015-08-24 03:27:58
listview的OnItemSelectedListener事件不能触发,但是Click事件可以触发是什么原因。
布局文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/activity_horizontal_margin"
android:orientation="horizontal"
tools:context=".MainActivity">
<ListView
android:id="@+id/mainlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@drawable/selector3">
</ListView>
</LinearLayout>
java源代码
ListView mainlist=(ListView)findViewById(R.id.mainlist);

mainlist.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);

String[] arr1={"第一栏","第二栏","第三栏","第四栏","第五栏"};
ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,R.layout.layout_app_setting_listitem,arr1);
mainlist.setAdapter(adapter1);

mainlist.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
System.out.println(position);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
System.out.println("没有做出选择");
}
});
...全文
274 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Blck宋 2015-09-01
  • 打赏
  • 举报
回复
Onitem方法中应该加一个判断位置的方法 if语句就可以吧
网络咖啡 2015-08-26
  • 打赏
  • 举报
回复
你的ListView的Item是单选或多选类型吗?
_Alien 2015-08-26
  • 打赏
  • 举报
回复
setOnItemClick就好
  • 打赏
  • 举报
回复
因为TextView默认不能选中,也不能longClick,,在layout里面设置可以select试下
風言楓語 2015-08-25
  • 打赏
  • 举报
回复
OnItemSelectedListener不能触发一般是item里面的子控件的焦点问题 在item跟布局加上 android:descendantFocusability="blocksDescendants" 这个属性
wodawoda888 2015-08-25
  • 打赏
  • 举报
回复
OnItemSelectedListener是选择的事件,是通过键盘方向键去选择item的事件,如果你直接用触摸屏的话,一般是出发点击事件的
Arnold9009 2015-08-25
  • 打赏
  • 举报
回复
触摸方式基本不会触发 接usb键盘,按方向键选的时候,焦点移动到哪个item上,就会触发这个item的selected回调
JPF1024 2015-08-25
  • 打赏
  • 举报
回复
那个是选中事件,一般点击很难触发选中事件。 参考: http://www.dewen.io/q/2076/ListView+%E4%B8%AD%E7%9A%84setOnItemSelectedListener(listener)%E4%B8%BA%E4%BB%80%E4%B9%88%E9%80%89%E4%B8%AD%E4%B8%80%E4%B8%AAitem%E5%90%8E%E6%97%A0%E6%B3%95%E5%BE%97%E5%88%B0%E5%93%8D%E5%BA%94%EF%BC%9F
剑酒酒 2015-08-25
  • 打赏
  • 举报
回复
没人能回答吗
sjj370216540 2015-08-25
  • 打赏
  • 举报
回复
厉害。。。。。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧