<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ZhipSoft.com
        冬去春來
            鄭重聲明:本Blog純屬個人學習、工作需要,記錄相關(guān)資料。請不要發(fā)表任何有人身攻擊的言論,謝謝!!www.ZhipSoft.com
    posts - 94,comments - 149,trackbacks - 0

    構(gòu)造自己的adapter,在getView中,設(shè)置顏色,點擊item,刷新listview。

    代碼:

    view plaincopy to clipboardprint?
    import java.util.ArrayList;  
    import java.util.HashMap;  
    import java.util.List;  
    import java.util.Map;  
    import android.app.Activity;  
    import android.content.Context;  
    import android.graphics.Color;  
    import android.os.Bundle;  
    import android.view.LayoutInflater;  
    import android.view.View;  
    import android.view.ViewGroup;  
    import android.widget.AdapterView;  
    import android.widget.BaseAdapter;  
    import android.widget.ListView;  
    import android.widget.TextView;  
    public class MyListView4 extends Activity {  
        private List<Map<String, Object>> mData;      
        ListView setlistViewLeft;  
        MyAdapter adapter;    
          
        @Override 
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.list);  
              
            mData = getData();        
            setlistViewLeft = (ListView)findViewById(R.id.listleft);   
              
            adapter = new MyAdapter(this);  
            setlistViewLeft.setAdapter(adapter);  
            setlistViewLeft.setOnItemClickListener(mLeftListOnItemClick);  
        }  
        private List<Map<String, Object>> getData() {  
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
            Map<String, Object> map = new HashMap<String, Object>();  
            map.put("title", "G1");  
            map.put("info", "google 1");  
            list.add(map);  
            map = new HashMap<String, Object>();  
            map.put("title", "G2");  
            map.put("info", "google 2");  
            list.add(map);  
            map = new HashMap<String, Object>();  
            map.put("title", "G3");  
            map.put("info", "google 3");  
            list.add(map);  
              
            map = new HashMap<String, Object>();  
            map.put("title", "G4");  
            map.put("info", "google 4");  
            list.add(map);  
              
            map = new HashMap<String, Object>();  
            map.put("title", "G5");  
            map.put("info", "google 5");  
            list.add(map);  
            return list;  
        }  
        AdapterView.OnItemClickListener mLeftListOnItemClick = new AdapterView.OnItemClickListener() {  
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {  
                  
                adapter.setSelectItem(arg2);  
                adapter.notifyDataSetInvalidated();  
    //          adapter.notifyDataSetChanged();  
            }  
              
        };  
        /** 
         * listview中點擊按鍵彈出對話框 
         */ 
        public final class ViewHolder {  
            public TextView titleText;  
            public TextView infoText;         
        }  
        public class MyAdapter extends BaseAdapter {  
            private LayoutInflater mInflater;  
            public MyAdapter(Context context) {  
                this.mInflater = LayoutInflater.from(context);  
            }  
            public int getCount() {  
                // TODO Auto-generated method stub  
                return mData.size();  
            }  
            public Object getItem(int arg0) {  
                // TODO Auto-generated method stub  
                return mData.get(arg0);  
            }  
            public long getItemId(int arg0) {  
                // TODO Auto-generated method stub  
                return arg0;  
            }  
            public View getView(int position, View convertView, ViewGroup parent) {  
                ViewHolder holder = null;  
                if (convertView == null) {  
                    holder = new ViewHolder();  
                    convertView = mInflater.inflate(R.layout.leftview, null);  
                    holder.titleText = (TextView) convertView.findViewById(R.id.titleleftlist);  
                    holder.infoText = (TextView) convertView.findViewById(R.id.infoleftlist);  
                      
                    convertView.setTag(holder);           
                } else {  
                    holder = (ViewHolder) convertView.getTag();  
                }  
                  
                holder.titleText.setText((String) mData.get(position).get("title"));  
                holder.infoText.setText((String) mData.get(position).get("info"));            
                  
                if (position == selectItem) {  
                    convertView.setBackgroundColor(Color.RED);  
                }   
                else {  
                    convertView.setBackgroundColor(Color.TRANSPARENT);  
                }     
                  
                //convertView.getBackground().setAlpha(80);   
                  
                return convertView;  
            }  
            public  void setSelectItem(int selectItem) {  
                 this.selectItem = selectItem;  
            }  
            private int  selectItem=-1;  
        }  

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    public class MyListView4 extends Activity {
     private List<Map<String, Object>> mData; 
     ListView setlistViewLeft;
     MyAdapter adapter; 
     
     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.list);
      
      mData = getData();  
      setlistViewLeft = (ListView)findViewById(R.id.listleft);
      
      adapter = new MyAdapter(this);
      setlistViewLeft.setAdapter(adapter);
      setlistViewLeft.setOnItemClickListener(mLeftListOnItemClick);
     }
     private List<Map<String, Object>> getData() {
      List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("title", "G1");
      map.put("info", "google 1");
      list.add(map);
      map = new HashMap<String, Object>();
      map.put("title", "G2");
      map.put("info", "google 2");
      list.add(map);
      map = new HashMap<String, Object>();
      map.put("title", "G3");
      map.put("info", "google 3");
      list.add(map);
      
      map = new HashMap<String, Object>();
      map.put("title", "G4");
      map.put("info", "google 4");
      list.add(map);
      
      map = new HashMap<String, Object>();
      map.put("title", "G5");
      map.put("info", "google 5");
      list.add(map);
      return list;
     }
     AdapterView.OnItemClickListener mLeftListOnItemClick = new AdapterView.OnItemClickListener() {
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
       
       adapter.setSelectItem(arg2);
       adapter.notifyDataSetInvalidated();
    //   adapter.notifyDataSetChanged();
      }
      
     };
     /**
      * listview中點擊按鍵彈出對話框
      */
     public final class ViewHolder {
      public TextView titleText;
      public TextView infoText;  
     }
     public class MyAdapter extends BaseAdapter {
      private LayoutInflater mInflater;
      public MyAdapter(Context context) {
       this.mInflater = LayoutInflater.from(context);
      }
      public int getCount() {
       // TODO Auto-generated method stub
       return mData.size();
      }
      public Object getItem(int arg0) {
       // TODO Auto-generated method stub
       return mData.get(arg0);
      }
      public long getItemId(int arg0) {
       // TODO Auto-generated method stub
       return arg0;
      }
      public View getView(int position, View convertView, ViewGroup parent) {
       ViewHolder holder = null;
       if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater.inflate(R.layout.leftview, null);
        holder.titleText = (TextView) convertView.findViewById(R.id.titleleftlist);
        holder.infoText = (TextView) convertView.findViewById(R.id.infoleftlist);
        
        convertView.setTag(holder);   
       } else {
        holder = (ViewHolder) convertView.getTag();
       }
       
       holder.titleText.setText((String) mData.get(position).get("title"));
       holder.infoText.setText((String) mData.get(position).get("info"));   
       
       if (position == selectItem) {
        convertView.setBackgroundColor(Color.RED);
       }
       else {
        convertView.setBackgroundColor(Color.TRANSPARENT);
       } 
       
       //convertView.getBackground().setAlpha(80);
       
       return convertView;
      }
      public  void setSelectItem(int selectItem) {
        this.selectItem = selectItem;
      }
      private int  selectItem=-1;
     }
    }


    android 自定義adapter extends BaseAdapter,做有自己布局的listView
    或:

    siteListView.setOnItemClickListener(new OnItemClickListener() {
       @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            for(int i=0;i<parent.getCount();i++){
                View v=parent.getChildAt(parent.getCount()-1-i);
                if (position == i) {
                    v.setBackgroundColor(Color.RED);
                } else {
                    v.setBackgroundColor(Color.TRANSPARENT);
                }
            }
        }
    });



            本Blog純屬個人學習、工作需要,記錄相關(guān)資料。請不要發(fā)表任何有人身攻擊的言論,謝謝! www.zhipsoft.cn
    posted on 2011-01-25 17:10 ZhipSoft 閱讀(12271) 評論(1)  編輯  收藏 所屬分類: AndRoid

    FeedBack:
    # re: [摘]android listview選中某一行,成選中狀態(tài)顏色高亮顯示
    2011-09-03 15:02 | qinglfxy
    這樣的都不會達到理想的效果,點擊某頁的某一項,顏色改變,其他頁也會有一項的顏色跟著改變的。  回復  更多評論
      
    主站蜘蛛池模板: 在线视频网址免费播放| 久久精品国产亚洲AV无码偷窥| 亚洲va在线va天堂va四虎| 夜夜爽妓女8888视频免费观看| 成熟女人特级毛片www免费| 亚洲av无码国产综合专区 | 一级成人生活片免费看| 国产高清在线免费视频| 亚洲第一成年免费网站| 日韩毛片免费在线观看| 亚洲丶国产丶欧美一区二区三区| va亚洲va日韩不卡在线观看| 精品免费AV一区二区三区| 久久久久久久免费视频| 亚洲三级视频在线观看| 午夜亚洲国产成人不卡在线| 无码AV片在线观看免费| 亚洲国产亚洲片在线观看播放| 无码国产精品一区二区免费式影视| 无人视频免费观看免费视频| 久久久青草青青国产亚洲免观| a一级爱做片免费| 亚洲产国偷V产偷V自拍色戒| 美丽姑娘免费观看在线观看中文版 | 午夜在线免费视频 | 老汉精品免费AV在线播放| 精品亚洲成a人在线观看| 免费人成视频在线观看不卡| 午夜在线免费视频| 中文字幕在线日亚洲9| 亚洲国产精品13p| 玖玖在线免费视频| 国产色在线|亚洲| 亚洲?V乱码久久精品蜜桃 | 亚洲精品无码日韩国产不卡?V| 中国黄色免费网站| 亚洲色偷偷av男人的天堂| 大学生高清一级毛片免费| 亚洲第一视频在线观看免费| 亚洲日本一线产区和二线 | 国产麻豆免费观看91|