ListView单选的实现总结
来源:互联网 发布:php源码后门检测 编辑:程序博客网 时间:2024/06/07 14:51
今天在智能停车场项目中需要实现PullToRefreshListView的单选功能,考虑到分页,刷新等,以前的实现方式是采用自己维护一个集合保存选中位置的选中状态,但这个方式比较繁琐,今天采用了listview的单选模式
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);来实现:
ListView是通过实现Checkable接口来处理单选模式的,这要求Item的视图实现Checkable接口,创建ChoiceListItemView类来实现该接口,ListView选中某个Item时,会调用ChoiceListItemView类的setChecked的方法:
自定义Adapter
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视图
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; }
来自为知笔记(Wiz)
0 0
- ListView单选的实现总结
- listview实现单选
- CheckedTextView实现ListView的单选效果
- Listview实现简单的单选功能
- Android实现listView的单选功能
- listview的单选
- ListView实现RaidoButton单选
- ListView实现单选(一)
- 实现listview单选效果
- listview+checkbox实现单选
- Listview+RadioButton实现单选
- android listView实现单选
- ListView实现单选功能
- Android ListView实现单选
- listview的单选/多选模式的实现
- 【Android】ListView的单选、多选的实现
- 实现单选的字体变色效果的listview
- ListView实现CheckBox的单选与多选
- 为什么数学家、统计学家和机器学习专家会用不同方式解决问题?
- 我的架构经验小结(一)—— 常用的架构模型
- poj 3279
- block语句块
- linux常用命令(43):traceroute 命令
- ListView单选的实现总结
- 关于xcode不同版本打开相同工程问题
- 理解Objective-C中的类与对象
- 机房收费系统(合作版)总结——技术篇(二)
- 正则表达式学习笔记
- 数字在排序数组中出现的次数
- 配置vim Python IDE 开发环境
- 如何绘制方形渐开线
- Symmetric Tree