定义一个自定义适配器并在其 getView() 方法中:
private int selectedItem;
// is used outside the adapter for example OnItemClickListener
public void setSelectedItem(int position) {
selectedItem = position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//...
// set selected item
LinearLayout activeItem = (LinearLayout) rowView;
if (position == selectedItem)
{
activeItem.setBackgroundResource(R.drawable.background_dark_blue);
}
else
{
activeItem.setBackgroundResource(R.drawable.border02);
}
//...
}
background_dark_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="@color/lightBlue6" />
<!-- border width and color -->
<stroke
android:width="2dp"
android:color="#FF0404B4" />
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp" />
</shape></item>
</layer-list>
边框02.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" android:layout_width="wrap_content">
<stroke android:width="0.5dp" android:color="#FF000000" />
<solid android:color="#FFFFFFFF" />
<padding android:left="0dp" android:top="0dp" android:right="0dp"
android:bottom="0dp" />
<corners android:radius="0dp" />
</shape>
</item>
</layer-list>