触碰模式下的listview的选中项背景高亮

我在工作中发现:触碰模式下的listview项无法被选中,所谓选中,意思是点击选中该项后,背景长期高亮。
      在模拟器上测试时,可以使用鼠标滑轮滚动选中,这会调用onItemSelected()方法,在这里可以设置选中项高亮。
      在真机上,由于没有滑轮,只能用手指点击选中,但是点击时,不会有选中后高亮的效果(注意:选中后高亮不是指点击选中那一下高亮,而是点击后长期高亮)。
      或许你会想到使用selector背景选择器来设置:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <!-- 默认时的背景图片-->  
  4. <item android:drawable="@drawable/pic1" />  
  5. <!-- 没有焦点时的背景图片-->  
  6. <item android:state_window_focused="false" android:drawable="@drawable/pic1" />  
  7. <!-- 非触摸模式下获得焦点并单击时的背景图片-->  
  8. <item android:state_focused="true" android:state_pressed="true"  
  9. android:drawable="@drawable/pic2" />  
  10. <!-- 触摸模式下单击时的背景图片-->  
  11. <item android:state_focused="false" android:state_pressed="true"  
  12. android:drawable="@drawable/pic3" />  
  13. <!--选中时的图片背景-->  
  14. <item android:state_selected="true" android:drawable="@drawable/pic4" />  
  15. <!--获得焦点时的图片背景-->  
  16. <item android:state_focused="true" android:drawable="@drawable/pic5" />  
  17. </selector>  
悲剧的是:我无法使用selector实现我想要的那种选中后长期高亮的状态
于是我只能在java代码中寻求思路了

下面的代码实现listview单选模式下点击后背景图片长期高亮
[java]  view plain copy
  1. package org.yaoming.listview;  
  2.   
  3. import org.yaoming.util.Common;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.content.SharedPreferences;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.ViewGroup;  
  13. import android.widget.AdapterView;  
  14. import android.widget.AdapterView.OnItemClickListener;  
  15. import android.widget.BaseAdapter;  
  16. import android.widget.ImageView;  
  17. import android.widget.LinearLayout;  
  18. import android.widget.ListView;  
  19. import android.widget.TextView;  
  20.   
  21. public class Listview_selectorActivity extends Activity implements OnItemClickListener {  
  22.     private String[] arrays;  
  23.     /** Called when the activity is first created. */  
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.         arrays = new String[] {"a","b","c","d","e"};  
  29.         //使用SharedPreferences是为了每次点击退出时都保存之前选中的项,以后一打开时就可以看到以前的选择  
  30.         SharedPreferences sp = getSharedPreferences("testListview"0);  
  31.         Common.SELECTED = sp.getInt("selected", Common.SELECTED);  
  32.         ListView listview =  (ListView) findViewById(R.id.listView1);  
  33.         listview.setAdapter(new MyAdapter(this));  
  34.         listview.setOnItemClickListener(this);  
  35.           
  36.     }  
  37.     public class MyAdapter extends BaseAdapter{  
  38.         Context context;  
  39.           
  40.         public class ViewHolder{  
  41.             TextView tv;  
  42.             ImageView iv;  
  43.         }   
  44.           
  45.         public MyAdapter(Context context){  
  46.             this.context = context;   
  47.         }  
  48.   
  49.         @Override  
  50.         public int getCount() {  
  51.             // TODO Auto-generated method stub  
  52.             return arrays.length;  
  53.         }  
  54.   
  55.         @Override  
  56.         public Object getItem(int position) {  
  57.             // TODO Auto-generated method stub  
  58.             return position;  
  59.         }  
  60.   
  61.         @Override  
  62.         public long getItemId(int position) {  
  63.             // TODO Auto-generated method stub  
  64.             return position;  
  65.         }  
  66.   
  67.         @Override  
  68.         public View getView(int position, View convertView, ViewGroup parent) {  
  69.               
  70.             //通过日志测试,原来每次点击listview的每一项,这里都会运行  
  71.             //通过查看源码,原来是getItemAtPosition()中getAdapter()搞的鬼  
  72.             Log.i("listview""zhixingguo ");  
  73.               
  74.             LinearLayout layout;  
  75.             ViewHolder viewholder;  
  76.             if(null == convertView){  
  77.                 layout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.item, null);  
  78.                 viewholder = new ViewHolder();  
  79.                 viewholder.tv = (TextView) layout.findViewById(R.id.textView2);  
  80.                 viewholder.iv = (ImageView) layout.findViewById(R.id.imageView1);  
  81.                 convertView = layout;  
  82.                 layout.setTag(viewholder);  
  83.             }else{  
  84.                 layout = (LinearLayout) convertView;  
  85.                 viewholder = (ViewHolder) layout.getTag();  
  86.             }  
  87.               
  88.             viewholder.tv.setText(arrays[position]);  
  89.             //在这里加个判断,若为选中项,则改变背景图片和背景色  
  90.             if(Common.SELECTED == position){  
  91.                 viewholder.iv.setBackgroundResource(R.drawable.asi);  
  92.                 layout.setBackgroundResource(R.color.blue);  
  93.             }else {  
  94.                 viewholder.iv.setBackgroundResource(R.drawable.icon);  
  95.                 layout.setBackgroundResource(R.color.transparent);  
  96.             }  
  97.             return convertView;  
  98.         }  
  99.           
  100.     }  
  101.     @Override  
  102.     public void onItemClick(AdapterView<?> parent, View view, int position,  
  103.             long id) {  
  104.         //如果在这里简单地通过findviewbyid()改变背景图片,点击时会出现图片乱显示的情况  
  105. //      ImageView iv =  (ImageView) view.findViewById(R.id.imageView1);  
  106. //      iv.setImageResource(R.drawable.asi);  
  107.           
  108.         //改变SELECTED的值,并在getview里判断加载的位置是否为选中的位置  
  109.         Common.SELECTED = position;  
  110.     }  
  111.       
  112. }  

