0

我创建了一个列表视图。当我单击列表中的任何项目时,我想为其设置背景。当我单击以前删除的其他项目背景并设置为其他项目时。我怎样才能做到这一点。我尝试了几种方式。我尝试过使用选择器和其他方式。代码片段如下:_

<?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" />

和活动代码

bookListView = (ListView) findViewById(R.id.list_books);
/*  selectedView = null;
    bookListView.invalidate();*/
    bookListView.setSelector( R.drawable.list_selector);
4

1 回答 1

0

你可以这样做:

    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();
            }
        }
    };

在自定义适配器中,使用以下命令覆盖 getView 方法:

        @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;
        }
于 2013-06-06T11:24:24.240 回答