android帶勾選項的列表

一.列表中每項的佈局文件

<?xml version= "1.0" encoding ="utf-8"?>

<LinearLayout xmlns: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 中加入 

  <style name="DeptCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox" >

            <item name= "android:button">@drawable/dept_checkbox_style </item>

    </style >

資源文件dept_checkbox_style.xml

<?xml version= "1.0" encoding ="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable= "@drawable/work_diary_checkbok_pre" android:state_checked="true" />

    <item android:drawable= "@drawable/work_diary_checkbok" android:state_checked="false" />

    <item android:drawable= "@drawable/work_diary_checkbok" />

</selector>

 

二.適配器編寫

1.在適配器中用一個map變量存儲選擇的值

2.在複選框組件上註冊OnCheckedChangeListener事件 在該事件中將選擇的值保存到map中

 

package com.shjy.jingin.model;

import java.util.HashMap;

import java.util.List;

 

import com.baidu.location.b.m;

import com.shidian.SDK.imageloader.core.DisplayImageOptions;

import com.shidian.SDK.imageloader.core.ImageLoader;

import com.shidian.SDK.imageloader.core.display.RoundedBitmapDisplayer2;

import com.shjy.jingin.R;

import com.shjy.jingin.activity.EditWorkDiaryActivity.SelectContext ;

import com.shjy.jingin.bean.SortModel;

import com.shjy.jingin.bean.Staff;

import com.shjy.jingin.context.JYBoxApplication;

import com.shjy.jingin.tools.Utils;

import com.shjy.jingin.util.RandomColor;

import com.shjy.jingin.widget.HeaderView;

 

import android.content.Context;

import android.graphics.Bitmap;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.SectionIndexer;

import android.widget.TextView;

import android.widget.CompoundButton.OnCheckedChangeListener;

 

public class SelectStaffAdapter extends BaseAdapter implements SectionIndexer{

        private List<SortModel> list = null;

        private Context mContext ;

        //由於存儲選擇的數據

        private static HashMap<Long, Boolean> isSelected = null;

        private ImageLoader loader ;

        private DisplayImageOptions options ;

        private JYBoxApplication jyBoxApplication ;

        public SelectStaffAdapter(Context mContext, List<SortModel> list) {

               this.mContext = mContext;

               this.list = list;

               isSelected = new HashMap<Long, Boolean>();

               jyBoxApplication = JYBoxApplication.getInstance();

               loader = ImageLoader. getInstance();// 圖片的異步加載器

               // 創建默認的顯示的圖片

        options = new DisplayImageOptions.Builder()

                           .showImageForEmptyUri(R.drawable. head3x)                      

                           .showImageOnFail(R.drawable. head3x)// 設置圖標加載或者解析錯誤時候的照片

                           .cacheInMemory( true).cacheOnDisc(true)// 設置下載的圖片放在緩存中

                           .displayer( new RoundedBitmapDisplayer2(0)).build();// 設置圖片圓角

       }

       

 

       

        public void updateListView(List<SortModel> list){

               this.list = list;

              notifyDataSetChanged();

       }

 

        public int getCount() {

               return this .list .size();

       }

 

        public Object getItem(int position) {

               return list .get(position);

       }

 

        public long getItemId(int position) {

               return position;

       }

       

       

       

        public static HashMap<Long, Boolean> getIsSelected() {

               return isSelected ;

       }

 

 

        public static void setIsSelected(HashMap<Long, Boolean> isSelected) {

              SelectStaffAdapter. isSelected = isSelected;

       }

 

 

        public View getView(final int position, View view, ViewGroup arg2) {

              ViewHolder viewHolder = null;

              SortModel mContent = this.list .get(position);

               final Staff staff = this.list.get(position).getStaff();

               if (view == null) {

                     viewHolder = new ViewHolder();

                     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

               final CheckBox checkBox = viewHolder.checkBox ;

              checkBox.setOnCheckedChangeListener( new OnCheckedChangeListener() {

                     

                      @Override

                      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                            

                            isSelected.put(staff.getId(), isChecked);                            

                     }

              });

              

              Boolean checked = isSelected.get(staff.getId());

              viewHolder. checkBox.setChecked(checked== null?false :checked);

               int section = 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());

              

               return view;

 

       }

       

 

 

        public static class ViewHolder {

               public HeaderView headerView ;

               public TextView photoText ;

//            public TextView nameTextView;

               public TextView deptNameTextView ;

               public TextView tvLetter ;

               public CheckBox checkBox ;

       }

 

 

       

        public int getSectionForPosition(int position) {

               return list .get(position).getSortLetters().charAt(0);

       }

 

       

        public int getPositionForSection(int section) {

               for (int i = 0; i < getCount(); i++) {

                     String sortStr = list.get(i).getSortLetters();

                      char firstChar = sortStr.toUpperCase().charAt(0);

                      if (firstChar == section) {

                            return i;

                     }

              }

              

               return -1;

       }

       

       

        private String getAlpha(String str) {

              String  sortStr = str.trim().substring(0, 1). toUpperCase();

              

               if (sortStr.matches("[A-Z]" )) {

                      return sortStr;

              } else {

                      return "#" ;

              }

       }

 

        @Override

        public Object[] getSections() {

               return null ;

       }

}

 

三.將數據庫中選擇的數據進行回顯

selectStaffAdapter = new SelectStaffAdapter(this, sortModels);       

 if(SelectContext.selectStaffs!=null&&SelectContext. selectStaffs.size()>0){

          HashMap<Long, Boolean> selectMap = new HashMap<Long, Boolean>();

          for(Staff staff:SelectContext.selectStaffs){

                 selectMap.put(staff.getId(), true);

            }

               selectStaffAdapter.setIsSelected(selectMap);

         }

 

在數據適配器getView中

Boolean checked = isSelected .get(staff.getId());

viewHolder. checkBox.setChecked( checked==null? false:checked);

 

----
spring mvc+tomcat源碼分析視頻(鏈接複製在瀏覽器打開)

https://study.163.com/course/courseMain.htm?share=2&shareId=480000001919582&courseId=1209399899&_trace_c_p_k2_=6d81bc445e9c462ab8d6345e40f6b0bf

發佈了89 篇原創文章 · 獲贊 5 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章