我想点击它时突出显示列表项。我写了下面的代码,但它不起作用。我希望点击列表视图中的特定项目。
MainActivity.java
ListView lv = getListView();
lv.setSelector( R.drawable.list_selector);
// on selecting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String FC_DATE = ((TextView) view.findViewById(R.id.fc_date)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
ForecastActivity.class);
// sending lat/long to next activity
in.putExtra(TAG_FC_DATE, FC_DATE);
in.putExtra(TAG_LAT, LAT);
in.putExtra(TAG_LONG, LONGITUDE);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true"
android:drawable="@drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/gradient_bg_hover" />
</selector>
答案 0 :(得分:2)
listitem_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="@drawable/gradient_bg_hover" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="@drawable/gradient_bg_hover" />
<item android:state_enabled="true"
android:state_selected="true" android:drawable="@drawable/gradient_bg_hover" />
<item
android:drawable="@drawable/gradient_bg" />
</selector>
将列表项选择器设置为ListView:
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/listitem_selector" />