在ListView中突出显示项目

时间:2011-11-24 22:16:15

标签: android android-listview

好吧,所以我知道这个问题之前已在本网站上提出过,但坦率地说,我没有从我见过的解决方案中得到任何结果。例如,这篇文章:highlighting the selected item in the listview in android表明你需要做的就是将一个choiceMode设置为ListView。我试过这个,它根本不起作用。

在我的代码中,我有一个ListView(onListItemClick)的监听器,每次触发时,我都有这样的东西...... getListView().setItemChecked(position, true),最重要的是我有一个XML选择器,我有附在我的ListView ...

<ListView                       
  android:id="@id/android:list"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:scrollbars="none"
  android:layout_weight="1.5"
  android:choiceMode="singleChoice"
  android:listSelector="@drawable/bounty_list_selector"/>

我在这里遗漏了什么吗? setItemChecked()对ListView的选择器没有影响吗?

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,并通过代码解决了这个问题。它可能不是最明亮也不是最有效的解决方案,但可以解决ListView绘图问题,到目前为止似乎运行良好。

活动文件:

private ListView listView;
private View selectedView;
private int selectedPosition;
private MyAdapter arrayAdapter;

...
// set a state machine for when a list item is clicked
    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (selectedPosition == -1) {
                // no item is selected, mark as selected
                selectedView = view;
                view.setBackgroundResource(R.drawable.bg_selected);
                selectedPosition = position;
            } else if (selectedPosition == position) {
                // the same item is selected as the previous one, deselect
                selectedView = null;
                view.setBackgroundResource(R.drawable.bg_unselected);
                selectedPosition = -1;
            } else {
                // another item is selected, deselect the previous and select the new one
                selectedView.setBackgroundResource(R.drawable.bg_unselected);
                selectedView = view;
                view.setBackgroundResource(R.drawable.bg_selected);
                selectedPosition = position;
            }

            arrayAdapter.setSelectedPosition(selectedPosition);
        }
    });

适配器代码:

public class MyAdapter extends ArrayAdapter<MyObject> {
    private int selectedPosition;

    private static class ViewHolder {
        RelativeLayout layout;
        ...
    }

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_row, null);
            holder = new ViewHolder();
            holder.layout = (RelativeLayout) convertView.findViewById(R.id.rl_item);
            ...

            convertView.setTag(holder);
        }
        else
            holder = (ViewHolder) convertView.getTag();

        ...
        if (position == selectedPosition)
            holder.layout.setBackgroundResource(R.drawable.bg_selected);
        else
            holder.layout.setBackgroundResource(R.drawable.bg_unselected);
        ...

        return convertView;
     }

     ...
}

希望它有所帮助!