0
我想突出显示从listview中选择的项目行。下面是我如何设置我的列表视图:Android ListView突出显示所选行
public void buildCatListView(){
//Code to get data from database
}
// Set the data into listview
catlistview.setAdapter(new ListAdapter(this));
catlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View item,
int position, long id) {
// Get the transaction record ID
categoryID = _catlist.get(position)
.getCategoryID();
for (int j = 0; j < parent.getChildCount(); j++)
parent.getChildAt(j).setBackgroundColor(Color.WHITE);
// Change the background color of the selected element
item.setBackgroundColor(Color.rgb(246, 206, 206));
}
});
mDbHelper.close();
}
而我的列表视图的XML文件:
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
而我的列表视图行的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/txtDisplayCat"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dp" />
<TextView
android:id="@+id/txtDisplayCatDesc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10dp" />
</LinearLayout>
它单击时突出显示选定的行。但它表现得非常怪异。比方说,我选择了第一个项目,它不仅突出显示了第一个项目,而且还显示了最后一个项目。然后当我向下滚动时,突出显示的行有时会转移到第二或第三。我不知道为什么会这样。
只是想知道有没有其他方法可以做到这一点?
在此先感谢。
非常感谢!有用!但是,当我滚动列表视图时,如何高亮显示颜色将保留在列表视图的顶部还是底部? – 2014-08-30 07:23:00