今天在智能停车场项目中需要实现PullToRefreshListView的单选功能,考虑到分页,刷新等,以前的实现方式是采用自己维护一个集合保存选中位置的选中状态,但这个方式比较繁琐,今天采用了listview的单选模式

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);来实现:

ListView是通过实现Checkable接口来处理单选模式的,这要求Item的视图实现Checkable接口,创建ChoiceListItemView类来实现该接口,ListView选中某个Item时,会调用ChoiceListItemView类的setChecked的方法:
 
自定义Adapter
  1. package com.centrvideo.parkapp.adapter;
    import java.util.List;
    import android.content.Context;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListView;
    import com.centrvideo.parkapp.demain.ExportInfo;
    public class ExportAdapter extends MyBaseAdapter<ExportInfo, ListView> {
    public ExportAdapter(Context context, List<ExportInfo> list) {
    super(context, list);
    }
    @Override
    public View getView(int position, View covertView, ViewGroup group) {
    ChoiceListItemView view;
    if (covertView == null) {
    view = new ChoiceListItemView(context, null);
    } else {
    view = (ChoiceListItemView) covertView;
    }
    ExportInfo exportInfo = list.get(position);
    view.setData(exportInfo);
    return view;
    }
    }
2、自定义ListView的item视图
  1. package com.centrvideo.parkapp.adapter;
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.CheckBox;
    import android.widget.Checkable;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import com.centrvideo.parkapp.R;
    import com.centrvideo.parkapp.demain.ExportInfo;
    import com.centrvideo.parkapp.util.ImageLoaderUtils;
    import com.lidroid.xutils.ViewUtils;
    import com.lidroid.xutils.view.annotation.ViewInject; public class ChoiceListItemView extends LinearLayout implements Checkable { @ViewInject(R.id.listview_export_image)
    private ImageView listview_export_image;
    @ViewInject(R.id.listview_export_entrytime)
    private TextView listview_export_entrytime;
    @ViewInject(R.id.listview_export_number)
    private TextView listview_export_number;
    @ViewInject(R.id.listview_entry_time)
    private TextView listview_entry_time;
    @ViewInject(R.id.cb_export)
    public CheckBox selectBtn;
    private ImageLoaderUtils imageLoaderUtils;
    public ChoiceListItemView(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.listview_export, this, true);
    ViewUtils.inject(v);
    imageLoaderUtils = ImageLoaderUtils.newInstance();
    } public void setData(ExportInfo exportInfo) {
    imageLoaderUtils.loadImage(exportInfo.getEntryimg(),
    listview_export_image, R.drawable.cell_hold);
    listview_export_entrytime.setText("入口时间:"
    + exportInfo.getEntrytime() + "");
    listview_export_number.setText("车牌号码:"
    + exportInfo.getPlatenumber() + "");
    listview_entry_time.setText("位置:" + exportInfo.getGatewayname()
    + "");
    } @Override
    public boolean isChecked() {
    return selectBtn.isChecked();
    } @Override
    public void setChecked(boolean checked) {
    selectBtn.setChecked(checked);
    //根据是否选中来选择不同的背景图片
    if (checked) {
    selectBtn.setBackgroundResource(R.drawable.cheliangduibi_queding);
    } else {
    selectBtn.setBackgroundResource(0);
    }
    } @Override
    public void toggle() {
    selectBtn.toggle();
    } }
3、Activity中调用:
//启用单选模式
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); //获得选中结果;通过listView.getCheckedItemPosition();//活动被选中的位置
case R.id.tv_titlebar_right:
if (CommonUtil.isFastDoubleClick()) {
return;
} int selectPosition = listView.getCheckedItemPosition();
CommonUtil.StartToast(ExportSureListActivity.this,"被选中的位置:"+selectPosition);
ExportInfo exportInfo = list1.get(selectPosition-1);//注意这里需要减1
CommonUtil.StartToast(ExportSureListActivity.this,"被选中的位置:"+exportInfo.toString());
// intent = new Intent(ExportSureListActivity.this,
// ChargeActivity.class);
// startActivity(intent);
break;
}

http://www.cnblogs.com/xiaoxiao-study/p/a1e1561444542d99b70e781be488f35e.html

