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

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

ListView是通过实现Checkable接口来处理单选模式的,这要求Item的视图实现Checkable接口,创建ChoiceListItemView类来实现该接口,ListView选中某个Item时,会调用ChoiceListItemView类的setChecked的方法:
 
自定义Adapter
    1. package com.centrvideo.parkapp.adapter;
    2. import java.util.List;
    3. import android.content.Context;
    4. import android.view.View;
    5. import android.view.ViewGroup;
    6. import android.widget.ListView;
    7. import com.centrvideo.parkapp.demain.ExportInfo;
    8. public class ExportAdapter extends MyBaseAdapter<ExportInfo, ListView> {
    9. public ExportAdapter(Context context, List<ExportInfo> list) {
    10. super(context, list);
    11. }
    12. @Override
    13. public View getView(int position, View covertView, ViewGroup group) {
    14. ChoiceListItemView view;
    15. if (covertView == null) {
    16. view = new ChoiceListItemView(context, null);
    17. } else {
    18. view = (ChoiceListItemView) covertView;
    19. }
    20. ExportInfo exportInfo = list.get(position);
    21. view.setData(exportInfo);
    22. return view;
    23. }
    24. }
2、自定义ListView的item视图
    1. package com.centrvideo.parkapp.adapter;
    2. import android.content.Context;
    3. import android.util.AttributeSet;
    4. import android.view.LayoutInflater;
    5. import android.view.View;
    6. import android.widget.CheckBox;
    7. import android.widget.Checkable;
    8. import android.widget.ImageView;
    9. import android.widget.LinearLayout;
    10. import android.widget.TextView;
    11. import com.centrvideo.parkapp.R;
    12. import com.centrvideo.parkapp.demain.ExportInfo;
    13. import com.centrvideo.parkapp.util.ImageLoaderUtils;
    14. import com.lidroid.xutils.ViewUtils;
    15. import com.lidroid.xutils.view.annotation.ViewInject;
    16.  
    17. public class ChoiceListItemView extends LinearLayout implements Checkable {
    18.  
    19. @ViewInject(R.id.listview_export_image)
    20. private ImageView listview_export_image;
    21. @ViewInject(R.id.listview_export_entrytime)
    22. private TextView listview_export_entrytime;
    23. @ViewInject(R.id.listview_export_number)
    24. private TextView listview_export_number;
    25. @ViewInject(R.id.listview_entry_time)
    26. private TextView listview_entry_time;
    27. @ViewInject(R.id.cb_export)
    28. public CheckBox selectBtn;
    29. private ImageLoaderUtils imageLoaderUtils;
    30. public ChoiceListItemView(Context context, AttributeSet attrs) {
    31. super(context, attrs);
    32. LayoutInflater inflater = LayoutInflater.from(context);
    33. View v = inflater.inflate(R.layout.listview_export, this, true);
    34. ViewUtils.inject(v);
    35. imageLoaderUtils = ImageLoaderUtils.newInstance();
    36. }
    37.  
    38. public void setData(ExportInfo exportInfo) {
    39. imageLoaderUtils.loadImage(exportInfo.getEntryimg(),
    40. listview_export_image, R.drawable.cell_hold);
    41. listview_export_entrytime.setText("入口时间:"
    42. + exportInfo.getEntrytime() + "");
    43. listview_export_number.setText("车牌号码:"
    44. + exportInfo.getPlatenumber() + "");
    45. listview_entry_time.setText("位置:" + exportInfo.getGatewayname()
    46. + "");
    47. }
    48.  
    49. @Override
    50. public boolean isChecked() {
    51. return selectBtn.isChecked();
    52. }
    53.  
    54. @Override
    55. public void setChecked(boolean checked) {
    56. selectBtn.setChecked(checked);
    57. //根据是否选中来选择不同的背景图片
    58. if (checked) {
    59. selectBtn.setBackgroundResource(R.drawable.cheliangduibi_queding);
    60. } else {
    61. selectBtn.setBackgroundResource(0);
    62. }
    63. }
    64.  
    65. @Override
    66. public void toggle() {
    67. selectBtn.toggle();
    68. }
    69.  
    70. }
3、Activity中调用:
  1. //启用单选模式
  2. listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  3.  
  4. //获得选中结果;通过listView.getCheckedItemPosition();//活动被选中的位置
  5. case R.id.tv_titlebar_right:
  6. if (CommonUtil.isFastDoubleClick()) {
  7. return;
  8. }
  9.  
  10. int selectPosition = listView.getCheckedItemPosition();
  11. CommonUtil.StartToast(ExportSureListActivity.this,"被选中的位置:"+selectPosition);
  12. ExportInfo exportInfo = list1.get(selectPosition-1);//注意这里需要减1
  13. CommonUtil.StartToast(ExportSureListActivity.this,"被选中的位置:"+exportInfo.toString());
  14. // intent = new Intent(ExportSureListActivity.this,
  15. // ChargeActivity.class);
  16. // startActivity(intent);
  17. break;
  18. }

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. java中处理字符编码(网页与数据库)(转)

    首先声明一下,此文章时从网上转载的.如下的某些方法是确实管用,但是从中发现了有一点不足,就是原文笔者没考虑使用不同Web Server时出现的情况,比如文章里我用红色字体画出来的部分代码在Tomcat ...

  2. Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils

    1.错误叙述性说明 2014-7-10 23:12:23 org.apache.catalina.core.StandardContext filterStart 严重: Exception star ...

  3. 前端构建工具gulp

    前端构建工具gulp使用   前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gul ...

  4. 排列组合相关算法 python

    获取指定长度得全部序列 通过事件来表述这个序列,即n重伯努利实验(二项分布)的全部可能结果.比如时间a表示为: a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 假设每次实验为从 ...

  5. Lucene40SkipListWriter

    多级跳跃表是保存在tim文件里的. tip是term index,tim是term dictionary.记忆方法是,p是pointer因此是term index. 这个类会保存多个level的las ...

  6. LinearLayout具体解释一:LinearLayout的简单介绍

    LinearLayout,中文意思是线性布局.假设你是初学android的,肯定会非常困惑"啥叫布局",啥又叫"线性布局"呢. 有的时候,我尝试用官方的语言去解 ...

  7. HIPO图

    HIPO图(Hierarchy Plus Input/Processing/Output)是表示软件结构的一种图形工具.以模块分解的层次性以及模块内部输入.处理.输出三大基本部分为基础建立的.它由两部 ...

  8. RH133读书笔记(2)-Lab 2 Working with packages

    Lab 2 Working with packages Goal: To gain working experience with package management System Setup: A ...

  9. List、Map和Set实现类

    List.Map和Set实现类 1.List实现类 (1)ArrayList (2)Vector 2.Map实现类 (1)HashMap (2)Hashtable 3.Set实现类 (1)HashSe ...

  10. [LeetCode53]Maximum Subarray

    问题: Find the contiguous subarray within an array (containing at least one number) which has the larg ...