android樹形列表實現

一. 實現方法

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

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

<RelativeLayout xmlns: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

    public View getView( int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        final Department element = departments.get(position);

        if (convertView == null) {

            holder = new ViewHolder();

            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();

        }

       

       

        //根據層級屬性動態調整邊距

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

        }

       

        final CheckBox checkBox = holder.checkBox ;

        checkBox.setOnCheckedChangeListener( new OnCheckedChangeListener() {

                     

                      @Override

                      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                            

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

//        }

        return convertView;

    }

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

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

 

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