ListView单选的实现总结(转)的更多相关文章

  1. listView 单选实现

    上一篇知道可以使用android自带的listview的chiocemode的单选模式实现.但那个布局是系统自带的checkedTextView,有时候我们需要自己实现布局,那么下面我们开始实现   ...

  2. ListView 自己定义BaseAdapter实现单选打勾(无漏洞)

    (假设须要完整demo,请评论留下邮箱) (眼下源代码已经不发送.假设须要源代码,加qq316701116.不喜勿扰) 近期由于一个项目的原因须要自己定义一个BaseAdapter实现ListVIew ...

  3. Android录屏命令、Android录Gif、Android录视频

    NoHttp开源地址:https://github.com/yanzhenjie/NoHttp NoHttp具体使用文档已公布,你想知道的全都有,请点我移步! 版权声明:转载请注明本文转自严振杰的博客 ...

  4. Android在listview添加checkbox实现单选多选操作问题(转)

    转自:http://yangshen998.iteye.com/blog/1310183 在Android某些开发需求当中,有时候需要在listveiw中加入checkbox实现单选,多选操作.表面上 ...

  5. 本地数据下,radiobutton和图片组合,利用adapter+listview进行单选

    浮生偷得半日闲,等接口定义的过程中,重新复习下adapter+listview实现单选的方法 主界面 <RelativeLayout xmlns:android="http://sch ...

  6. ListView 实现多选/单选

    http://blog.csdn.net/ljfbest/article/details/40685327 ListView自身带了单选.多选模式,可通过listview.setChoiceMode来 ...

  7. 【转】ListView与RadioButton组合——自定义单选列表

    原文网址:http://blog.csdn.net/checkin001/article/details/11519131 Android自带的RadioButton单选框只支持添加文字,我们自己写A ...

  8. ListView与RadioButton组合——自定义单选列表

      标签: radiobuttonlistviewandroidlayout 2013-09-10 11:13 19396人阅读 评论(8) 收藏 举报  分类: Android(19)  版权声明: ...

  9. C# winform项目中ListView控件使用CheckBoxes属性实现单选功能

    C# winform项目中ListView控件使用CheckBoxes属性实现单选功能 在做项目时需要使用ListView控件的CheckBoxes属性显示,还要在点击行时自动选中CheckBoxes ...

随机推荐

  1. d3d纹理参数

    D3DTEXTURESTAGESTATETYPE 纹理状态定义了一个多层次的复杂的操作质感.一些采样工作状态设置顶点,有些组像素操作. 纹理层次状态可以快速保存并返回状态. typedef enum ...

  2. Mvc后台接收 参数

    @Html.TextAreaFor(m => m.Emps, new { @class = "easyui-validatebox", @style = "heig ...

  3. spring mvc综合easyui点击上面菜单栏中的菜单项问题

    采用easyui的tree报错发生的背景后,会弹出一个窗口,有一个问题是,当你点击顶部   解决方案,如下面(运用easyui1.36): /home/cyz/workspace/hb_manager ...

  4. 邮箱password复位图

    blog宗旨:用图说话 辅助文字说明: 长处:用户系统完毕接口部分.界面留给业务自行设计. 缺点:邮箱发送邮件定制模板,没有完毕松耦合. 改进点:邮箱的准备发送邮件模板. 版权声明:本文博客原创文章, ...

  5. 交叉编译libxml2

    请勿用于商业用途,转载请注明出处! xml的优势就是可以方便的管理配置项,libxml2是c语言实现的xml管理库,眼下项目须要ARM下的版本号,libxml2编译过程例如以下: 0.准备工作 下载地 ...

  6. 解决adb server is out of date. killing...问题

    在运行 adb 命令时出现了例如以下提示: adb server is out of date.  killing... 导致 adb 无法正常启动,更无法运行其它命令. 有问题怎么办?百度呗.查了查 ...

  7. Android学习路径(两)项目文件本身使用场景和文件演示

    ios讨论群1群:135718460  1.src文件:java源码存放文件夹 2.gen 文件:自己主动生成全部由android开发工具自己主动生成的文件,文件夹中最重要的就是R.java文件,这个 ...

  8. Eclipse建筑物SSH(struts-2.2.3 + spring-2.5.6 + hibernate-3.6.8)相框-随着源代码

    一直想自己搭建一个ssh框架,这次因为编写demo的须要,就亲手搭建了一下,并逐步測试!以下进入正题: 创建Struts项目 整合步骤: 1,在Eclipse中创建一个DynamicWeb Proje ...

  9. Linux 于 shell 变数 $#,$@,$0,$1,$2 含义解释:

    变量说明: $$ Shell自己PID(ProcessID) $! Shell背景上次执行Process的PID $? 命令的结束代码(返回值) $- 使用Set命令设定的Flag一览 $* 全部參数 ...

  10. crm操作观点

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Crm.Sdk.Messages; /// <summary> ...