ListView单选的实现总结(转)

 

          今天在智能停车场项目中需要实现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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值