ZhipSoft.com
    冬去春来
        郑重声明:本Blog纯属个人学习、工作需要,记录相关资料。请不要发表任何有人身攻击的言论,谢谢!!www.ZhipSoft.com
posts - 94,comments - 149,trackbacks - 0

构造自己的adapter,在getView中,设置颜色,点击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纯属个人学习、工作需要,记录相关资料。请不要发表任何有人身攻击的言论,谢谢! www.zhipsoft.cn
posted on 2011-01-25 17:10 ZhipSoft 阅读(12254) 评论(1)  编辑  收藏 所属分类: AndRoid

FeedBack:
# re: [摘]android listview选中某一行,成选中状态颜色高亮显示
2011-09-03 15:02 | qinglfxy
这样的都不会达到理想的效果,点击某页的某一项,颜色改变,其他页也会有一项的颜色跟着改变的。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: