packagecom.example.selectitemtest;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importandroid.app.Activity;importandroid.content.Context;importandroid.graphics.Color;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.AbsListView;importandroid.widget.AdapterView;importandroid.widget.BaseAdapter;importandroid.widget.ImageView;importandroid.widget.ListView;importandroid.widget.TextView;importandroid.widget.Toast;public class MainActivity extendsActivity {privateListView lv;private List>data;privateMyAdapter adapter;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.lv);//获取将要绑定的数据设置到data中
data =getData();
adapter= new MyAdapter(this);
lv.setAdapter(adapter);
lv.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
lv.setOnItemClickListener(newListView.OnItemClickListener() {
@Overridepublic void onItemClick(AdapterView> arg0, View arg1, intarg2,longarg3) {
Toast.makeText(getApplicationContext(),"click position:"+arg2, Toast.LENGTH_SHORT).show();
adapter.setSelectedItem(arg2);//adapter.notifyDataSetInvalidated();
}
});
}private List>getData()
{
List> list = new ArrayList>();
Mapmap;for(int i=0;i<10;i++)
{
map= new HashMap();
map.put("img", R.drawable.ic_launcher);
map.put("title", "花郎");
map.put("info", "动力源于兴趣...");
list.add(map);
}returnlist;
}ViewHolder
{publicImageView img;publicTextView title;publicTextView info;
}public class MyAdapter extendsBaseAdapter
{private LayoutInflater mInflater = null;private int selectedItem = -1;privateMyAdapter(Context context)
{.mInflater =LayoutInflater.from(context);
}
@Overridepublic intgetCount() {//How many items are in the data set represented by this Adapter.data.size();
}
@Overridepublic Object getItem(intposition) {//Get the data item associated with the specified position in the data set.position;
}public void setSelectedItem(int selectedItem)
{
this.selectedItem =selectedItem;
}
@Overridepublic long getItemId(intposition) {//Get the row id associated with the specified position in the list.position;
}//Get a View that displays the data at the specified position in the data set.//获取一个在数据集中指定索引的视图来显示数据
@Overridepublic View getView(intposition, View convertView, ViewGroup parent) {
ViewHolder holder= null;(convertView == null)
{
holder= newViewHolder();//根据自定义的Item布局加载布局
convertView = mInflater.inflate(R.layout.list_item, null);
holder.img=(ImageView)convertView.findViewById(R.id.img);
holder.title=(TextView)convertView.findViewById(R.id.tv);
holder.info=(TextView)convertView.findViewById(R.id.info);//将设置好的布局保存到缓存中,并将其设置在Tag里,以便后面方便取出Tag
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
holder.img.setBackgroundResource((Integer)data.get(position).get("img"));
holder.title.setText((String)data.get(position).get("title"));
holder.info.setText((String)data.get(position).get("info"));if(position == selectedItem)
{
//convertView.setBackgroundColor(Color.BLUE);
//convertView.setSelected(true);
convertView.setBackgroundResource(R.drawable.all_listentry_left_selected);
}else
{
//convertView.setBackgroundColor(Color.GRAY);
//convertView.setSelected(false);
convertView.setBackgroundResource(R.drawable.lstview);
}returnconvertView;
}
}
}