Multiple selection in custom ListView with CAB

前端 未结 3 790
孤城傲影
孤城傲影 2020-11-28 21:43

After reading and try\'n\'error for days, I´m giving up and ask for help.

< edit > I am using ActionBarSherlock. < /edit >

What I want to achie

3条回答
  •  感情败类
    2020-11-28 22:30

    See if the code helps you(it's basically a ListActivity with a custom adapter to hold the status of checked items(+ different background)):

    public class CABSelection extends ListActivity {
    
        private ArrayList mItems = new ArrayList();
        private SelectionAdapter mAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            for (int i = 0; i < 24; i++) {
                mItems.add("Name" + i);
            }
            // R.layout.adapters_cabselection_row is a LinearLayout(with green
            // background(#99cc00)) that wraps an ImageView and a TextView
            mAdapter = new SelectionAdapter(this,
                    R.layout.adapters_cabselection_row, R.id.the_text, mItems);
            setListAdapter(mAdapter);
            getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
            getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() {
    
                private int nr = 0;
    
                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    MenuInflater inflater = getMenuInflater();
                    inflater.inflate(R.menu.cabselection_menu, menu);
                    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 positions = mAdapter.getCurrentCheckedPosition();
                    for (Integer pos : positions) {
                        sb.append(" " + pos + ",");
                    }               
                    switch (item.getItemId()) {
                    case R.id.edit_entry:
                        Toast.makeText(CABSelection.this, "Edited entries: " + sb.toString(),
                                Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.delete_entry:
                        Toast.makeText(CABSelection.this, "Deleted entries : " + sb.toString(),
                                Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.finish_it:
                        nr = 0;
                        mAdapter.clearSelection();
                        Toast.makeText(CABSelection.this, "Finish the CAB!",
                                Toast.LENGTH_SHORT).show();
                        mode.finish();
                    }
                    return false;
                }
    
                @Override
                public void onDestroyActionMode(ActionMode mode) {
                    nr = 0;
                    mAdapter.clearSelection();
                }
    
                @Override
                public void onItemCheckedStateChanged(ActionMode mode,
                        int position, long id, boolean checked) {
                    if (checked) {
                        nr++;
                        mAdapter.setNewSelection(position, checked);                    
                    } else {
                        nr--;
                        mAdapter.removeSelection(position);                 
                    }
                    mode.setTitle(nr + " rows selected!");
    
                }
    
            });
        }
    
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            l.setItemChecked(position, !mAdapter.isPositionChecked(position));
        }
    
        private class SelectionAdapter extends ArrayAdapter {
    
            private HashMap mSelection = new HashMap();
    
            public SelectionAdapter(Context context, int resource,
                    int textViewResourceId, List objects) {
                super(context, resource, textViewResourceId, objects);
            }
    
            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 getCurrentCheckedPosition() {
                return mSelection.keySet();
            }
    
            public void removeSelection(int position) {
                mSelection.remove(position);
                notifyDataSetChanged();
            }
    
            public void clearSelection() {
                mSelection = new HashMap();
                notifyDataSetChanged();
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views
                v.setBackgroundColor(Color.parseColor("#99cc00")); //default color
                if (mSelection.get(position) != null) {
                    v.setBackgroundColor(Color.RED);// this is a selected position so make it red
                }
                return v;
            }
    
        }
    
    }
    

    The R.layout.adapters_cabselection_row is a custom layout for the row(a very simple one) with a green background:

    
    
    
        
    
        
    
    
    

    R.menu.cabselection_menu is a menu file with 3 options(edit, delete, finish the CAB) which don't do anything except pop a Toast with a message regarding the rows selected:

    
    
    
        
        
        
    
    
    

提交回复
热议问题