高亮listView的选中的item

1,首先设置ListView的selector,定义该selector.
2,在选中ListView 的item项时高亮该item.

具体实现方法(以设置应用的heard为例):
     1,设置selector, getListView().setSelector(R.drawable.  header_list_selector  );.定义selector:
     
<? xml  version =  "1.0"  encoding  = "utf-8" ?>
< selector  xmlns:android = "http://schemas.android.com/apk/res/android"  >
     < item  android:state_pressed  = "true"  android:drawable = "@drawable/head_list_selector_d"  />
     < item  android:state_activated  = "true"  android:drawable = "@drawable/head_list_selector_s"  />
      < item  android:drawable = "@drawable/head_list_selector_n"  />
</ selector >
     2,在代码中设置选中item的高亮
     protected  void  highlightHeader(  int  id)
    {
         if  (id != 0)
        {
            Integer index =  mHeaderIndexMap .get(id);
             if  (index !=  null )
            {
                getListView().setItemChecked(index,  true );
                 if  (isMultiPane())
                {
                    getListView().smoothScrollToPosition(index);
                }
            }
        }
    }


注意点:
     1,xml文件中应该定义 android:state_activated 属性.
     2,在代码中设置 getListView().setItemChecked(index,  true  );

你可能感兴趣的:(高亮listView的选中的item)