0

I want custom ListView layout which has multiple selection , so I am doing custom adapter but how I can allow user to select multiple . In default ListView we are given choice mode but I want layout different not checkbook

  <Imageview>
  <Textview>

Do I have to manage in onItemClick or any method is der ? Small snippet will help enter image description here

1

2 Answers 2

2

For this you need ListView.CHOICE_MODE_MULTIPLE_MODAL. See the following code snippet,

First create a ListView and it's adapter,

        listView = (ListView) findViewById(R.id.listView);
        adapter = new AttendanceListAdapter(this, attendanceList);

Set the List choice mode to multiple and add a Multi choice listener,

        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        listView.setMultiChoiceModeListener(new ModeCallback());
        listView.setAdapter(adapter);

Your Multi choice listener should look something like this,

private class ModeCallback implements ListView.MultiChoiceModeListener {

        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.list_select_menu, menu);
            mode.setTitle("Select Items");
            return true;
        }

        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return true;
        }

        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.share:
                    Toast.makeText(AddAttendanceActivity.this, "Shared " + listView.getCheckedItemCount() +
                            " items", Toast.LENGTH_SHORT).show();
                    mode.finish();
                    break;
                default:
                    Toast.makeText(AddAttendanceActivity.this, "Clicked " + item.getTitle(),
                            Toast.LENGTH_SHORT).show();
                    break;
            }
            return true;
        }

        public void onDestroyActionMode(ActionMode mode) {
        }

        public void onItemCheckedStateChanged(ActionMode mode,
                                              int position, long id, boolean checked) {
            final int checkedCount = listView.getCheckedItemCount();
            switch (checkedCount) {
                case 0:
                    mode.setSubtitle(null);
                    break;
                case 1:
                    mode.setSubtitle("One item selected");
                    break;
                default:
                    mode.setSubtitle("" + checkedCount + " items selected");
                    break;
            }
        }

    }

Now if you want the selected rows to highlighted add this style to the root element of your list items layout.

  <style name="activated" parent="AppTheme">
        <item name="android:background">?android:attr/activatedBackgroundIndicator</item>
    </style>
Sign up to request clarification or add additional context in comments.

1 Comment

setMultiChoiceModeListener is api 11+
0

I think you you want something like whatsup select for that on long click listener you can change color of custom listview items(rows)

1 Comment

He's talking about multiple list row selection. By long press you can only highlight a single row not multiple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.