将项目加载到ListView后如何突出显示Android ListView第一项?
1.这是我的列表视图
1.This is my listview
<ListView
android:id="@+id/lv_FromTable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:listSelector="@drawable/list_selector"
android:orientation="vertical" />
2.in在onCreate的主要活动中,我加载了项目并将适配器设置为listview
2.in onCreate of main activity i load item and set adapter to listview
lv_FromTable = (ListView)rootView.findViewById(R.id.lv_FromTable);
FromTableAdapter = new ManageTableAdapter(context, FromTableList);
lv_FromTable.setAdapter(FromTableAdapter);
FromTableAdapter.notifyDataSetChanged();
3.i想突出显示第一个项目,同时单击列表视图项目
3.i want to highlight on first item same click on listview item
4.i已经尝试了以下代码
4.i have already tried below code
lv_FromTable.performItemClick(lv_FromTable.getAdapter().getView(0, null, null), 0, lv_FromTable.getAdapter().getItemId(0));
这是引发onitemclick事件,但未突出显示第一项.
it's raise onitemclick event but not highlight on first item.
- 在将项目加载到listview后如何突出显示android listview的第一项?
FromTableAdapter.setSelectedPosition(0);
FromTableAdapter.notifyDataSetChanged();
将getter和setter放在适配器中具有选定位置的位置,并使用该变量并在适配器的getView中突出显示选定的行.
Have getter and setter in adapter with selected position and use the variable and highlight the selected row in getView of adapter.
或
您正在执行单击右键.
在onItemClick
,view.setSelected(true);
中,但是您的可绘制xml应该具有类似的内容
You're performing click right.
In onItemClick
, view.setSelected(true);
but your drawable xml should have something like
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/pressed_color"/>
<item
android:drawable="@color/default_color" />
</selector>
或
lv_FromTable.setItemChecked(0,true);
编辑:不要忘记,没有上面的listSelector
不会起作用
Edit : Don't forget, without the listSelector
above wont work
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="@drawable/list_selector_background" />