How to highlight row in ListView in Android?

后端 未结 8 1419
天涯浪人
天涯浪人 2020-11-27 06:05

I need to highlight a row in a ListView that was selected (to show the user what he chose), so, it\'s not the one that is going to be chosen, it\'s the one he c

相关标签:
8条回答
  • 2020-11-27 06:11

    I had a problem finding an easy solution to this because so many tutorials and answers contained information on single select via radio buttons (Which relied heavily on RadioGroup's).

    My problem was that I could set the item to my "Highlighted" state but then couldn't reset the list when the next item was selected. Here is what I came up with:

    listView.setOnItemClickListener(new ItemHighlighterListener ());
    

    With this class:

    private class ItemHighlighterListener implements OnItemClickListener{
    
        private View lastSelectedView = null;
    
        public void clearSelection()
        {
            if(lastSelectedView != null) lastSelectedView.setBackgroundColor(android.R.color.transparent);
        }
    
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
            clearSelection();
            lastSelectedView = view;
            view.setBackgroundDrawable(view.getContext().getResources().getDrawable(R.drawable.shape_selected_menu_item));
        }
    }
    
    0 讨论(0)
  • 2020-11-27 06:16

    Just:

    1. Set the correct choice mode in your list view. setChoiceMode
    2. Set a background to support the selection state in you item layout, like:

      android:background="?android:attr/activatedBackgroundIndicator"
      

    FYI:

    • http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
    • http://android-developers.blogspot.mx/2008/12/touch-mode.html
    0 讨论(0)
  • 2020-11-27 06:20

    You can use transition. Follow my code because i have achieved highlighting of specific listview item by choice Lets say you want to highlight first item for 5 seconds.

     if(position == 0){
                        viewHolderThubnail.relImage.setBackgroundResource(R.drawable.translate);
                        TransitionDrawable transition = (TransitionDrawable) viewHolderThubnail.relImage.getBackground();
                        transition.startTransition(1000);
    
                    }else{
    
                        viewHolderThubnail.relImage.setBackgroundResource(R.color.white);
                    }
    

    translate.xml

    <?xml version="1.0" encoding="UTF-8"?>
       <transition xmlns:android="http://schemas.android.com/apk/res/android">
              <!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
              <item android:drawable="@drawable/new_state" />
              <item android:drawable="@drawable/original_state" />
       </transition>
    

    new_state.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <shape   xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle">
        <solid android:color="#92BCE7"/>
    </shape>
    

    original_state.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <shape   xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="rectangle">
        <solid android:color="#FFFFFF"/>
    </shape>
    

    If you understood this code, which is very simple i must say, then the listview item at zero'th position will highlight in blue color for 5 seconds and then it will slow fade to white color.

    0 讨论(0)
  • 2020-11-27 06:24

    It's much easier to implement this in your layout files and let Android handle the rest...

    1) Make sure you have android:choiceMode="" set on your ListView layout (singleChoice, multipleChoice, etc). By default it is set to none.

    <ListView
        android:id="@+id/invite_friends_fragment_contacts_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:choiceMode="multipleChoice"/> <!-- THIS LINE -->
    

    2) Create a state selector XML file and save it in your drawables folder. In this example, we'll name it state_selector.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@android:color/blue" android:state_selected="true"/>
        <item android:drawable="@android:color/blue" android:state_activated="true"/>
        <item android:drawable="@android:color/transparent"/>
    </selector>
    

    3) In your list item layout, add the state_selector.xml file as the background:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/state_selector">  <!-- THIS LINE -->
    
        <!-- ALL OF YOUR ELEMENTS WILL GO HERE (TextViews, ImageViews, etc) -->
    
    </RelativeLayout>
    

    If you are using multipleChoice, you can override onItemClick()and set/unset selected items accordingly. Android will change the background color as specified in your state_selector.xml file.

    0 讨论(0)
  • 2020-11-27 06:27

    The SIMPLEST of all

    View updatedview=null;
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    
        //these two lines of code means that only one item can be selected at a time
        if(updatedview != null)
            updatedview.setBackgroundColor(Color.TRANSPARENT);
        updatedview=view;
    
        Toast.makeText(getApplicationContext(), " " + str[position],Toast.LENGTH_LONG).show();
        view.setBackgroundColor(Color.CYAN);
    }
    
    0 讨论(0)
  • 2020-11-27 06:31

    Why dont you store the selections in an array, then pass that array in the constructor of the ListView Array Adapter, something like myArrayAdapter(context,layoutID,dataArray,selectionArray)

    then in your getView method for the arrayadapter, just do a check. For example in pseudocode

    if row was previously selected
        change background color
    
    0 讨论(0)
提交回复
热议问题