如何突出显示列表视图中的选定行?

How to highlight selected row in listview?

我想在列表视图中用不同颜色突出显示 selected 行。在这里,我使用 CustomAdapter 来填充列表视图中的行。当我单击任何行时,然后更改 selected 行的背景。如果我 select 另一个突出显示 selected 一个,删除以前突出显示的背景。

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 class

    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>

使用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"/>

您需要有一个状态可绘制对象作为列表选择器。您可以在样式中从 Listview 小部件扩展,也可以自行设置。

默认为:

 <item name="android:listSelector">?attr/listChoiceBackgroundIndicator</item>

在你的例子中你应该这样写:

android:listSelector="?attr/listChoiceBackgroundIndicator"

而不是设置在您的 onitemclicklistener 中选择的视图,您应该设置激活。

view.setActivated(true);

创建 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>

现在将 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"/>