Android ListView上的焦点问题
我有一个搜索Android ListView上的焦点问题,android,android-layout,android-widget,Android,Android Layout,Android Widget,我有一个搜索活动,其布局文件如下 <?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_pa
活动
,其布局文件如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white2" >
<EditText
android:id="@+id/et_search"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:drawableRight="@drawable/search_20"
android:ems="10"
android:hint="Search by name" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey05"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_name"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="0.25"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>
<TextView
android:id="@+id/tv_sno"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Serial No."
android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>
<TextView
android:id="@+id/tv_type"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="0.25"
android:text="Type"
android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>
</LinearLayout>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:transcriptMode="normal" >
</ListView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_chkin"
android:layout_width="0dip"
android:layout_height="50dip"
android:layout_marginBottom="5dip"
android:layout_marginRight="2dip"
android:layout_marginTop="20dip"
android:layout_weight="1"
android:background="@drawable/custombutton"
android:text="Submit" />
<Button
android:id="@+id/btn_chkout"
android:layout_width="0dip"
android:layout_height="50dip"
android:layout_marginBottom="5dip"
android:layout_marginLeft="2dip"
android:layout_marginTop="20dip"
android:layout_weight="1"
android:background="@drawable/custombutton"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
2.
/@覆盖
公共单击(适配器视图arg0,视图arg1,内部位置,
长id){
lview3.选举(职位);
}
For#1在emulator上运行完全正常,但在我的三星Galaxy Y上,它显示*“应用程序意外停止。请重试”*
对于#2,只要我触摸屏幕上的项目,项目就会高亮显示,只要我移开手指,高亮显示的颜色就会消失
我的listview_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linLayout_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_name"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Medium Text"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_sno"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_weight="0.25"
android:text="Medium Text"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_type"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_weight="0.30"
android:text="Medium Text"
android:textSize="15sp" />
</LinearLayout>
Yipeee。。。
这就解决了问题。以下是我在我的ListAdapter
// set selected item
LinearLayout activeItem = (LinearLayout) convertView;
if (position == SearchAssetActivity.selectedItem){
activeItem.setBackgroundColor(Color.GRAY);
// for focus on it
int top = (activeItem == null) ? 0 : activeItem.getTop();
((ListView) parent).setSelectionFromTop(position, top);
}
else{
activeItem.setBackgroundColor(Color.BLACK);
}
在列表视图中有选择模式。这是在列表视图中进行持久选择的标准代码
// inside onCreate method
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// Make the newly clicked item the currently selected one.
getListView().setItemChecked(position, true);
}
在此之前,需要使用选择器设置列表视图项的布局背景
android:background="@drawable/activated_background"
已激活的\u background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="400">
<item android:drawable="#ff0099cc" android:state_selected="true"/>
<item android:drawable="#ff0099cc" android:state_checked="true"/>
<item android:drawable="#ff0099cc" android:state_activated="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>
android:clickable=“false”android:focusable=“false”将这些属性设置为true。不要在emulator中检查你的应用程序,如果你使用任何一款android手机进行检查,你都可以捕获所有的bug。很抱歉,我还必须在listview中显示每一行的布局文件@Akki:我确实在我的布局文件中将android:clickable=“false”android:focusable=“false”设置为true,我以前也尝试过。在这种情况下,它会突出显示所选项目,但只是在列表中所选行的上方和下方绘制两条水平线。我觉得适配器中用于为列表中的每一行创建视图的TextView
可能正在使用click事件。因此,我已将clickable和focusable设置为false。甚至尝试过设置lview3.setItemsCanFocus(true)代码>在onCreate()
和lview3.setItemChecked中(位置,true)代码>在onItemClick()中
android:background="@drawable/activated_background"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="400">
<item android:drawable="#ff0099cc" android:state_selected="true"/>
<item android:drawable="#ff0099cc" android:state_checked="true"/>
<item android:drawable="#ff0099cc" android:state_activated="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>