关闭

android中设置ListView的选中的Item的背景颜色

标签: Androidlistviewitem背景颜色
92人阅读 评论(0) 收藏 举报
ListView中没有默认的选择颜色,只有选择Item后的焦点颜色,鼠标点击时Item有颜色,放开鼠标后颜色也就没有了,要实现放开鼠标后选择项的背景还是有颜色的。

  

   

1、配置main.xml

Java代码 复制代码 收藏代码
  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.     <ListView android:id="@+id/listView" android:listSelector="#000000" 
  6.         android:layout_width="fill_parent" 
  7.         android:layout_height="wrap_content" /> 
  8. </LinearLayout> 

    设置Item的获取焦点颜色为白色android:listSelector="#000000"(即不显示背景颜色)

2、配置用于ListView显示Item的button_layout.xml

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout 
  3.   xmlns:android="http://schemas.android.com/apk/res/android" 
  4.   android:id="@+id/LinearLayoutButton" 
  5.   android:layout_width="144px" 
  6.   android:layout_height="99px" 
  7.   android:gravity="center" 
  8.   android:orientation="vertical"
  9.    
  10.      
  11.     <TextView  
  12.         android:id="@+id/TextViewButton"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="fill_parent" 
  15.         android:textSize="20px"
  16.     </TextView> 
  17.      
  18.      
  19. </LinearLayout> 

3、实现Activity

Java代码 复制代码 收藏代码
  1. package com.listButtonTest.www; 
  2.  
  3. import java.util.ArrayList; 
  4.  
  5. import android.app.Activity; 
  6. import android.content.Context; 
  7. import android.graphics.Color; 
  8. import android.os.Bundle; 
  9. import android.os.Handler; 
  10. import android.os.Message; 
  11. import android.view.LayoutInflater; 
  12. import android.view.View; 
  13. import android.view.ViewGroup; 
  14. import android.widget.AdapterView; 
  15. import android.widget.BaseAdapter; 
  16. import android.widget.LinearLayout; 
  17. import android.widget.ListView; 
  18. import android.widget.TextView; 
  19.  
  20. public class listButtonTest extends Activity { 
  21.     private ListView listView = null
  22.     private ListAdapter listAdapter = null
  23.  
  24.     /** Called when the activity is first created. */ 
  25.     @Override 
  26.     public void onCreate(Bundle savedInstanceState) { 
  27.         super.onCreate(savedInstanceState); 
  28.         setContentView(R.layout.main); 
  29.         listView = (ListView) this.findViewById(R.id.listView); 
  30.         ArrayList<ButtonView> buttonListView = new ArrayList<ButtonView>(); 
  31.         ButtonView a = new ButtonView(R.string.l1); 
  32.         buttonListView.add(a); 
  33.         ButtonView b = new ButtonView(R.string.l2); 
  34.         buttonListView.add(b); 
  35.         ButtonView c = new ButtonView(R.string.l3); 
  36.         buttonListView.add(c); 
  37.         ButtonView d = new ButtonView(R.string.l4); 
  38.         buttonListView.add(d); 
  39.         ButtonView e = new ButtonView(R.string.l5); 
  40.         buttonListView.add(e); 
  41.  
  42.         listAdapter = new ListAdapter(buttonListView); 
  43.         listView.setAdapter(listAdapter); 
  44.         listView.setDividerHeight(0); 
  45.  
  46.         listView.setOnItemClickListener(new ListView.OnItemClickListener() { 
  47.  
  48.             @Override 
  49.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
  50.                     long arg3) { 
  51.                 // TODO Auto-generated method stub 
  52.                 listAdapter.setSelectedPosition(arg2); 
  53.                   
  54.                 listAdapter.notifyDataSetInvalidated();    
  55.  
  56.             } 
  57.         }); 
  58.     }; 
  59.  
  60.   
  61.  
  62.     public class ListAdapter extends BaseAdapter { 
  63.  
  64.         ArrayList<ButtonView> arrayList = null
  65.         LayoutInflater inflater; 
  66.         View view; 
  67.         ButtonLayoutHolder buttonLayoutHolder; 
  68.         LinearLayout buttonLayout = null
  69.         TextView buttonText = null
  70.  
  71.         private int selectedPosition = -1;// 选中的位置 
  72.  
  73.         public ListAdapter(ArrayList<ButtonView> buttonListView) { 
  74.             // TODO Auto-generated constructor stub 
  75.             arrayList = buttonListView; 
  76.         } 
  77.  
  78.         @Override 
  79.         public int getCount() { 
  80.             // TODO Auto-generated method stub 
  81.             return arrayList.size(); 
  82.         } 
  83.  
  84.         @Override 
  85.         public Object getItem(int position) { 
  86.             // TODO Auto-generated method stub 
  87.             return arrayList.get(position); 
  88.         } 
  89.  
  90.         @Override 
  91.         public long getItemId(int position) { 
  92.             // TODO Auto-generated method stub 
  93.             return position; 
  94.         } 
  95.  
  96.         public void setSelectedPosition(int position) { 
  97.             selectedPosition = position; 
  98.         } 
  99.  
  100.         @Override 
  101.         public View getView(int position, View convertView, ViewGroup parent) { 
  102.             // TODO Auto-generated method stub 
  103.             inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  104.             view = inflater.inflate(R.layout.button_layout, null, false); 
  105.             buttonLayoutHolder = (ButtonLayoutHolder) view.getTag(); 
  106.  
  107.             if (buttonLayoutHolder == null) { 
  108.                 buttonLayoutHolder = new ButtonLayoutHolder(); 
  109.                 buttonLayoutHolder.buttonLayout = (LinearLayout) view 
  110.                         .findViewById(R.id.LinearLayoutButton); 
  111.                 buttonLayoutHolder.textView = (TextView) view 
  112.                         .findViewById(R.id.TextViewButton); 
  113.                 view.setTag(buttonLayoutHolder); 
  114.             } 
  115.             buttonLayout = buttonLayoutHolder.buttonLayout; 
  116.             buttonText = buttonLayoutHolder.textView; 
  117.             if (selectedPosition == position) { 
  118.                 buttonText.setSelected(true); 
  119.                 buttonText.setPressed(true); 
  120.                 buttonLayout.setBackgroundColor(Color.RED); 
  121.             } else
  122.                 buttonText.setSelected(false); 
  123.                 buttonText.setPressed(false); 
  124.                 buttonLayout.setBackgroundColor(Color.TRANSPARENT);    
  125.  
  126.             } 
  127.  
  128.             buttonText.setTextColor(Color.WHITE); 
  129.             buttonText.setText(arrayList.get(position).textViewId); 
  130.  
  131.             return view; 
  132.  
  133.         } 
  134.  
  135.     }; 
  136.  
  137.  
  138. class ButtonView { 
  139.     int textViewId; 
  140.  
  141.     ButtonView(int tId) { 
  142.         textViewId = tId; 
  143.     } 
  144.  
  145. class ButtonLayoutHolder { 
  146.     LinearLayout buttonLayout; 
  147.     TextView textView; 

  在listView的setOnItemClickListener事件中标记这次选择的Item的下标:listAdapter.setSelectedPosition(arg2);

  然后调用listAdapter.notifyDataSetInvalidated()通知后台重新刷新界面。

  在ListAdapter的getView()方法中,如果是选中的Item则显示背景颜色,如果不是则不显示背景颜色。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1614次
    • 积分:39
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:14篇
    • 译文:0篇
    • 评论:0条