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"

相關文章
相關標籤/搜索