ListView for multi-select/Radio

Source: Internet
Author: User

The ListView itself has a single, multi-select mode, which can be set by Listview.setchoicemode:

Listview.setchoicemode (listview.choice_mode_multiple);//Turn on multi-select mode listview.setchoicemode ( Listview.choice_mode_single);//Open Radio mode Listview.setchoicemode (listview.choice_mode_none);//default mode Listview.setchoicemode (Listview.choice_mode_multiple_modal);//not used, do not know what to do
implementing a single selectionRequired settings: Listview.setchoicemode (Listview.choice_mode_single);
the ListView control also needs to specify a selector:android:listselector= "@drawable/checkable_item_selector" [HTML]View Plaincopy
  1. <selector xmlns:android="">
  2. <item android:drawable= "@color/c1" android:state_pressed="true"/>
  3. <item android:drawable= "@color/c1" android:state_checked="true"/>
  4. <item android:drawable="@color/c2"/>
  5. </selector>
but the color of the single-selection is still the system-selected color, not the C1 you set, do not know why?
implement multi-selectsetting: Listview.setchoicemode (Listview.choice_mode_single);
To implement the Checkable interface for each item's view, the following is the LinearLayout implementation checkable interface: [Java]View Plaincopy
  1. Public class Checkablelinearlayout extends LinearLayout implements checkable {
  2. private Boolean mchecked;
  3. Public checkablelinearlayout (context context, AttributeSet attrs) {
  4. Super (context, attrs);
  5. }
  6. @Override
  7. public void Setchecked (boolean checked) {
  8. mchecked = checked;
  9. Setbackgrounddrawable (checked? New Colordrawable (0xff0000a0): null); Blue appears when selected
  10. }
  11. @Override
  12. Public Boolean isChecked () {
  13. return mchecked;
  14. }
  15. @Override
  16. public void Toggle () {
  17. Setchecked (!mchecked);
  18. }
  19. }
Use as follows: [HTML]View Plaincopy
  1. <com.ljfbest.temp.CheckableLinearLayout xmlns:android=" Android "
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content">
  4. <TextView
  5. android:id="@+id/tv"
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:gravity="center_vertical"
  9. android:minheight="? Android:attr/listpreferreditemheightsmall"
  10. android:paddingend="? android:attr/listpreferreditempaddingend"
  11. android:paddingstart="? Android:attr/listpreferreditempaddingstart"
  12. android:textappearance="? Android:attr/textappearancelistitemsmall" />
  13. </com.ljfbest.temp.CheckableLinearLayout>
The demo chart is attached below:

Here are a few APIs to get/set the selected entry information:Listview.getcheckeditemcount ();//Gets the selected number of rows: invalid for Choice_mode_nonelistview.getcheckeditemposition ();//Gets the selected row, only valid for radio mode, returns an intlistview.getcheckeditemids ();//Gets the IDs of the selected entries, which is the long[] type, note that adapter Hasstableids () is required to return true, And these IDs are returned by adapter's getitemid (int position). Demo in Demo

listview.setitemchecked (position, value);//Set the state of a line

ListView for multi-select/Radio

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.