android listview item选中,Android UI设计--ListView的item选中效果

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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值