我想在listview中突出显示具有不同颜色的所选行。在这里,我使用CustomAdapter填充listview中的行。当我单击任何行时,然后更改所选行的背景。如果我选择了另一个突出显示,请删除之前突出显示的背景。
relation_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="fill_parent"
android:layout_height="33dp"
android:text="Done"
android:id="@+id/btnDoneRelationDialog"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/buttonshape"
android:textColor="#FFFFFF"
android:textAllCaps="false"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/popup_lstview_relations"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="@+id/relativeLayout"
android:choiceMode="singleChoice"/>
</RelativeLayout>
</LinearLayout>
AddMemberSearch.java
listViewPopup = (ListView) d.findViewById(R.id.popup_lstview_relations);
RelationAdapter relationAdapter = new RelationAdapter(AddmemberSearch.this, relationList);
listViewPopup.setAdapter(relationAdapter);
listViewPopup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
// listViewPopup.getSelectedView().setBackgroundColor(getResources().getColor(R.color.title_bar_color));
Object o = listViewPopup.getItemAtPosition(position);
relValue = ((ValueNamePairBO) o).getValue();
Log.d("System Out", "relValue: " + relValue);
view.setSelected(true);
autoComplteVerifyUserId.setText("");
}
});
RelationAdapter类
public class RelationAdapter extends BaseAdapter {
private ArrayList<ValueNamePairBO> listData;
private LayoutInflater layoutInflater;
public RelationAdapter(Context aContext, ArrayList<ValueNamePairBO> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.relation_row, null);
holder = new ViewHolder();
holder.txtRelationName = (TextView) convertView.findViewById(R.id.txtRelationName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtRelationName.setText(listData.get(position).getName());
return convertView;
}
static class ViewHolder {
TextView txtRelationName;
}
}
relation_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/txtRelationName"
android:layout_gravity="left"
android:textColor="#000000"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingLeft="20dp"
/>
</LinearLayout>
答案 0 :(得分:1)
使用android:listSelector
并设置您想要突出显示的颜色
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/popup_lstview_relations"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="@+id/relativeLayout"
android:choiceMode="singleChoice"
android:listSelector="@android:color/darker_gray"/>
答案 1 :(得分:1)
创建drawable / listview_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#e4e4e4" />
</shape>
创建drawable / select_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#6f89cff0" />
</shape>
创建drawable / list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/select_bg" android:state_pressed="true"/>
<item android:drawable="@drawable/select_bg" android:state_selected="true"/>
<item android:drawable="@drawable/listview_bg"/>
</selector>
现在在listview上添加listSelector
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/popup_lstview_relations"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="@+id/relativeLayout"
android:choiceMode="singleChoice"
android:listSelector="@drawable/list_selector"/>
答案 2 :(得分:0)
您需要将状态可绘制为列表选择器。您可以从样式中扩展Listview小部件,也可以自己设置。
默认为:
android:listSelector="?attr/listChoiceBackgroundIndicator"
在你的例子中,你应该写:
view.setActivated(true);
而不是在onitemclicklistener中设置选择的视图,而应设置激活。
{{1}}