ListView的item选中效果
有时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
ListView的item选中效果的更多相关文章
- Windows 10 UWP开发:如何去掉ListView默认的选中效果
原文:Windows 10 UWP开发:如何去掉ListView默认的选中效果 开发UWP的时候,很多人会碰到一个问题,就是ListView在被数据绑定之后经常有个默认选中的效果,就像这样: 而且它不 ...
- 改变listview中item选中时文字的颜色
摘要 当listview的某个item选中时,默认有个选中的高亮显示,如果你要自定义选中时的高亮显示效果,可以在listview中设置属性 android:listSelector="@dr ...
- 如何禁用ListView中的选中效果
有的时候,我们希望ListView没有选中效果. 导致选中效果出现可能有两方面的原因: 1.每一行View自身可能有选中效果,可以将它的背景设置为透明 2.ListView有一个listSelecto ...
- listview 拖动item效果实现
listview 拖动item效果实现 效果图如下: 拖拽前: 拖拽后: 首先参考源码中:TouchInterceptor 类,该类会在下面给出: 第一步:主类: /** * */ packag ...
- ListView 实现带有Filpper效果的左右滑动删除 Item
ListView 实现带有Filpper效果的左右滑动删除 Item 的实现最基本的方法还是 对 Listview 的继承重写 .然后是在删除过程中加入 TranslateAnimation 滑动事 ...
- uwp,GridView、ListView取消选中效果
在SelectionChanged事件中,添加两行代码,取消点击Item后的选中效果 void GridViewSelectionChanged(object sender, SelectionCha ...
- 关于listView的item失去焦点不能点击 Item中包含Button 导致抢占焦点
今天发现一个问题.listView的item点击以后进入到下一个页面,下个页面有个返回按钮,直接返回回去以后点击事件不能触发,滑动或者重新打开这个listView,就可以达到原来的效果.后来发现是因为 ...
- 奇葩问题:ListView中Item与Item中的Button不能单击问题
android中ListView是一个经常要用到的一个组件,用到该组件时经常会碰到ListView的Item和Item中的Button不能单击的问题. 本人在使用时同样也遇到过这样的情况,共有三种情况 ...
- Android 用HorizontalScrollView实现ListView的Item滑动删除 ,滑动错乱 冲突
用HorizontalScrollView实现类似微信的滑动删除 测试于:Android2.2+ 对于Android来说按键操作已经在减少,越来越多的手势操作层出不穷,今天介绍一款LIstView的I ...
随机推荐
- Go语言类型switch
switch还可以用于判断变量类型.使用方式为T.(type),即在变量后加上.(type).见代码: package main import ( "fmt" ) func mai ...
- 发布项目MVC4-EF6.0出错
出错: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFram ...
- sharepoint 2010 切换域
前提: 现在已经有一个sharepoint 2010的环境,当前域为contosoA.com,有个需求需要将这个域切换到域contosoB.com.下面是成功操作的步骤. 1.数据最重要 备份所有数据 ...
- 11.Warning (332060): Node: pi_fck3p was determined to be a clock but was found without an associated clock assignment.
解释及措施:(1):这个信号是不是你期望的时钟信号?还是被综合器误将普通信号综合成了时钟信号?有没有在代码中用过这个信号的上升沿/下降沿? (2):如果是期望的时钟信号,那么是否有可能调整管脚位置约束 ...
- 在使用SQLite插入数据时出现乱码的解决办法
在VC++中通过sqlite3.dll接口对sqlite数据库进行操作,包括打开数据库,插入,查询数据库等,如果操作接口输入参数包含中文字符,会导致操作异常.例如调用sqlite3_open打开数 ...
- 最小二乘法(least squares method)
一.背景 号到北大去听hulu的讲座<推荐系统和计算广告在视频行业应用>,想到能见到传说中的项亮大神,特地拿了本<推荐系统实践>求签名.讲座开始,主讲人先问了下哪些同学有机器学 ...
- 阿里云无线&前端团队是如何基于webpack实现前端工程化的
背景 前端经历了初期的野蛮生长(切图,写简单的特效)——为了兼容浏览器兼容性而出现的各种类库(JQUERY,YUI等——mv*(饱暖思淫欲,代码多了,也就想到怎样组织代码结构,backbone,ang ...
- ffmpeg 按时间戳读取文件 -re
ffmpeg读取文件有两种方式:一种是直接读取,文件被迅速读完;一种是按时间戳读取.一般都是按时间戳读取文件, 命令行加入-re,表示按时间戳读取文件,在ffmpeg_opt.c 中可以看到re对应的 ...
- Dreamweaver 8
Dreamweaver 8 附注册码:WPD800-56030-83832-97910
- 【BZOJ】【3907】网格
组合数学/python 3907: 网格 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 162 Solved: 76[Submit][Status][ ...