今天在智能停车场项目中需要实现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; }
http://www.cnblogs.com/xiaoxiao-study/p/a1e1561444542d99b70e781be488f35e.html
时间: 2024-12-17 14:29:56