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;
        }  

来自为知笔记(Wiz)

时间: 2024-08-10 19:07:00

ListView单选的实现总结的相关文章

ListView单选的实现总结(转)

今天在智能停车场项目中需要实现PullToRefreshListView的单选功能,考虑到分页,刷新等,以前的实现方式是采用自己维护一个集合保存选中位置的选中状态,但这个方式比较繁琐,今天采用了listview的单选模式 listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);来实现: ListView是通过实现Checkable接口来处理单选模式的,这要求Item的视图实现Checkable接口,创建ChoiceListItemView类来实现该

超简单的listview单选模式SingleMode(自定义listview item)

来源:https://stackoverflow.com/questions/8337180/custom-single-choice-listview/12823457#12823457 1.在listview item里面设置 <span style="font-size:18px;"><RadioButton android:id="@+id/radio1" android:checked="false" android:

Android ListView单选CheckBox

思路 1触发事件setOnItemClickListener改变CheckBox 2在适配器的getView()里改变CheckBox状态,需要记录选中的CheckBox位置 3适配器提供方法改变CheckBox 4用notifyDataSetChanged启动getView() 实现代码 //适配器 public class MyAdapter extends BaseAdapter { // 填充数据的list private ArrayList<Map<String, Object>

android ListView单选功能

在Adapter  getView内部实现控制. private int selectPosition =-1; class PayAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return payList==null?0:payList.size(); } @Override public Object getItem(int position)

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

(假设须要完整demo,请评论留下邮箱) (眼下源代码已经不发送.假设须要源代码,加qq316701116.不喜勿扰) 近期由于一个项目的原因须要自己定义一个BaseAdapter实现ListVIew单选打勾的功能,尽管听起来非常easy,我在网上也 看过一些样例,似乎是实现了,但往往存在一些漏洞.往往漏洞例如以下 1.网上样例item较少,item增多时漏洞出现,忽略了BaseAdapter中getView()方法中convertView重用的问题 2.忽略了BaseAdapter中getVi

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

最近因为一个项目的原因需要自定义一个BaseAdapter实现ListVIew单选打勾的功能,虽然听起来很简单,我在网上也 看过一些例子,似乎是实现了,但往往存在一些漏洞.往往漏洞如下 1.网上例子item较少,item增多时漏洞出现,忽略了BaseAdapter中getView()方法中convertView重用的问题 2.忽略了BaseAdapter中getView()方法并不是一下子加载完所有item,上下拖动listview时item会重新加载,getview会重新被调用,所以上下拖动的

android列表选择模式的实现

我们或许曾一次又一次的接到这样的需求,有一堆数据需要用户去选择,并且以列表的方式呈现,根据需求,我们需要单选或者复选列表里面的数据,并且最终取得所选数据作下一步的操作.那么对于这个需求,我们聪明的程序员往往都能想到一些解决方案去处理.譬如我,咳咳,虽然我不是很聪明,但是我想到了. [也许这样实现是对的]通常需要选择的列表数据我都会在adapter所绑定的数据实体内增加一个标记代表是否选中,在点击的时候去遍历并改变adapter中的实体标记,通知更新,然后根据标记在adapter的getView方

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

NoHttp开源地址:https://github.com/yanzhenjie/NoHttp NoHttp具体使用文档已公布,你想知道的全都有,请点我移步! 版权声明:转载请注明本文转自严振杰的博客: http://blog.yanzhenjie.com 演示 大家看博客时常常看到以下这样的图片,都非常想知道怎么做的吧,好在自己写博客时也把操作录下来: 这个图是我还有一个博客的图讲Android三级联动和ListView单选多选的,博客和源代码传送门,如今呢就一步步教大家怎么来做这个图. 上方

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

原文网址:http://blog.csdn.net/checkin001/article/details/11519131 Android自带的RadioButton单选框只支持添加文字,我们自己写Adapter实现自定义的RadioButton 首先item的XML源码 search_user_item.xml (现在只是文字+单选按钮+自定义背景,可以根据需要随意扩展) [html] view plaincopy <?xml version="1.0" encoding=&q