Example usage for android.widget AbsListView CHOICE_MODE_MULTIPLE

List of usage examples for android.widget AbsListView CHOICE_MODE_MULTIPLE

Introduction

In this page you can find the example usage for android.widget AbsListView CHOICE_MODE_MULTIPLE.

Prototype

int CHOICE_MODE_MULTIPLE

To view the source code for android.widget AbsListView CHOICE_MODE_MULTIPLE.

Click Source Link

Document

The list allows multiple choices

Usage

From source file:app.com.example.android.sunshine.ItemChoiceManager.java

public void onClick(RecyclerView.ViewHolder vh) {
    if (mChoiceMode == AbsListView.CHOICE_MODE_NONE)
        return;//from w  w w.  j a  v a  2 s  . c  om

    int checkedItemCount = mCheckStates.size();
    int position = vh.getAdapterPosition();

    if (position == RecyclerView.NO_POSITION) {
        Log.d(LOG_TAG, "Unable to Set Item State");
        return;
    }

    switch (mChoiceMode) {
    case AbsListView.CHOICE_MODE_NONE:
        break;
    case AbsListView.CHOICE_MODE_SINGLE: {
        boolean checked = mCheckStates.get(position, false);
        if (!checked) {
            for (int i = 0; i < checkedItemCount; i++) {
                mAdapter.notifyItemChanged(mCheckStates.keyAt(i));
            }
            mCheckStates.clear();
            mCheckStates.put(position, true);
            mCheckedIdStates.clear();
            mCheckedIdStates.put(mAdapter.getItemId(position), position);
        }
        // We directly call onBindViewHolder here because notifying that an item has
        // changed on an item that has the focus causes it to lose focus, which makes
        // keyboard navigation a bit annoying
        mAdapter.onBindViewHolder(vh, position);
        break;
    }
    case AbsListView.CHOICE_MODE_MULTIPLE: {
        boolean checked = mCheckStates.get(position, false);
        mCheckStates.put(position, !checked);
        // We directly call onBindViewHolder here because notifying that an item has
        // changed on an item that has the focus causes it to lose focus, which makes
        // keyboard navigation a bit annoying
        mAdapter.onBindViewHolder(vh, position);
        break;
    }
    case AbsListView.CHOICE_MODE_MULTIPLE_MODAL: {
        throw new RuntimeException("Multiple Modal not implemented in ItemChoiceManager.");
    }
    }
}

From source file:com.myapps.xyzreader.ui.ItemChoiceManager.java

public void onClick(RecyclerView.ViewHolder vh) {
    if (mChoiceMode == AbsListView.CHOICE_MODE_NONE)
        return;//  w ww  .  j  a v  a2  s.c  om

    int checkedItemCount = mCheckStates.size();
    int position = vh.getAdapterPosition();

    if (position == RecyclerView.NO_POSITION) {
        Log.d(LOG_TAG, "Unable to Set Item State");
        return;
    }

    switch (mChoiceMode) {
    case AbsListView.CHOICE_MODE_NONE:
        break;
    case AbsListView.CHOICE_MODE_SINGLE: {
        boolean checked = mCheckStates.get(position, false);
        if (!checked) {

            mCheckStates.clear();
            mCheckStates.put(position, true);
            mCheckedIdStates.clear();
            mCheckedIdStates.put(mAdapter.getItemId(position), position);

            mAdapter.notifyDataSetChanged(); // select & unselect
        }
        // We directly call onBindViewHolder here because notifying that an item has
        // changed on an item that has the focus causes it to lose focus, which makes
        // keyboard navigation a bit annoying
        mAdapter.onBindViewHolder(vh, position);
        break;
    }
    case AbsListView.CHOICE_MODE_MULTIPLE: {
        boolean checked = mCheckStates.get(position, false);
        mCheckStates.put(position, !checked);
        // We directly call onBindViewHolder here because notifying that an item has
        // changed on an item that has the focus causes it to lose focus, which makes
        // keyboard navigation a bit annoying
        mAdapter.onBindViewHolder(vh, position);
        break;
    }
    case AbsListView.CHOICE_MODE_MULTIPLE_MODAL: {
        throw new RuntimeException("Multiple Modal not implemented in ItemChoiceManager.");
    }
    }
}

