有时app会需要点击某个item并实现选中的效果,例如做pad时用Fragment实现的左侧列表右侧内容的效果,点击左侧某一个item后会高亮选中

有时简单的使用setSelected(boolean b)或setSelection(int position)会不成功,需要重写Adapter,并在getView中进行处理

package com.example.selectitemtest;

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.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
private ListView lv;
private List<Map<String, Object>> data;
private MyAdapter adapter;
@Override
protected void onCreate(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(new ListView.OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), "click position:"+arg2, Toast.LENGTH_SHORT).show();
adapter.setSelectedItem(arg2);
//adapter.notifyDataSetInvalidated();
}
});
}
private List<Map<String, Object>> getData()
{
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
for(int i=0;i<10;i++)
{
map = new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("title", "花郎");
map.put("info", "动力源于兴趣...");
list.add(map);
}
return list;
} //ViewHolder静态类
static class ViewHolder
{
public ImageView img;
public TextView title;
public TextView info;
} public class MyAdapter extends BaseAdapter
{
private LayoutInflater mInflater = null;
private int selectedItem = -1;
private MyAdapter(Context context)
{
//根据context上下文加载布局,这里的是Demo17Activity本身,即this
this.mInflater = LayoutInflater.from(context);
} @Override
public int getCount() {
//How many items are in the data set represented by this Adapter.
//在此适配器中所代表的数据集中的条目数
return data.size();
} @Override
public Object getItem(int position) {
// Get the data item associated with the specified position in the data set.
//获取数据集中与指定索引对应的数据项
return position;
} public void setSelectedItem(int selectedItem)
{
this.selectedItem =
selectedItem;
}
@Override
public long getItemId(int position) {
//Get the row id associated with the specified position in the list.
//获取在列表中与指定索引对应的行id
return position;
} //Get a View that displays the data at the specified position in the data set.
//获取一个在数据集中指定索引的视图来显示数据
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
//如果缓存convertView为空,则需要创建View
if(convertView == null)
{
holder = new ViewHolder();
//根据自定义的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);
}
return convertView;
} }
}

代码中红色标注处就是重点,lv.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);这句话必须要加

Defines the choice behavior for the List. By default, Lists do not have any choice behavior (CHOICE_MODE_NONE). By setting the choiceMode to CHOICE_MODE_SINGLE, the List allows up to one item to be in a chosen state. By setting the choiceMode to CHOICE_MODE_MULTIPLE, the list allows any number of items to be chosen.

实现效果如下

转自:http://www.cnblogs.com/loulijun/archive/2013/02/17/2914122.html

最新文章

  1. WebService的开发、部署、调用
  2. CentOS 6.5下Zabbix的安装配置
  3. 联想G480笔记本安装系统
  4. jQuery小例子
  5. 分享吉林大学机械科学与工程学院,zhao jun 博士的Halcon学习过程及知识分享
  6. 尝试Hexo
  7. 考虑virtual函数以外的其它选择
  8. 使用“bulk insert ”进行批量插入数据
  9. ASP.net+SQL server2008简单的数据库增删改查 VS2012
  10. Android经常使用UI组件 - TextView
  11. 前端基本知识(三):JS的闭包理解
  12. 开源API测试工具 Hitchhiker v0.5更新 - 完善细节
  13. 360搜索引擎so自动收录php改写方案——适合phpcms等cms
  14. 痞子衡嵌入式:飞思卡尔Kinetis系列MCU启动那些事(2)- KBOOT形态(ROM/Bootloader/Flashloader)
  15. 算法笔记--次小生成树 &amp;&amp; 次短路 &amp;&amp; k 短路
  16. 非root用户安装python3
  17. Math.max.apply(null,arr)求最大值
  18. 16.3 authguard 通过routing path控制显示URL 通过ngif显示和隐藏
  19. zabbix监控常见系统报错
  20. js-ES6学习笔记-async函数

热门文章

  1. Linux下使用Magent+Memcached缓存服务器集群部署
  2. oracle11g 新特性 - rman自动备份控制文件延迟
  3. object sender ,EventArs e
  4. [SRH.Docker] HBase Java 第一天学习记录
  5. IOS中 如何去除Tabview里面cell之间的下划线
  6. ExtJS 提示
  7. 初探Xamarin
  8. [shell基础]——join命令
  9. SQL Server2008 无法连接到 local
  10. Javascript Date Format