如何设置android中点击列表项的背景色?

如何设置android中点击列表项的背景色?,android,listview,background,selection,Android,Listview,Background,Selection,我已经创建了一个列表视图。当我单击列表中的任何项目时,我想将其设置为背景。当我单击上一个项目的其他项目时,背景将被删除并设置为其他。我如何实现这一点。我试过几种方法。我试过使用选择器和其他方法。 代码片段如下所示:_ <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item androi

我已经创建了一个列表视图。当我单击列表中的任何项目时,我想将其设置为背景。当我单击上一个项目的其他项目时,背景将被删除并设置为其他。我如何实现这一点。我试过几种方法。我试过使用选择器和其他方法。 代码片段如下所示:_

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:state_pressed="true"
    android:drawable="@color/selectedItem" />
<item
    android:state_focused="true"
    android:drawable="@color/GREEN" />
<item
    android:drawable="@color/BLACK" />

您可以这样做:

    OnItemClickListener listViewOnItemClick = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
                // We store the current position of the selected item
                mSelectedItem = position;
                // We refresh a custom adapter
                mAdapter.notifyDataSetChanged();
            }
        }
    };

此ListView使用哪种适配器?我正在使用arrayAdapter.apply click event on item,然后更改背景布局的颜色。是的,我也在尝试这种方法,但如何在选择其他项时删除颜色。请给出一些代码片段。我会使用自定义适配器,但可能有一种方法可以使用侦听器执行某些操作
    OnItemClickListener listViewOnItemClick = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
                // We store the current position of the selected item
                mSelectedItem = position;
                // We refresh a custom adapter
                mAdapter.notifyDataSetChanged();
            }
        }
    };
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final View view = View.inflate(mContext, R.layout.item_list, null);

            if (position == mSelectedItem) {
                //Set active style
            }

            return view;
        }