android带勾选项的列表
来源:互联网 发布:修改数据库时间 编辑:程序博客网 时间:2024/06/05 15:45
一.列表中每项的布局文件
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/catalog"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E0E0E0"
android:layout_weight="1.0"
android:paddingLeft="5dip"
android:paddingTop="5dip"
android:paddingBottom="5dip"
android:textSize="13dp"
android:textColor="@color/index_font"
android:text="A"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dip"
android:background="@color/white"
>
<com.shjy.jingin.widget.HeaderView
android:id="@+id/select_staff_photo"
android:layout_width="46dip"
android:layout_height="46dip"
android:scaleType="centerCrop"
android:layout_centerVertical="true"
android:background="@drawable/head3x"
android:layout_marginLeft="10dip"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_toRightOf="@+id/select_staff_photo"
android:layout_marginLeft="10dip">
<TextView
android:id="@+id/select_staff_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dip"
android:textSize="16dp"
android:textColor="@color/main_font"/>
<TextView
android:id="@+id/select_staff_dept_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:textSize="13dp"
android:textColor="@color/main_font_color2"/>
</LinearLayout>
<RelativeLayout
android:id="@+id/ry_phone_staff"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="15dip">
<CheckBox
android:id="@+id/select_staff_checkbox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
style="@style/DeptCheckboxTheme"
android:layout_marginRight="18dp"
/>
</RelativeLayout>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#d1d1d1"/>
</LinearLayout>
对复选框修饰的样式文件 @style/DeptCheckboxTheme
style.xml 中加入
<stylename="DeptCheckboxTheme"parent="@android:style/Widget.CompoundButton.CheckBox">
<itemname="android:button">@drawable/dept_checkbox_style</item>
</style>
资源文件dept_checkbox_style.xml
<?xmlversion="1.0"encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:drawable="@drawable/work_diary_checkbok_pre"android:state_checked="true"/>
<itemandroid:drawable="@drawable/work_diary_checkbok"android:state_checked="false"/>
<itemandroid:drawable="@drawable/work_diary_checkbok"/>
</selector>
二.适配器编写
1.在适配器中用一个map变量存储选择的值2.在复选框组件上注册OnCheckedChangeListener事件 在该事件中将选择的值保存到map中
packagecom.shjy.jingin.model;
importjava.util.HashMap;
importjava.util.List;
importcom.baidu.location.b.m;
importcom.shidian.SDK.imageloader.core.DisplayImageOptions;
importcom.shidian.SDK.imageloader.core.ImageLoader;
importcom.shidian.SDK.imageloader.core.display.RoundedBitmapDisplayer2;
importcom.shjy.jingin.R;
importcom.shjy.jingin.activity.EditWorkDiaryActivity.SelectContext;
importcom.shjy.jingin.bean.SortModel;
importcom.shjy.jingin.bean.Staff;
importcom.shjy.jingin.context.JYBoxApplication;
importcom.shjy.jingin.tools.Utils;
importcom.shjy.jingin.util.RandomColor;
importcom.shjy.jingin.widget.HeaderView;
importandroid.content.Context;
importandroid.graphics.Bitmap;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.BaseAdapter;
importandroid.widget.CheckBox;
importandroid.widget.CompoundButton;
importandroid.widget.SectionIndexer;
importandroid.widget.TextView;
importandroid.widget.CompoundButton.OnCheckedChangeListener;
publicclassSelectStaffAdapterextendsBaseAdapterimplementsSectionIndexer{
privateList<SortModel>list=null;
privateContextmContext;
//由于存储选择的数据
privatestaticHashMap<Long, Boolean>isSelected=null;
privateImageLoaderloader;
privateDisplayImageOptionsoptions;
privateJYBoxApplicationjyBoxApplication;
publicSelectStaffAdapter(Context mContext, List<SortModel> list) {
this.mContext= mContext;
this.list= list;
isSelected=newHashMap<Long, Boolean>();
jyBoxApplication= JYBoxApplication.getInstance();
loader= ImageLoader.getInstance();// 图片的异步加载器
// 创建默认的显示的图片
options=newDisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.head3x)
.showImageOnFail(R.drawable.head3x)// 设置图标加载或者解析错误时候的照片
.cacheInMemory(true).cacheOnDisc(true)// 设置下载的图片放在缓存中
.displayer(newRoundedBitmapDisplayer2(0)).build();// 设置图片圆角
}
publicvoidupdateListView(List<SortModel> list){
this.list= list;
notifyDataSetChanged();
}
publicintgetCount() {
returnthis.list.size();
}
publicObject getItem(intposition) {
returnlist.get(position);
}
publiclonggetItemId(intposition) {
returnposition;
}
publicstaticHashMap<Long, Boolean> getIsSelected() {
returnisSelected;
}
publicstaticvoidsetIsSelected(HashMap<Long, Boolean> isSelected) {
SelectStaffAdapter.isSelected= isSelected;
}
publicView getView(finalintposition, View view, ViewGroup arg2) {
ViewHolder viewHolder =null;
SortModel mContent =this.list.get(position);
finalStaff staff =this.list.get(position).getStaff();
if(view ==null) {
viewHolder =newViewHolder();
view = LayoutInflater.from(mContext).inflate(R.layout.item_select_staff_list,null);
viewHolder.headerView= (HeaderView)view.findViewById(R.id.select_staff_photo);
// viewHolder.nameTextView = (TextView) view.findViewById(R.id.select_staff_name);
viewHolder.deptNameTextView= (TextView) view.findViewById(R.id.select_staff_dept_name);
viewHolder.tvLetter= (TextView) view.findViewById(R.id.catalog);
viewHolder.checkBox= (CheckBox)view.findViewById(R.id.select_staff_checkbox);
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) view.getTag();
}
//将选择的值记录在 isSelected
finalCheckBox checkBox = viewHolder.checkBox;
checkBox.setOnCheckedChangeListener(newOnCheckedChangeListener() {
@Override
publicvoidonCheckedChanged(CompoundButton buttonView,booleanisChecked) {
isSelected.put(staff.getId(), isChecked);
}
});
Boolean checked =isSelected.get(staff.getId());
viewHolder.checkBox.setChecked(checked==null?false:checked);
intsection = getSectionForPosition(position);
if(position == getPositionForSection(section)){
viewHolder.tvLetter.setVisibility(View.VISIBLE);
viewHolder.tvLetter.setText(mContent.getSortLetters());
}else{
viewHolder.tvLetter.setVisibility(View.GONE);
}
String serviceAddress =jyBoxApplication.getServiceAddress();
String path ="http://"+ serviceAddress +"/"+ staff.getPath();
loader.clearDiscCache();
loader.clearMemoryCache();
loader.displayImage(path, viewHolder.headerView,options);
// viewHolder.nameTextView.setText(staff.getName());
viewHolder.deptNameTextView.setText(staff.getDeptName());
returnview;
}
publicstaticclassViewHolder {
publicHeaderViewheaderView;
publicTextViewphotoText;
// public TextView nameTextView;
publicTextViewdeptNameTextView;
publicTextViewtvLetter;
publicCheckBoxcheckBox;
}
publicintgetSectionForPosition(intposition) {
returnlist.get(position).getSortLetters().charAt(0);
}
publicintgetPositionForSection(intsection) {
for(inti = 0; i < getCount(); i++) {
String sortStr =list.get(i).getSortLetters();
charfirstChar = sortStr.toUpperCase().charAt(0);
if(firstChar == section) {
returni;
}
}
return-1;
}
privateStringgetAlpha(String str) {
String sortStr = str.trim().substring(0, 1).toUpperCase();
if(sortStr.matches("[A-Z]")) {
returnsortStr;
}else{
return"#";
}
}
@Override
publicObject[] getSections() {
returnnull;
}
}
三.将数据库中选择的数据进行回显
selectStaffAdapter=newSelectStaffAdapter(this,sortModels);
if(SelectContext.selectStaffs!=null&&SelectContext.selectStaffs.size()>0){
HashMap<Long, Boolean> selectMap =newHashMap<Long, Boolean>();
for(Staff staff:SelectContext.selectStaffs){
selectMap.put(staff.getId(),true);
}
selectStaffAdapter.setIsSelected(selectMap);
}
在数据适配器getView中
Booleanchecked = isSelected .get(staff.getId());
viewHolder.checkBox.setChecked(checked==null?false:checked);
0 0
- android带勾选项的列表
- Android-创建带多选项列表的对话框
- Android-创建带单选项列表的对话框
- android中列表选项、单选选项、多选选项对话框的使用
- Android实现带图片的List列表
- android弹出带图标的列表对话框
- android带气泡的第三方选项卡
- Android动态标签选项列表效果的实现
- 安卓对话框之----创建带多选项列表的对话框
- android dialog ——列表项带图标的列表对话框、模拟菜单项带
- 带选项框的listview
- 带选项框的listview
- android中带索引的列表-----索引的高级使用
- Android带索引联系人列表
- android 系统自带的软件可以删除列表--Defy
- 【Android开发】范例2-带图标的列表对话框
- Android必知必会-带列表的地图POI周边搜索
- Android开发带搜索26字母的列表框架
- 微信小程序开发基础篇(三)之框架
- Maximum Product Subarray
- FSGUI的安装ISO(fsgui,freeswitch,postgresql,redis,centos6)
- css分隔线中间带文字
- 3.几种常见的数据存储方式
- android带勾选项的列表
- boolan——c++学习笔记1
- 开启tomcat的SSL(https)支持
- linux --- inotify 文件系统变化通知机制
- 自省
- linux下perl开发环境
- HDU 3037 lucas + 费马小定理
- 【NOIP2014八校联考第3场第1试10.4】规避(path) (Standard IO)
- IDEA 常用设置