├── .gitignore ├── .idea ├── caches │ ├── build_file_checksums.ser │ └── gradle_models.ser ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── assets │ └── levelList.gif │ ├── java │ └── com │ │ └── demo │ │ └── multilevellist │ │ ├── DensityUtil.java │ │ ├── MainActivity.java │ │ ├── TreeAdapter.java │ │ ├── TreePoint.java │ │ └── TreeUtils.java │ └── res │ ├── drawable-hdpi │ ├── btn_check_off_normal.png │ ├── btn_check_on_normal.png │ ├── ic_left.png │ ├── ic_search.png │ ├── outline_list_collapse.png │ └── outline_list_expand.png │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ ├── bg_imagebutton.xml │ ├── bg_input_filter.xml │ ├── ic_launcher_background.xml │ ├── select_btn_check.xml │ ├── work_divile_line2.9.png │ ├── work_divile_line5.9.png │ ├── work_divile_line6.9.png │ └── work_divile_line_bg.xml │ ├── layout │ ├── activity_main.xml │ └── adapter_treeview.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | /.idea 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oldbirdy/MultilevelList/f8023e128a9420fa16eefecd580efe4024d42c05/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/caches/gradle_models.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oldbirdy/MultilevelList/f8023e128a9420fa16eefecd580efe4024d42c05/.idea/caches/gradle_models.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | xmlns:android 14 | 15 | ^$ 16 | 17 | 18 | 19 |
20 |
21 | 22 | 23 | 24 | xmlns:.* 25 | 26 | ^$ 27 | 28 | 29 | BY_NAME 30 | 31 |
32 |
33 | 34 | 35 | 36 | .*:id 37 | 38 | http://schemas.android.com/apk/res/android 39 | 40 | 41 | 42 |
43 |
44 | 45 | 46 | 47 | .*:name 48 | 49 | http://schemas.android.com/apk/res/android 50 | 51 | 52 | 53 |
54 |
55 | 56 | 57 | 58 | name 59 | 60 | ^$ 61 | 62 | 63 | 64 |
65 |
66 | 67 | 68 | 69 | style 70 | 71 | ^$ 72 | 73 | 74 | 75 |
76 |
77 | 78 | 79 | 80 | .* 81 | 82 | ^$ 83 | 84 | 85 | BY_NAME 86 | 87 |
88 |
89 | 90 | 91 | 92 | .* 93 | 94 | http://schemas.android.com/apk/res/android 95 | 96 | 97 | ANDROID_ATTRIBUTE_ORDER 98 | 99 |
100 |
101 | 102 | 103 | 104 | .* 105 | 106 | .* 107 | 108 | 109 | BY_NAME 110 | 111 |
112 |
113 |
114 |
115 |
116 |
-------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 15 | 16 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MultilevelList 2 | 多级列表 3 | [csdn 博客地址](https://blog.csdn.net/xu_coding/article/details/81490297) 4 | 效果图如下![效果图](https://github.com/oldbirdy/MultilevelList/blob/master/app/src/main/assets/levelList.gif) 5 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.demo.multilevellist" 7 | minSdkVersion 21 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:28.0.0' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | } 29 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/assets/levelList.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oldbirdy/MultilevelList/f8023e128a9420fa16eefecd580efe4024d42c05/app/src/main/assets/levelList.gif -------------------------------------------------------------------------------- /app/src/main/java/com/demo/multilevellist/DensityUtil.java: -------------------------------------------------------------------------------- 1 | package com.demo.multilevellist; 2 | 3 | import android.content.Context; 4 | 5 | /** 6 | * Created by xulc on 2018/8/7. 7 | */ 8 | 9 | public class DensityUtil { 10 | 11 | public static int dip2px(Context context, float dpValue) { 12 | float scale = context.getResources().getDisplayMetrics().density; 13 | return (int) (dpValue * scale + 0.5f); 14 | } 15 | 16 | public static int px2dip(Context context, float pxValue) { 17 | float scale = context.getResources().getDisplayMetrics().density; 18 | return (int) (pxValue / scale + 0.5f); 19 | } 20 | 21 | public static float dip2pxf(Context context, int dpValue) { 22 | float scale = context.getResources().getDisplayMetrics().density; 23 | return dpValue * scale + 0.5f; 24 | } 25 | 26 | public static float dip2pxf(Context context, float dpValue) { 27 | float scale = context.getResources().getDisplayMetrics().density; 28 | return dpValue * scale + 0.5f; 29 | } 30 | 31 | /** 32 | * 将px值转换为sp值,保证文字大小不变 33 | * 34 | * @param context 35 | * @param pxValue 36 | * (DisplayMetrics类中属性scaledDensity) 37 | * @return 38 | */ 39 | public static int px2sp(Context context, float pxValue) { 40 | float fontScale = context.getResources().getDisplayMetrics().scaledDensity; 41 | return (int) (pxValue / fontScale + 0.5f); 42 | } 43 | 44 | /** 45 | * 将sp值转换为px值,保证文字大小不变 46 | * 47 | * @param context 48 | * @param spValue 49 | * (DisplayMetrics类中属性scaledDensity) 50 | * @return 51 | */ 52 | public static int sp2px(Context context, float spValue) { 53 | float fontScale = context.getResources().getDisplayMetrics().scaledDensity; 54 | return (int) (spValue * fontScale + 0.5f); 55 | } 56 | 57 | public static float sp2pxf(Context context, float spValue) { 58 | float fontScale = context.getResources().getDisplayMetrics().scaledDensity; 59 | return spValue * fontScale + 0.5f; 60 | } 61 | } 62 | 63 | -------------------------------------------------------------------------------- /app/src/main/java/com/demo/multilevellist/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.demo.multilevellist; 2 | import android.support.annotation.Nullable; 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.text.Editable; 6 | import android.text.TextWatcher; 7 | import android.view.View; 8 | import android.widget.AdapterView; 9 | import android.widget.EditText; 10 | import android.widget.ListView; 11 | import java.util.ArrayList; 12 | import java.util.Collections; 13 | import java.util.Comparator; 14 | import java.util.HashMap; 15 | import java.util.List; 16 | 17 | public class MainActivity extends AppCompatActivity { 18 | 19 | private TreeAdapter adapter; 20 | private ListView listView; 21 | private EditText et_filter; 22 | private List pointList = new ArrayList<>(); 23 | private HashMap pointMap = new HashMap<>(); 24 | 25 | 26 | @Override 27 | protected void onCreate(@Nullable Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(); 30 | init(); 31 | addListener(); 32 | } 33 | 34 | 35 | public void setContentView() { 36 | setContentView(R.layout.activity_main); 37 | } 38 | 39 | 40 | public void init() { 41 | adapter = new TreeAdapter(this, pointList, pointMap); 42 | listView = findViewById(R.id.listView); 43 | listView.setAdapter(adapter); 44 | et_filter = findViewById(R.id.et_filter); 45 | initData(); 46 | } 47 | 48 | //初始化数据 49 | //数据特点:TreePoint 之间的关系特点 id是任意唯一的。 如果为根节点 PARENTID 为"0" 如果没有子节点,也就是本身是叶子节点的时候ISLEAF = "1" 50 | // DISPLAY_ORDER 是同一级中 显示的顺序 51 | //如果需要做选中 单选或者多选,只需要给TreePoint增加一个选中的属性,在ReasonAdapter中处理就好了 52 | private void initData() { 53 | pointList.clear(); 54 | int id =1000; 55 | int parentId = 0; 56 | int parentId2 = 0; 57 | int parentId3 = 0; 58 | for(int i=1;i<5;i++){ 59 | id++; 60 | pointList.add(new TreePoint(""+id,"分类"+i,"" + parentId,"0",i)); 61 | for(int j=1;j<5;j++){ 62 | if(j==1){ 63 | parentId2 = id; 64 | } 65 | id++; 66 | pointList.add(new TreePoint(""+id,"分类"+i+"_"+j,""+parentId2,"0",j)); 67 | for(int k=1;k<5;k++){ 68 | if(k==1){ 69 | parentId3 = id; 70 | } 71 | id++; 72 | pointList.add(new TreePoint(""+id,"分类"+i+"_"+j+"_"+k,""+parentId3,"1",k)); 73 | } 74 | } 75 | } 76 | //打乱集合中的数据 77 | Collections.shuffle(pointList); 78 | //对集合中的数据重新排序 79 | updateData(); 80 | } 81 | 82 | 83 | public void addListener() { 84 | 85 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 86 | @Override 87 | public void onItemClick(AdapterView parent, View view, int position, long id) { 88 | adapter.onItemClick(position); 89 | } 90 | }); 91 | 92 | et_filter.addTextChangedListener(new TextWatcher() { 93 | @Override 94 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { 95 | 96 | } 97 | 98 | @Override 99 | public void onTextChanged(CharSequence s, int start, int before, int count) { 100 | 101 | } 102 | 103 | @Override 104 | public void afterTextChanged(Editable s) { 105 | searchAdapter(s); 106 | } 107 | }); 108 | } 109 | 110 | private void searchAdapter(Editable s) { 111 | adapter.setKeyword(s.toString()); 112 | } 113 | 114 | //对数据排序 深度优先 115 | private void updateData() { 116 | for (TreePoint treePoint : pointList) { 117 | pointMap.put(treePoint.getID(), treePoint); 118 | } 119 | Collections.sort(pointList, new Comparator() { 120 | @Override 121 | public int compare(TreePoint lhs, TreePoint rhs) { 122 | int llevel = TreeUtils.getLevel(lhs, pointMap); 123 | int rlevel = TreeUtils.getLevel(rhs, pointMap); 124 | if (llevel == rlevel) { 125 | if (lhs.getPARENTID().equals(rhs.getPARENTID())) { //左边小 126 | return lhs.getDISPLAY_ORDER() > rhs.getDISPLAY_ORDER() ? 1 : -1; 127 | } else { //如果父辈id不相等 128 | //同一级别,不同父辈 129 | TreePoint ltreePoint = TreeUtils.getTreePoint(lhs.getPARENTID(), pointMap); 130 | TreePoint rtreePoint = TreeUtils.getTreePoint(rhs.getPARENTID(), pointMap); 131 | return compare(ltreePoint, rtreePoint); //父辈 132 | } 133 | } else { //不同级别 134 | if (llevel > rlevel) { //左边级别大 左边小 135 | if (lhs.getPARENTID().equals(rhs.getID())) { 136 | return 1; 137 | } else { 138 | TreePoint lreasonTreePoint = TreeUtils.getTreePoint(lhs.getPARENTID(), pointMap); 139 | return compare(lreasonTreePoint, rhs); 140 | } 141 | } else { //右边级别大 右边小 142 | if (rhs.getPARENTID().equals(lhs.getID())) { 143 | return -1; 144 | } 145 | TreePoint rreasonTreePoint = TreeUtils.getTreePoint(rhs.getPARENTID(), pointMap); 146 | return compare(lhs, rreasonTreePoint); 147 | } 148 | } 149 | } 150 | }); 151 | adapter.notifyDataSetChanged(); 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /app/src/main/java/com/demo/multilevellist/TreeAdapter.java: -------------------------------------------------------------------------------- 1 | package com.demo.multilevellist; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.support.annotation.IntDef; 6 | import android.text.Html; 7 | import android.text.Spanned; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.BaseAdapter; 12 | import android.widget.ImageButton; 13 | import android.widget.ImageView; 14 | import android.widget.TextView; 15 | import android.widget.Toast; 16 | import java.util.HashMap; 17 | import java.util.List; 18 | 19 | 20 | 21 | /** 22 | * Created by xulc on 2018/7/27. 23 | */ 24 | 25 | public class TreeAdapter extends BaseAdapter { 26 | private Context mcontext; 27 | private Activity mActivity; 28 | private List pointList; 29 | private String keyword = ""; 30 | private HashMap pointMap = new HashMap<>(); 31 | 32 | private int operateMode = ModeSelect; 33 | //两种操作模式 点击 或者 选择 34 | private static final int ModeClick = 1; 35 | private static final int ModeSelect = 2; 36 | 37 | @IntDef({ModeClick,ModeSelect}) 38 | public @interface Mode{ 39 | 40 | } 41 | 42 | //设置操作模式 43 | public void setOperateMode(@Mode int operateMode){ 44 | this.operateMode = operateMode; 45 | } 46 | 47 | public TreeAdapter(final Context mcontext, List pointList, HashMap pointMap) { 48 | this.mcontext = mcontext; 49 | this.mActivity = (Activity) mcontext; 50 | this.pointList = pointList; 51 | this.pointMap = pointMap; 52 | } 53 | 54 | /** 55 | * 搜索的时候,先关闭所有的条目,然后,按照条件,找到含有关键字的数据 56 | * 如果是叶子节点, 57 | */ 58 | public void setKeyword(String keyword) { 59 | this.keyword = keyword; 60 | for (TreePoint treePoint : pointList) { 61 | treePoint.setExpand(false); 62 | } 63 | if (!"".equals(keyword)) { 64 | for (TreePoint tempTreePoint : pointList) { 65 | if (tempTreePoint.getNNAME().contains(keyword)) { 66 | if ("0".equals(tempTreePoint.getISLEAF())) { //非叶子节点 67 | tempTreePoint.setExpand(true); 68 | } 69 | //展开从最顶层到该点的所有节点 70 | openExpand(tempTreePoint); 71 | } 72 | } 73 | } 74 | this.notifyDataSetChanged(); 75 | } 76 | 77 | /** 78 | * 从TreePoint开始一直展开到顶部 79 | * @param treePoint 80 | */ 81 | private void openExpand(TreePoint treePoint) { 82 | if ("0".equals(treePoint.getPARENTID())) { 83 | treePoint.setExpand(true); 84 | } else { 85 | pointMap.get(treePoint.getPARENTID()).setExpand(true); 86 | openExpand(pointMap.get(treePoint.getPARENTID())); 87 | } 88 | } 89 | 90 | 91 | //第一要准确计算数量 92 | @Override 93 | public int getCount() { 94 | int count = 0; 95 | for (TreePoint tempPoint : pointList) { 96 | if ("0".equals(tempPoint.getPARENTID())) { 97 | count++; 98 | } else { 99 | if (getItemIsExpand(tempPoint.getPARENTID())) { 100 | count++; 101 | } 102 | } 103 | } 104 | return count; 105 | } 106 | 107 | //判断当前Id的tempPoint是否展开了 108 | private boolean getItemIsExpand(String ID) { 109 | for (TreePoint tempPoint : pointList) { 110 | if (ID.equals(tempPoint.getID())) { 111 | return tempPoint.isExpand(); 112 | } 113 | } 114 | return false; 115 | } 116 | 117 | @Override 118 | public Object getItem(int position) { 119 | return pointList.get(convertPostion(position)); 120 | } 121 | 122 | private int convertPostion(int position) { 123 | int count = 0; 124 | for (int i = 0; i < pointList.size(); i++) { 125 | TreePoint treePoint = pointList.get(i); 126 | if ("0".equals(treePoint.getPARENTID())) { 127 | count++; 128 | } else { 129 | if (getItemIsExpand(treePoint.getPARENTID())) { 130 | count++; 131 | } 132 | } 133 | if (position == (count - 1)) { 134 | return i; 135 | } 136 | } 137 | return 0; 138 | } 139 | 140 | @Override 141 | public long getItemId(int position) { 142 | return position; 143 | } 144 | 145 | 146 | @Override 147 | public View getView(int position, View convertView, ViewGroup parent) { 148 | ViewHolder holder; 149 | if (convertView == null) { 150 | convertView = LayoutInflater.from(mcontext).inflate(R.layout.adapter_treeview, null); 151 | holder = new ViewHolder(); 152 | holder.text = (TextView) convertView.findViewById(R.id.text); 153 | holder.icon = (ImageView) convertView.findViewById(R.id.icon); 154 | holder.ib_select = (ImageButton) convertView.findViewById(R.id.ib_select); 155 | convertView.setTag(holder); 156 | } else { 157 | holder = (ViewHolder) convertView.getTag(); 158 | } 159 | final TreePoint tempPoint = (TreePoint) getItem(position); 160 | int level = TreeUtils.getLevel(tempPoint,pointMap); 161 | holder.icon.setPadding(25 * level, holder.icon.getPaddingTop(), 0, holder.icon.getPaddingBottom()); 162 | if ("0".equals(tempPoint.getISLEAF())) { //如果为父节点 163 | if (!tempPoint.isExpand()) { //不展开显示加号 164 | holder.icon.setVisibility(View.VISIBLE); 165 | holder.icon.setImageResource(R.drawable.outline_list_collapse); 166 | } else { //展开显示减号 167 | holder.icon.setVisibility(View.VISIBLE); 168 | holder.icon.setImageResource(R.drawable.outline_list_expand); 169 | } 170 | } else { //如果叶子节点,不占位显示 171 | holder.icon.setVisibility(View.INVISIBLE); 172 | } 173 | if(operateMode == ModeSelect){ 174 | holder.ib_select.setVisibility(View.VISIBLE); 175 | holder.ib_select.setSelected(tempPoint.isSelected()); 176 | holder.ib_select.setOnClickListener(new View.OnClickListener() { 177 | @Override 178 | public void onClick(View v) { 179 | onModeSelect(tempPoint); 180 | } 181 | }); 182 | }else{ 183 | holder.ib_select.setVisibility(View.GONE); 184 | } 185 | //如果存在搜索关键字 186 | if (keyword != null && !"".equals(keyword) && tempPoint.getNNAME().contains(keyword)) { 187 | int index = tempPoint.getNNAME().indexOf(keyword); 188 | int len = keyword.length(); 189 | Spanned temp = Html.fromHtml(tempPoint.getNNAME().substring(0, index) 190 | + "" 191 | + tempPoint.getNNAME().substring(index, index + len) + "" 192 | + tempPoint.getNNAME().substring(index + len, tempPoint.getNNAME().length())); 193 | 194 | holder.text.setText(temp); 195 | } else { 196 | holder.text.setText(tempPoint.getNNAME()); 197 | } 198 | holder.text.setCompoundDrawablePadding(DensityUtil.dip2px(mcontext, 10)); 199 | return convertView; 200 | } 201 | 202 | 203 | 204 | public void onItemClick(int position) { 205 | TreePoint treePoint = (TreePoint) getItem(position); 206 | if ("1".equals(treePoint.getISLEAF())) { //点击叶子节点 207 | //处理回填 208 | Toast.makeText(mcontext, getSubmitResult(treePoint), Toast.LENGTH_SHORT).show(); 209 | } else { //如果点击的是父类 210 | if (treePoint.isExpand()) { 211 | for (TreePoint tempPoint : pointList) { 212 | if (tempPoint.getPARENTID().equals(treePoint.getID())) { 213 | if ("0".equals(treePoint.getISLEAF())) { 214 | tempPoint.setExpand(false); 215 | } 216 | } 217 | } 218 | treePoint.setExpand(false); 219 | } else { 220 | treePoint.setExpand(true); 221 | } 222 | } 223 | this.notifyDataSetChanged(); 224 | } 225 | 226 | 227 | //选择操作 228 | private void onModeSelect(TreePoint treePoint){ 229 | if ("1".equals(treePoint.getISLEAF())) { //选择叶子节点 230 | //处理回填 231 | treePoint.setSelected(!treePoint.isSelected()); 232 | } else { //选择父节点 233 | int position = pointList.indexOf(treePoint); 234 | boolean isSelect = treePoint.isSelected(); 235 | treePoint.setSelected(!isSelect); 236 | if(position == -1){ 237 | return ; 238 | } 239 | if(position == pointList.size()-1){ 240 | return; 241 | } 242 | position++; 243 | for(;position < pointList.size();position++){ 244 | TreePoint tempPoint = pointList.get(position); 245 | if(tempPoint.getPARENTID().equals(treePoint.getPARENTID())){ //如果找到和自己同级的数据就返回 246 | break; 247 | } 248 | tempPoint.setSelected(!isSelect); 249 | } 250 | } 251 | this.notifyDataSetChanged(); 252 | } 253 | 254 | //选中所有的point 255 | // private void selectPoint(TreePoint treePoint) { 256 | // if(){ 257 | // 258 | // } 259 | // } 260 | 261 | 262 | 263 | 264 | private String getSubmitResult(TreePoint treePoint) { 265 | StringBuilder sb = new StringBuilder(); 266 | addResult(treePoint, sb); 267 | String result = sb.toString(); 268 | if (result.endsWith("-")) { 269 | result = result.substring(0, result.length() - 1); 270 | } 271 | return result; 272 | } 273 | 274 | private void addResult(TreePoint treePoint, StringBuilder sb) { 275 | if (treePoint != null && sb != null) { 276 | sb.insert(0, treePoint.getNNAME() + "-"); 277 | if (!"0".equals(treePoint.getPARENTID())) { 278 | addResult(pointMap.get(treePoint.getPARENTID()), sb); 279 | } 280 | } 281 | } 282 | 283 | 284 | class ViewHolder { 285 | TextView text; 286 | ImageView icon; 287 | ImageButton ib_select; 288 | } 289 | 290 | } 291 | -------------------------------------------------------------------------------- /app/src/main/java/com/demo/multilevellist/TreePoint.java: -------------------------------------------------------------------------------- 1 | package com.demo.multilevellist; 2 | 3 | /** 4 | * Created by xulc on 2018/7/27. 5 | */ 6 | 7 | public class TreePoint { 8 | private String ID; // 7241, //账号id 9 | private String NNAME; // "用户原因", //原因名称 10 | private String PARENTID; // 0, //父id 0表示父节点 11 | private String ISLEAF; //0, //是否是叶子节点 1为叶子节点 12 | private int DISPLAY_ORDER; // 1 //同一个级别的显示顺序 13 | private boolean isExpand = false; //是否展开了 14 | private boolean isSelected = false; //是否选中了 15 | 16 | 17 | public TreePoint(String ID, String NNAME, String PARENTID, String ISLEAF, int DISPLAY_ORDER) { 18 | this.ID = ID; 19 | this.NNAME = NNAME; 20 | this.PARENTID = PARENTID; 21 | this.ISLEAF = ISLEAF; 22 | this.DISPLAY_ORDER = DISPLAY_ORDER; 23 | } 24 | 25 | public String getID() { 26 | return ID; 27 | } 28 | 29 | public void setID(String ID) { 30 | this.ID = ID; 31 | } 32 | 33 | public String getNNAME() { 34 | return NNAME; 35 | } 36 | 37 | public void setNAME(String NAME) { 38 | this.NNAME = NNAME; 39 | } 40 | 41 | public String getPARENTID() { 42 | return PARENTID; 43 | } 44 | 45 | public void setPARENTID(String PARENTID) { 46 | this.PARENTID = PARENTID; 47 | } 48 | 49 | public String getISLEAF() { 50 | return ISLEAF; 51 | } 52 | 53 | public void setISLEAF(String ISLEAF) { 54 | this.ISLEAF = ISLEAF; 55 | } 56 | 57 | public int getDISPLAY_ORDER() { 58 | return DISPLAY_ORDER; 59 | } 60 | 61 | public void setDISPLAY_ORDER(int DISPLAY_ORDER) { 62 | this.DISPLAY_ORDER = DISPLAY_ORDER; 63 | } 64 | 65 | public boolean isExpand() { 66 | return isExpand; 67 | } 68 | 69 | public void setExpand(boolean expand) { 70 | isExpand = expand; 71 | } 72 | 73 | public boolean isSelected() { 74 | return isSelected; 75 | } 76 | 77 | public void setSelected(boolean selected) { 78 | isSelected = selected; 79 | } 80 | } -------------------------------------------------------------------------------- /app/src/main/java/com/demo/multilevellist/TreeUtils.java: -------------------------------------------------------------------------------- 1 | package com.demo.multilevellist; 2 | 3 | import android.util.Log; 4 | 5 | 6 | import java.util.HashMap; 7 | 8 | /** 9 | * Created by xulc on 2018/7/27. 10 | */ 11 | 12 | public class TreeUtils { 13 | //第一级别为0 14 | public static int getLevel(TreePoint treePoint,HashMap map){ 15 | if("0".equals(treePoint.getPARENTID())){ 16 | return 0; 17 | }else{ 18 | return 1+getLevel(getTreePoint(treePoint.getPARENTID(),map),map); 19 | } 20 | } 21 | 22 | 23 | 24 | public static TreePoint getTreePoint(String ID, HashMap map){ 25 | if(map.containsKey(ID)){ 26 | return map.get(ID); 27 | } 28 | Log.e("xlc","ID:" + ID); 29 | return null; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/btn_check_off_normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oldbirdy/MultilevelList/f8023e128a9420fa16eefecd580efe4024d42c05/app/src/main/res/drawable-hdpi/btn_check_off_normal.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/btn_check_on_normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oldbirdy/MultilevelList/f8023e128a9420fa16eefecd580efe4024d42c05/app/src/main/res/drawable-hdpi/btn_check_on_normal.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oldbirdy/MultilevelList/f8023e128a9420fa16eefecd580efe4024d42c05/app/src/main/res/drawable-hdpi/ic_left.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oldbirdy/MultilevelList/f8023e128a9420fa16eefecd580efe4024d42c05/app/src/main/res/drawable-hdpi/ic_search.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/outline_list_collapse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oldbirdy/MultilevelList/f8023e128a9420fa16eefecd580efe4024d42c05/app/src/main/res/drawable-hdpi/outline_list_collapse.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/outline_list_expand.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oldbirdy/MultilevelList/f8023e128a9420fa16eefecd580efe4024d42c05/app/src/main/res/drawable-hdpi/outline_list_expand.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_imagebutton.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_input_filter.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/select_btn_check.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/work_divile_line2.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oldbirdy/MultilevelList/f8023e128a9420fa16eefecd580efe4024d42c05/app/src/main/res/drawable/work_divile_line2.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/work_divile_line5.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oldbirdy/MultilevelList/f8023e128a9420fa16eefecd580efe4024d42c05/app/src/main/res/drawable/work_divile_line5.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/work_divile_line6.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oldbirdy/MultilevelList/f8023e128a9420fa16eefecd580efe4024d42c05/app/src/main/res/drawable/work_divile_line6.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/work_divile_line_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 |