• BaseAdapter使listview设置不同背景图片并添加selector


    前段时间为了实现根据item不同的内容实现不同的背景色google了好久只找到了个隔行换色,通过自定义SimpleAdapter终于实现了此功能,但是定义了selector并没有触发点击效果。今天重新想了一遍终于把这个问题解决了。在自定义的getView里就给每个convertView定义它的背景色和selector。

    [代码]自定义SimpleAdapter     

    01 public class MySimpleAdapter extends BaseAdapter {
    02     private LayoutInflater mInflater;
    03  
    04     private List<HashMap<String, Object>> dataList;
    05  
    06     public MySimpleAdapter(Context context, List<HashMap<String, Object>> data) {
    07         mInflater = LayoutInflater.from(context);
    08         this.dataList = data;
    09     }
    10  
    11     @Override
    12     public int getCount() {
    13         return dataList.size();
    14     }
    15  
    16     @Override
    17     public Object getItem(int position) {
    18         return position;
    19     }
    20  
    21     @Override
    22     public long getItemId(int position) {
    23         return position;
    24     }
    25  
    26     @Override
    27     public View getView(int position, View convertView, ViewGroup parent) {
    28         ViewHolder holder;
    29         if (convertView == null) {
    30             convertView = mInflater.inflate(R.layout.color_list_item, null);
    31             holder = new ViewHolder();
    32             holder.blue = (TextView)convertView.findViewById(R.id.blue_item);
    33             holder.red = (TextView)convertView.findViewById(R.id.red_item);
    34             holder.green = (TextView)convertView.findViewById(R.id.green_item);
    35             holder.yellow = (TextView)convertView.findViewById(R.id.yellow_item);
    36  
    37             convertView.setTag(holder);
    38  
    39         } else {
    40             holder = (ViewHolder)convertView.getTag();
    41  
    42         }
    43         holder.blue.setText((CharSequence)dataList.get(position).get("blue"));
    44         holder.red.setText((CharSequence)dataList.get(position).get("red"));
    45         holder.green.setText((CharSequence)dataList.get(position).get("green"));
    46         holder.yellow.setText((CharSequence)dataList.get(position).get("yellow"));
    47         if ("blue".equals(dataList.get(position).get("status").toString())) {
    48             convertView.setBackgroundResource(R.drawable.item_chang_blue);
    49         } else if ("red".equals(dataList.get(position).get("status").toString())) {
    50             convertView.setBackgroundResource(R.drawable.item_chang_red);
    51         } else if ("green".equals(dataList.get(position).get("status").toString())) {
    52             convertView.setBackgroundResource(R.drawable.flightinfo_item_chang_green);
    53         } else if ("yellow".equals(dataList.get(position).get("status").toString())) {
    54             convertView.setBackgroundResource(R.drawable.flightinfo_item_chang_yellow);
    55         }
    56         return convertView;
    57     }
    58  
    59     @Override
    60     public void notifyDataSetChanged() {
    61         super.notifyDataSetChanged();
    62     }
    63  
    64     class ViewHolder {
    65         TextView blue;
    66         TextView red;
    67         TextView green;
    68         TextView yellow;
    69     }
    70      
    71 }

    2. [代码]选择器item_chang_blue.xml 其他同理     

    01 <?xml version="1.0" encoding="utf-8"?>
    02 <selector
    03   xmlns:android="http://schemas.android.com/apk/res/android">
    04      
    05 <!-- 非触摸模式下获得焦点并单击时的背景图片 --> 
    06  <item
    07     android:state_focused="true"
    08     android:state_pressed="true"
    09  android:drawable= "@drawable/list_item_pressed"/>  
    10 <!-- 触摸模式下单击时的背景图片 -->
    11    
    12   <item 
    13  android:state_focused="false" 
    14     android:state_pressed="true"   
    15     android:drawable="@drawable/list_item_pressed" /> 
    16  
    17  
    18 <!--选中时的图片背景  --> 
    19  <item
    20  android:state_selected="true"
    21     android:drawable="@drawable/list_item_pressed"/> 
    22 <!--获得焦点时的图片背景  --> 
    23  <item
    24     android:state_focused="true"
    25     android:drawable="@drawable/list_item_pressed"/>
    26     <!--默认背景  -->  ​
    27     <item android:drawable="@drawable/status_blue" />
    28 </selector>
    29 ​​
  • 相关阅读:
    数据库
    Activity组件
    view,视图组件
    360导航板式
    service音乐播放
    service
    音乐播放器组件
    获取时间字符串
    JDK服务器存储
    数据库_黑名单练习_完善服务广播
  • 原文地址:https://www.cnblogs.com/fx2008/p/3140268.html
Copyright © 2020-2023  润新知