From source file:com.example.radioplayer.adapter.ItemChoiceManager.java

public void onClick(RecyclerView.ViewHolder vh) {

    if (mChoiceMode == AbsListView.CHOICE_MODE_NONE) {
        return;/*from  w ww.  j a  v  a2 s  .c o  m*/
    }

    int checkedItemCount = mCheckStates.size();
    int position = vh.getAdapterPosition();
    if (position == RecyclerView.NO_POSITION) {
        Log.d(LOG_TAG, "Unable to Set Item State");
        return;
    }

    switch (mChoiceMode) {
    case AbsListView.CHOICE_MODE_NONE:
        break;
    case AbsListView.CHOICE_MODE_SINGLE: {
        boolean checked = mCheckStates.get(position, false);
        if (!checked) {
            for (int i = 0; i < checkedItemCount; i++) {
                mAdapter.notifyItemChanged(mCheckStates.keyAt(i));
            }
            mCheckStates.clear();
            mCheckStates.put(position, true);
            mCheckedIdStates.clear();
            mCheckedIdStates.put(mAdapter.getItemId(position), position);
        }
        // We directly call onBindViewHolder here because notifying that an item has
        // changed on an item that has the focus causes it to lose focus, which makes
        // keyboard navigation a bit annoying
        mAdapter.onBindViewHolder(vh, position);
        break;
    }
    case AbsListView.CHOICE_MODE_MULTIPLE: {
        boolean checked = mCheckStates.get(position, false);
        mCheckStates.put(position, !checked);
        // We directly call onBindViewHolder here because notifying that an item has
        // changed on an item that has the focus causes it to lose focus, which makes
        // keyboard navigation a bit annoying
        mAdapter.onBindViewHolder(vh, position);
        break;
    }
    case AbsListView.CHOICE_MODE_MULTIPLE_MODAL: {
        throw new RuntimeException("Multiple Modal not implemented in ItemChoiceManager.");
    }
    }
}

From source file:com.harlie.android.sunshine.app.ItemChoiceManager.java

@SuppressWarnings("unchecked")
public void onClick(RecyclerView.ViewHolder vh) {
    if (mChoiceMode == AbsListView.CHOICE_MODE_NONE)
        return;/*from  w  ww .  ja v  a2 s.  co  m*/

    int checkedItemCount = mCheckStates.size();
    int position = vh.getAdapterPosition();

    if (position == RecyclerView.NO_POSITION) {
        Log.d(LOG_TAG, "Unable to Set Item State");
        return;
    }

    switch (mChoiceMode) {
    case AbsListView.CHOICE_MODE_NONE:
        break;
    case AbsListView.CHOICE_MODE_SINGLE: {
        boolean checked = mCheckStates.get(position, false);
        if (!checked) {
            for (int i = 0; i < checkedItemCount; i++) {
                mAdapter.notifyItemChanged(mCheckStates.keyAt(i));
            }
            mCheckStates.clear();
            mCheckStates.put(position, true);
            mCheckedIdStates.clear();
            mCheckedIdStates.put(mAdapter.getItemId(position), position);
        }
        // We directly call onBindViewHolder here because notifying that an item has
        // changed on an item that has the focus causes it to lose focus, which makes
        // keyboard navigation a bit annoying
        mAdapter.onBindViewHolder(vh, position); // warning: Unchecked call to 'onBindViewHolder(VH, int)' as a member of raw type 'android.support.v7.widget.RecyclerView.Adapter'
        break;
    }
    case AbsListView.CHOICE_MODE_MULTIPLE: {
        boolean checked = mCheckStates.get(position, false);
        mCheckStates.put(position, !checked);
        // We directly call onBindViewHolder here because notifying that an item has
        // changed on an item that has the focus causes it to lose focus, which makes
        // keyboard navigation a bit annoying
        mAdapter.onBindViewHolder(vh, position); // warning: Unchecked call to 'onBindViewHolder(VH, int)' as a member of raw type 'android.support.v7.widget.RecyclerView.Adapter'
        break;
    }
    case AbsListView.CHOICE_MODE_MULTIPLE_MODAL: {
        throw new RuntimeException("Multiple Modal not implemented in ItemChoiceManager.");
    }
    }
}

