Android Selector

  1. Selector的应用:html

http://www.cnblogs.com/loulijun/archive/2012/04/15/2450312.htmlandroid

使用selector修改TextView中字体的颜色:ide

http://blog.csdn.net/dinglin_87/article/details/7885806 布局

2.这个是另外一种思路字体

http://blog.csdn.net/lo5sea/article/details/6647680this

3.viewholder高亮错乱spa

http://blog.sina.com.cn/s/blog_7040845601017ak5.html.net

关键代码以下:设计

final MyAdapter myAdapter=new MyAdapter(this, data);
  setListAdapter(myAdapter);
  getListView().setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      myAdapter.setSelectItem(position);
       myAdapter.notifyDataSetInvalidated();

   }
  });orm

public class MyAdapter extends BaseAdapter {
  private Context context;
  private List<Map<String, Object>> list;
  private LayoutInflater layoutInflater;
  private int selectItem=-1;
  public MyAdapter(Context context, List<Map<String, Object>> list) {
   this.context = context;
   this.list = list;
   this.layoutInflater = LayoutInflater.from(context);
  }

  @Override
  public int getCount() {
   return list.size();
  }

  @Override
  public Object getItem(int position) {
   return list.get(position);
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   final ViewHolder viewHolder;
   if (convertView == null) {
    convertView = layoutInflater.inflate(R.layout.item, null);
    viewHolder = new ViewHolder();
    viewHolder.linearlayout = (LinearLayout) convertView.findViewById(R.id.linearlayout);
    viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
    viewHolder.title = (TextView) convertView.findViewById(R.id.title);
    convertView.setTag(viewHolder);
   } else {
    viewHolder = (ViewHolder) convertView.getTag();
   }
   viewHolder.img.setBackgroundResource((Integer) list.get(position).get("imgid"));
   viewHolder.title.setText(list.get(position).get("title").toString());
   //若是位置相同则设置背景为黄色
    if (position == selectItem) {
    convertView.setBackgroundColor(Color.YELLOW);
    }
    else {
    convertView.setBackgroundColor(Color.TRANSPARENT);
    }

   return convertView;
  }

  public void setSelectItem(int selectItem) {
   this.selectItem = selectItem;
  }

  class ViewHolder {
   private LinearLayout linearlayout;
   private TextView title;
   private ImageView img;
  }
 }



android:listSelector="#00000000"  :能够使listview 的item默认点击的背景黄色为透明

4.最简单的一种:

 <ListView

        android:id="@+id/road_condition_listview"

        android:layout_width="match_parent"

        android:listSelector="#f00"

        android:layout_height="fill_parent" >

    </ListView>

一个教训:

 listview的item的布局为:一个线性布局中有两个图片,一个文本,设置listview的setOnItemClickListener,跳转的到详情页,如今的问题是:能跳转到详情页,可是点击的时候不是有个默认的选中的背景吗?这个不会显示,好像失去焦点了,设置android:listSelector=“#f00”也没有做用 

 自定义item布局,若是不是透明背景(就是item的根布局设置了bacground),就把原来的效果盖住了。须要本身设计布局的点击变色的效果
5.动态更换view类的背景----StateListDrawable的应用

参考:http://yq135314.iteye.com/blog/1333511

gridview初始化的item的背景色都不一样,点击item有对应的点击后的效果,须要为每个item都写一个selector.xml文件吗?不用!

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
   if (convertView == null) {
    convertView = mLayoutInflater.inflate(R.layout.bottom_gridview_item, null);
   }
   ImageView icon = (ImageView) convertView.findViewById(R.id.bottom_gridview_item_icon);
   TextView lable = (TextView) convertView.findViewById(R.id.bottom_gridview_item_lable);
   icon.setImageResource((int) bottom_listData.get(position).get("icon"));
   lable.setText((String) bottom_listData.get(position).get("lable"));
   //convertView.setBackgroundColor(bottom_bgs[position]);
   TypedArray colorsArrayNormal = getResources().obtainTypedArray(R.array.bg_colors);
   TypedArray colorsArraySelected = getResources().obtainTypedArray(R.array.bg_colors_selected);
   convertView.setBackground(addStateDrawable(MainActivity.this, colorsArrayNormal.getResourceId(position,0), colorsArraySelected.getResourceId(position,0), colorsArraySelected.getResourceId(position,0)));
   //convertView.setBackground(colorsArrayNormal.getDrawable(position));
   return convertView;
  }

 }
  public StateListDrawable addStateDrawable(Context context,  int idNormal, int idPressed, int idFocused) { 
       StateListDrawable sd = new StateListDrawable(); 
       Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal); 
        Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed); 
        Drawable focus = idFocused == -1 ? null : context.getResources().getDrawable(idFocused); 
        //注意该处的顺序,只要有一个状态与之相配,背景就会被换掉 
        //因此不要把大范围放在前面了,若是sd.addState(new[]{},normal)放在第一个的话,就没有什么效果了  
        sd.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focus); 
        sd.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed); 
        sd.addState(new int[]{android.R.attr.state_focused}, focus); 
        sd.addState(new int[]{android.R.attr.state_pressed}, pressed); 
        sd.addState(new int[]{android.R.attr.state_enabled}, normal); 
        sd.addState(new int[]{}, normal); 
        return sd; 
    } 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array name="bg_colors">
        <item >@color/f1</item>
        <item >@color/f2</item>
        <item >@color/f3</item>
        <item >@color/f4</item>
        <item >@color/f5</item>
        <item >@color/f6</item>
    </integer-array>
    <integer-array name="bg_colors_selected">
        <item >@color/f11</item>
        <item >@color/f21</item>
        <item >@color/f31</item>
        <item >@color/f41</item>
        <item >@color/f51</item>
        <item >@color/f61</item>
    </integer-array>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
     <color name="f1">#ecf9ff</color><!--白色 -->   
     <color name="f2">#ffc71c</color><!--淡黄 -->   
     <color name="f3">#048bc3</color><!--浓蓝 -->   
     <color name="f4">#01b5f4</color><!--淡蓝 -->   
     <color name="f5">#f49f10</color><!--浓黄 -->   
     <color name="f6">#91db00</color><!--绿色 -->   
    
     <color name="f11">#aceefa</color><!--白色选中 -->   
     <color name="f21">#aceefa</color><!--淡黄选中 -->   
     <color name="f31">#aceefa</color><!--浓蓝选中 -->   
     <color name="f41">#aceefa</color><!--淡蓝选中-->   
     <color name="f51">#aceefa</color><!--浓黄选中 -->   
     <color name="f61">#aceefa</color><!--绿色选中 -->   
</resources>


6、android LinearLayout设置selector不起做用解决:

设置方法 : android:background="@drawable/fen_selector"

若是只有这个的话,是不起做用的。还必须加上: android:clickable="true"

相关文章
相关标签/搜索