还有一个自定义的util类
[java]  view plain copy
  1. package org.yaoming.util;  
  2.   
  3. public class Common {  
  4.    public static int SELECTED = -1;  
  5. }  


另外,main.xml的代码:
[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <TextView android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content" android:text="@string/hello" />  
  7.     <ListView android:id="@+id/listView1" android:choiceMode="singleChoice"  
  8.         android:cacheColorHint="#00000000" android:layout_height="wrap_content"  
  9.         android:layout_width="match_parent"></ListView>  
  10. </LinearLayout>  

listview的item布局:
[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="horizontal"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent">  
  7.     <TextView android:text="TextView"  
  8.         android:id="@+id/textView2" android:textSize="30sp"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"></TextView>  
  11.     <ImageView android:src="@drawable/icon"  
  12.         android:layout_width="wrap_content" android:id="@+id/imageView1"  
  13.         android:layout_height="wrap_content"></ImageView>  
  14.   
  15. </LinearLayout>  

不使用selector,直接在java代码里设置。在onItemClick的时候获得position,并在getview判断是否为选中项,设置背景颜色高亮,记得布局文件中listview要设置成singleChoiceMode。
值得注意的是, onItemClick会触发getItemAtPosition方法
[java]  view plain copy
  1. /** 
  2.      * Callback method to be invoked when an item in this AdapterView has 
  3.      * been clicked. 
  4.      * <p> 
  5.      * Implementers can call getItemAtPosition(position) if they need 
  6.      * to access the data associated with the selected item. 
  7.      * 
  8.      * @param parent The AdapterView where the click happened. 
  9.      * @param view The view within the AdapterView that was clicked (this 
  10.      *            will be a view provided by the adapter) 
  11.      * @param position The position of the view in the adapter. 
  12.      * @param id The row id of the item that was clicked. 
  13.      */  
  14.     void onItemClick(AdapterView<?> parent, View view, int position, long id);  
  15. }  

getItemAtPosition又会调用getAdapter方法,就相当于重新刷新一次ui中的listview
[java]  view plain copy
  1. /** 
  2.      * Gets the data associated with the specified position in the list. 
  3.      * 
  4.      * @param position Which data to get 
  5.      * @return The data associated with the specified position in the list 
  6.      */  
  7.     public Object getItemAtPosition(int position) {  
  8.         T adapter = getAdapter();  
  9.         return (adapter == null || position < 0) ? null : adapter.getItem(position);  
  10.     }  

实现效果如下:


下面是源码下载地址

http://download.csdn.net/detail/iamkila/4034770


原文:http://blog.csdn.net/iamkila/article/details/7218351

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值