From source file:com.ultramegasoft.flavordex2.dialog.AppChooserDialog.java

@NonNull
@Override/*w w w .  jav a  2s. co  m*/
@SuppressLint("InflateParams")
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Context context = getContext();
    if (context == null) {
        return super.onCreateDialog(savedInstanceState);
    }

    final Bundle args = getArguments();
    final boolean multiChoice = args != null && args.getBoolean(ARG_MULTI_CHOICE);
    final View root = LayoutInflater.from(context).inflate(R.layout.dialog_app_chooser, null);

    mListView = root.findViewById(R.id.list);
    mListView.setAdapter(new AppListAdapter(context, multiChoice));

    final int count;
    if (multiChoice) {
        count = mListView.getCount();
        mListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                invalidateButtons();
            }
        });

        final ListAdapter adapter = mListView.getAdapter();
        for (int i = 0; i < count; i++) {
            mListView.setItemChecked(i, adapter.isEnabled(i));
        }
    } else {
        count = 1;
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(id);
            }
        });
    }

    final TextView header = root.findViewById(R.id.header);
    final String appsString = getResources().getQuantityString(R.plurals.app, count);
    header.setText(getString(R.string.header_select_app, appsString));

    final AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle(R.string.title_import_app)
            .setIcon(R.drawable.ic_import).setView(root).setNegativeButton(R.string.button_cancel, null);

    if (multiChoice) {
        builder.setPositiveButton(R.string.button_import, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                importSelected();
            }
        });
    }

    return builder.create();
}

From source file:com.hangulo.xyzreader2.ui.ItemChoiceManager.java

public void onClick(RecyclerView.ViewHolder vh) {
    if (mChoiceMode == AbsListView.CHOICE_MODE_NONE)
        return;/*  w ww . j ava2s  .c o  m*/

    int checkedItemCount = mCheckStates.size();
    int position = vh.getAdapterPosition();

    if (position == RecyclerView.NO_POSITION) {
        Log.d(LOG_TAG, "Unable to Set Item State");
        return;
    }

    switch (mChoiceMode) {
    case AbsListView.CHOICE_MODE_NONE:
        break;
    case AbsListView.CHOICE_MODE_SINGLE: {
        boolean checked = mCheckStates.get(position, false);
        if (!checked) {
            //                    for (int i = 0; i < checkedItemCount; i++) {
            //                        mAdapter.notifyItemChanged(mCheckStates.keyAt(i)); // this original code is occured some UI delay problem, so I've changed to notifyDataSetChanged().
            //                    }

            mCheckStates.clear();
            mCheckStates.put(position, true);
            mCheckedIdStates.clear();
            mCheckedIdStates.put(mAdapter.getItemId(position), position);

            mAdapter.notifyDataSetChanged(); // select & unselect
        }
        // We directly call onBindViewHolder here because notifying that an item has
        // changed on an item that has the focus causes it to lose focus, which makes
        // keyboard navigation a bit annoying
        mAdapter.onBindViewHolder(vh, position);
        break;
    }
    case AbsListView.CHOICE_MODE_MULTIPLE: {
        boolean checked = mCheckStates.get(position, false);
        mCheckStates.put(position, !checked);
        // We directly call onBindViewHolder here because notifying that an item has
        // changed on an item that has the focus causes it to lose focus, which makes
        // keyboard navigation a bit annoying
        mAdapter.onBindViewHolder(vh, position);
        break;
    }
    case AbsListView.CHOICE_MODE_MULTIPLE_MODAL: {
        throw new RuntimeException("Multiple Modal not implemented in ItemChoiceManager.");
    }
    }
}

From source file:com.itime.team.itime.fragments.CalendarTypeFragment.java

private void initCalendarTypeAdapter() {
    mAdapter = new CalendarTypeAdapter(getContext(), mData);
    mCalendarTypeListView.setAdapter(mAdapter);
    mCalendarTypeListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    // click/*from www.  j av a  2s. c  om*/
    mCalendarTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ParcelableCalendarType calType = (ParcelableCalendarType) mAdapter.getItem(position);
            toggleIfShow(calType);
        }
    });
    // long click
    mCalendarTypeListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            ParcelableCalendarType calType = (ParcelableCalendarType) mAdapter.getItem(position);
            editCalendarType(calType);
            return false;
        }
    });
    // delete button click
    mAdapter.setOnDeleteClickListener(new CalendarTypeAdapter.OnDeleteClickListener() {
        @Override
        public void onDeleteClickListener(View view, int position) {

        }
    });
}

