MultiChoiceModeListener GridView选择不更改背景颜色

时间:2015-04-29 04:21:23

标签: android android-listview expandablelistview android-adapter android-gridview

我按照this教程在我的网格视图中实现了多项选择,到目前为止我可以选择项目,但它们没有改变背景颜色,我调试了代码,似乎mSelection总是空的,我不知道为什么会发生这种情况

public class GridAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Child> child;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    private HashMap<Integer, Boolean> mSelection;


    public GridAdapter(Context context, ArrayList<Child> childValues) {
        mContext = context;
        mSelection = new HashMap<Integer, Boolean>();
        child = childValues;
    }


    @Override
    public int getCount() {
        return child.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

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

        ViewHolder holder = null;
        NetworkImageView i;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.child_item, null);
            holder = new ViewHolder();

            if (imageLoader == null)
                imageLoader = AppController.getInstance().getImageLoader();

            i = (NetworkImageView) convertView
                    .findViewById(R.id.flag);

            i.setImageUrl(String.valueOf(child.get(position).getImage()), imageLoader);


            if (mSelection.get(position) != null) {

                convertView.setBackgroundResource(R.color.bg);// this is a selected position so make it red
            }

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

        holder.text = (TextView) convertView.findViewById(R.id.label);
        holder.text.setText(child.get(position).getName());
        return convertView;
    }


    static class ViewHolder {
        TextView text;
    }

    public void setNewSelection(int position, boolean value) {
        mSelection.put(position, value);
        notifyDataSetChanged();
    }

    public boolean isPositionChecked(int position) {
        Boolean result = mSelection.get(position);
        return result == null ? false : result;
    }

    public Set<Integer> getCurrentCheckedPosition() {
        return mSelection.keySet();
    }

    public void removeSelection(int position) {
        mSelection.remove(position);
        notifyDataSetChanged();
    }

    public void clearSelection() {
        mSelection = new HashMap<Integer, Boolean>();
        notifyDataSetChanged();
    }
}

可扩展适配器:

adapter = new GridAdapter(context, listchild);
        gridView.setAdapter(adapter);// Adapter
        gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);

        gridView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
            private int nr = 0;
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, final int position, long id, boolean checked) {


                if (checked) {
                    nr++;
                    adapter.setNewSelection(position, checked);
                } else {
                    nr--;
                    adapter.removeSelection(position);
                }
                mode.setTitle(nr + " rows selected!");

            }


            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                mode.setTitle("Select Items");

                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                StringBuilder sb = new StringBuilder();
                Set<Integer> positions = adapter.getCurrentCheckedPosition();
                for (Integer pos : positions) {
                    sb.append(" " + pos + ",");
                }

                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                nr = 0;
                adapter.clearSelection();

            }


        });


        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gridView.setItemChecked(position, !adapter.isPositionChecked(position));
            }
        });

        return convertView;

    }

0 个答案:

没有答案