android树形列表实现

时间:2022-09-06 16:02:57
一. 实现方法      1.列表中每项的布局文件    <?xmlversion="1.0"encoding="utf-8"?> <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@color/white"   android:paddingTop="10dip"   android:paddingBottom="10dip">    <TextView        android:id="@+id/select_department_name"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_centerVertical="true"        android:textSize="16dp"        android:gravity="center"        android:text="测试"        android:layout_alignParentLeft="true"      />          <CheckBox        android:id="@+id/selectcheckbox"        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>
      2.通过控制列表项中的每项与边的距离,例如用树形展示部门层级关系,可以在模型数据对象Department中根据层级level属性来动态调整   适配器中的关键代码     @Override   publicView getView(intposition, View convertView, ViewGroup parent) {        ViewHolder holder =null;       finalDepartmentelement = departments.get(position);       if(convertView ==null) {            holder =newViewHolder();            convertView =inflater.inflate(R.layout.select_department_item,null);            holder.checkBox= (CheckBox) convertView.findViewById(R.id.selectcheckbox);            holder.contentText= (TextView) convertView.findViewById(R.id.select_department_name);                      convertView.setTag(holder);        }else{            holder = (ViewHolder) convertView.getTag();        }                      //根据层级属性动态调整边距       intlevel =element.getDepth();       if(level == 1){              holder.contentText.setPadding(                    10,                    holder.contentText.getPaddingTop(),                    holder.contentText.getPaddingRight(),                    holder.contentText.getPaddingBottom());        }else{              holder.contentText.setPadding(                   indentionBase* (level),                    holder.contentText.getPaddingTop(),                    holder.contentText.getPaddingRight(),                    holder.contentText.getPaddingBottom());        }              finalCheckBox checkBox = holder.checkBox;        checkBox.setOnCheckedChangeListener(newOnCheckedChangeListener() {                                          @Override                     publicvoidonCheckedChanged(CompoundButton buttonView,booleanisChecked) {                                                       isSelected.put(element.getId(), isChecked);                                               }              });               holder.contentText.setText(element.getName());        Boolean checked =isSelected.get(element.getId());        holder.checkBox.setChecked(checked==null?false:checked);       //        if (element.isHasChildren() && !element.isExpanded()) {////            holder.disclosureImg.setImageResource(R.drawable.arrow_close);////            //这里要主动设置一下icon可见,因为convertView有可能是重用了"设置了不可见"的view,下同。////            holder.disclosureImg.setVisibility(View.VISIBLE);//        } else if (element.isHasChildren() && element.isExpanded()) {////            holder.disclosureImg.setImageResource(R.drawable.arrow_open);////            holder.disclosureImg.setVisibility(View.VISIBLE);//        } else if (!element.isHasChildren()) {////            holder.disclosureImg.setImageResource(R.drawable.arrow_close);////            holder.disclosureImg.setVisibility(View.INVISIBLE);//        }       returnconvertView;    }