From source file:com.android.contacts.SimImportFragment.java

@Nullable
@Override/*from  w ww .  j ava2s.  c  om*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_sim_import, container, false);

    mAccountHeaderContainer = view.findViewById(R.id.account_header_container);
    mAccountScrolledElevationPixels = getResources().getDimension(R.dimen.contact_list_header_elevation);
    mAccountHeaderPresenter = new AccountHeaderPresenter(mAccountHeaderContainer);
    if (savedInstanceState != null) {
        mAccountHeaderPresenter.onRestoreInstanceState(savedInstanceState);
    } else {
        // Default may be null in which case the first account in the list will be selected
        // after they are loaded.
        mAccountHeaderPresenter.setCurrentAccount(mPreferences.getDefaultAccount());
    }
    mAccountHeaderPresenter.setObserver(new AccountHeaderPresenter.Observer() {
        @Override
        public void onChange(AccountHeaderPresenter sender) {
            rememberSelectionsForCurrentAccount();
            mAdapter.setAccount(sender.getCurrentAccount());
            showSelectionsForCurrentAccount();
            updateToolbarWithCurrentSelections();
        }
    });
    mAdapter.setAccount(mAccountHeaderPresenter.getCurrentAccount());

    mListView = (ListView) view.findViewById(R.id.list);
    mListView.setOnScrollListener(this);
    mListView.setAdapter(mAdapter);
    mListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    mListView.setOnItemClickListener(this);
    mImportButton = view.findViewById(R.id.import_button);
    mImportButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            importCurrentSelections();
            // Do we wait for import to finish?
            getActivity().setResult(Activity.RESULT_OK);
            getActivity().finish();
        }
    });

    mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
    mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getActivity().setResult(Activity.RESULT_CANCELED);
            getActivity().finish();
        }
    });

    mLoadingIndicator = (ContentLoadingProgressBar) view.findViewById(R.id.loading_progress);

    return view;
}

From source file:com.github.runoshun.in_app_survey.ui.QuestionFragment.java

protected View onCreateMultiChoiceView(final MultiChoiceQuestion question, LayoutInflater inflater,
        ViewGroup container, @SuppressWarnings("UnusedParameters") Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_default_choice, container, false);
    TextView questionText = (TextView) view.findViewById(R.id.survey_question);
    ListView listView = (ListView) view.findViewById(R.id.survey_listview);

    questionText.setText(question.question);

    ArrayAdapter<String> adapter = new ArrayAdapter<>(view.getContext(),
            android.R.layout.simple_list_item_multiple_choice, question.choices);
    listView.setAdapter(adapter);/*  w  w w. j a  v  a 2 s. c om*/
    listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            QuestionFragment.this.onMultiChoiceItemClick(question, (ListView) adapterView, position);
        }
    });

    return view;
}

From source file:org.linphone.ContactsListFragment.java

private void searchContacts(String search) {
    if (search == null || search.length() == 0) {
        changeContactsAdapter();/* w w  w. j a  va 2 s.c o m*/
        return;
    }

    if (searchCursor != null) {
        searchCursor.close();
    }
    if (onlyDisplayLinphoneContacts) {
        searchCursor = Compatibility.getSIPContactsCursor(getActivity().getContentResolver(), search,
                ContactsManager.getInstance().getContactsId());
        indexer = new AlphabetIndexer(searchCursor, Compatibility.getCursorDisplayNameColumnIndex(searchCursor),
                " ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        contactsList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        contactsList.setAdapter(new ContactsListAdapter(null, searchCursor));
    } else {
        searchCursor = Compatibility.getContactsCursor(getActivity().getContentResolver(), search,
                ContactsManager.getInstance().getContactsId());
        contactsList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        indexer = new AlphabetIndexer(searchCursor, Compatibility.getCursorDisplayNameColumnIndex(searchCursor),
                " ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        contactsList.setAdapter(new ContactsListAdapter(null, searchCursor));
    }

}