setSelected在onLongClick()中工作,但在bindView()中不起作用

时间:2015-05-21 14:07:14

标签: android listview

我正在实现CAB,我的ListView是从数据库中填充的。当我滚动ListView或旋转设备屏幕时,先前所选项目的背景将恢复为默认背景。

我用来存储选择状态的持有人,以便在bindView中恢复它:

private static class ViewInfo {
    boolean selected;
}

bindView:

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        view.setOnLongClickListener(mOnLongClickListener);
        Object tag = view.getTag();
        if (tag != null) {
            ViewInfo info = (ViewInfo) view.getTag();
            view.setSelected(info.selected);
        } else {
            view.setTag(new ViewInfo());
        }

        // Load data from the database here
    }

OnLongClickListener:

mOnLongClickListener = new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        ViewInfo viewInfo = (ViewInfo) v.getTag();
        v.setSelected(viewInfo.selected = !viewInfo.selected);
        return true;
    }
};

我的ListView:

<ListView
    android:id="@+id/filtering_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:choiceMode="multipleChoiceModal"
    android:drawSelectorOnTop="true" />

我的列表项背景 filtering_list_item_bg

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

    <item android:drawable="@android:color/holo_blue_light" android:state_pressed="true"/>
    <!-- pressed -->
    <item android:drawable="@android:color/holo_blue_light" android:state_focused="true"/>
    <!-- focused -->
    <item android:drawable="@android:color/background_light" android:state_selected="true"/>

</selector>

我的列表项布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/filtering_list_item_bg"
    android:paddingBottom="12dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="12dp" >

    <!-- text views, image views, etc. -->

</RelativeLayout>

我在这里无法理解的是setSelectedbindView中调用但未改变背景的原因。

2 个答案:

答案 0 :(得分:1)

您在哪里将选择器设置为列表项?你需要这样做。

试试这个 - 转到List Item XML代码,假设外部布局类似于以下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/my_selector"
android:layout_width="match_parent"
android:layout_height="match_parent">

注意这一行 - android:background =&#34; @ drawable / my_selector&#34; - 您需要在列表项中添加它,然后在选择时它将改变颜色等。

将Long Click侦听器添加到listView而不是item-line 173中的bitbucket repo应该读取

 mAdapter = new ListAdapter(getActivity(),
                R.layout.filter_list_item,
                null,
                COLUMNS,
                new int[] { R.id.ip_address, R.id.port },
                0);

        mFilteringListView.setAdapter(mAdapter);

       mFilteringListView.setOnLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            ViewInfo viewInfo = (ViewInfo) v.getTag();
                if (viewInfo.selected) {
                    mSelectedCount--;
                } else {
                    mSelectedCount++;
                }
                v.setSelected(viewInfo.selected = !viewInfo.selected);
                if (mSelectedCount > 0) {
                    mActivity.startActionMode(mActionModeCallback);
                }
                return true;
        }
    });

答案 1 :(得分:1)

我设法在没有setSelected的情况下保存所选项目的颜色。

我的列表项没有背景颜色。它是根据当前状态(选中/未选中)以编程方式分配的。

我的列表项布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="12dp"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="12dp" >
    <!-- text views, image views, etc. -->
</RelativeLayout>

我的ListView。

<ListView
        android:id="@+id/filtering_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

OtItemLongClickListener。我将所选项目保存到哈希表中,因为它offers O(1) for the get, put, and remove operations.

mFilteringListView.setOnItemLongClickListener(new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if (mAdapter.mSelectedIds.get(id) == null || !mAdapter.mSelectedIds.get(id)) {
            mAdapter.mSelectedIds.put(id, true);
            view.setBackgroundColor(getActivity().getResources().getColor(R.color.highlighted_item));
        } else {
            /*
             * I don't put a false because there is no reason to store unselected items.
             * If the user selects and unselects items much, the hash table will grow
             * rapidly
             * 
             * Maksim Dmitriev
             * May 21, 2015
             */
            mAdapter.mSelectedIds.remove(id);
            view.setBackgroundColor(getActivity().getResources().getColor(android.R.color.white));
        }
        return true;
    }
});

ListView适配器,其中为哈希表中的每个项目恢复了背景:

private static class ListAdapter extends SimpleCursorAdapter {

    final Context mContext;
    LongSparseArray<Boolean> mSelectedIds = new LongSparseArray<Boolean>();

    public ListAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
        mContext = context;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        long id = cursor.getInt(cursor.getColumnIndex(Columns._ID));
        if (mSelectedIds.get(id) == null || !mSelectedIds.get(id)) {
            view.setBackgroundColor(mContext.getResources().getColor(android.R.color.white));
        } else {
            view.setBackgroundColor(mContext.getResources().getColor(R.color.highlighted_item));
        }
        // fill the other text views, image views, etc.
    }
}