相关链接
Android 打造任意层级树形控件 考验你的数据结构和设计
android ListView 实现3级节点 (可拓展N级)
第一篇年代有点久了,需要约束数据格式;第二篇来自这篇的灵感,只是逻辑不一样,万恶的转载万恶的百度,文章各网站都是,一开始找到的源码乱码,找到原文时自己都写好了,原理都是对数据进行包装处理。本篇是自己迭代时删除,相对来说效率更高,同时增加展开全部和折叠全部。原文章是调用系统removeAll()删除,removeAll()最终也是迭代删除,只是在逻辑上跟容易理解。
源码实现
数据模型
package com.silicar.sample;
import android.content.Intent;
import java.util.List;
/**
* Created by Brady on 2016/7/9.
*/
public class DataVo {
String title;
Intent intent;
List<DataVo> dataVoList;
public DataVo() {
}
public DataVo(String title, Intent intent, List<DataVo> dataVoList) {
this.title = title;
this.intent = intent;
this.dataVoList = dataVoList;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Intent getIntent() {
return intent;
}
public void setIntent(Intent intent) {
this.intent = intent;
}
public List<DataVo> getDataVoList() {
return dataVoList;
}
public void setDataVoList(List<DataVo> dataVoList) {
this.dataVoList = dataVoList;
}
}
数据包装
package com.silicar.sample;
import java.util.List;
/**
* Created by Brady on 2016/7/8.
*/
public class DataWrapper<T> {
//存在下级列表
private boolean hasChild;
//折叠状态
private boolean isExpand;
//列表等级
private int level;
//上一级位置
private int parentPosition;
//当前等级位置
private int position;
//数据
private T data;
//下级数据
private List<T> childList;
public DataWrapper() {
}
public DataWrapper(boolean hasChild, boolean isExpand, int level, int parentPosition, int position, T data) {
this.hasChild = hasChild;
this.isExpand = isExpand;
this.level = level;
this.parentPosition = parentPosition;
this.position = position;
this.data = data;
}
public boolean isHasChild() {
return hasChild;
}
public void setHasChild(boolean hasChild) {
this.hasChild = hasChild;
}
public boolean isExpand() {
return isExpand;
}
public void setExpand(boolean expand) {
isExpand = expand;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getParentPosition() {
return parentPosition;
}
public void setParentPosition(int parentPosition) {
this.parentPosition = parentPosition;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public List<T> getChildList() {
return childList;
}
public void setChildList(List<T> childList) {
this.childList = childList;
}
}
Adapter实现
package com.silicar.sample;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.silicar.library.adapter.CommonAdapter;
import com.silicar.library.adapter.CommonViewHolder;
import com.silicar.library.adapter.ModelCommonAdapter;
import com.silicar.sample.databinding.ItemSampleListBinding;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Created by Brady on 2016/7/8.
*/
public class SampleListAdapter extends ModelCommonAdapter<DataWrapper> {
public SampleListAdapter(Context context) {
this(context, R.layout.item_sample_list);
}
public SampleListAdapter(Context context, int layoutId) {
super(context, layoutId);
adapter = new CommonAdapter<DataWrapper>(context, list, layoutId) {
@Override
public void onBindViewHolder(CommonViewHolder holder, DataWrapper item, int position) {
ItemSampleListBinding binding = holder.getBinding();
holder.itemView.setTag(POSITION, position);
holder.itemView.setOnClickListener(mOnItemClickListener);
}
};
}
/**
* 改变展开状态
*
* @param position
* @return
*/
public boolean changeExpand(int position) {
DataWrapper<DataVo> vo = list.get(position);
//Log.e("Expand", "~" + vo.isExpand());
//Log.e("HasChild", "~" + vo.isHasChild());
if (vo.isHasChild()) {
if (!vo.isExpand()) {
addList(vo.getChildList(), vo.getLevel() + 1, vo.getPosition());
} else {
removeList(vo.getLevel(), vo.getPosition());
}
vo.setExpand(!vo.isExpand());
return true;
}
return false;
}
/**
* 添加(展开)列表
*
* @param dataVoList 列表
* @param level 等级
* @param parentPosition 上一级位置
*/
public void addList(List<DataVo> dataVoList, int level, int parentPosition) {
List<DataWrapper> dataWrapperList = new ArrayList<>();
for (int i = 0; i < dataVoList.size(); i++) {
DataVo vo = dataVoList.get(i);
DataWrapper<DataVo> dataWrapper = new DataWrapper<>(false, false, level, parentPosition, i, vo);
if (vo.getDataVoList() != null && !vo.getDataVoList().isEmpty()) {
dataWrapper.setHasChild(true);
dataWrapper.setChildList(vo.getDataVoList());
//addList(vo.getDataVoList(), level + 1, i);
}
dataWrapperList.add(dataWrapper);
}
if (level == 1) {
list.addAll(dataWrapperList);
} else {
DataWrapper<DataVo> vo;
for (int i = 0; i < list.size(); i++) {
vo = list.get(i);
if (vo.getLevel() == level - 1 && vo.getPosition() == parentPosition) {
list.addAll(i + 1, dataWrapperList);
break;
}
}
}
}
/**
* 删除(折叠)列表
*
* @param level 等级
* @param parentPosition 位置
*/
public void removeList(int level, int parentPosition) {
Iterator<DataWrapper> iterator = list.iterator();
removeList(iterator, level, parentPosition, -1);
}
/**
* 删除(折叠)列表
*
* @param iterator
* @param level 等级
* @param parentPosition 位置
* @param oldPosition 上一级位置
*/
private void removeList(Iterator<DataWrapper> iterator, int level, int parentPosition, int oldPosition) {
DataWrapper<DataVo> vo;
while (iterator.hasNext()) {
vo = iterator.next();
Log.e("level", "~" + vo.getLevel());
Log.e("ParentPosition", "~" + vo.getParentPosition());
Log.e("position", "~" + parentPosition);
if (vo.getLevel() == level + 1 && vo.getParentPosition() == parentPosition) {
iterator.remove();
//存在下级并且已展开
if (vo.isHasChild() && vo.isExpand()) {
removeList(iterator, vo.getLevel(), vo.getPosition(), parentPosition);
}
} else if (vo.getLevel() == level && vo.getParentPosition() == oldPosition) {
//存在待删除的上一级(只有-1位置表示不存在上一级)
iterator.remove();
break;
} else if (vo.getLevel() == level && vo.getPosition() > parentPosition) {
//超出删除位置,结束遍历
break;
}
}
}
//展开全部
public void expandAll(List<DataVo> dataVoList, int level, int parentPosition) {
removeList(level, parentPosition);
List<DataWrapper> dataWrapperList = new ArrayList<>();
addExpandAll(dataVoList, dataWrapperList, level, parentPosition);
if (level == 1) {
list.addAll(dataWrapperList);
} else {
DataWrapper<DataVo> vo;
for (int i = 0; i < list.size(); i++) {
vo = list.get(i);
if (vo.getLevel() == level - 1 && vo.getPosition() == parentPosition) {
list.addAll(i + 1, dataWrapperList);
break;
}
}
}
}
private void addExpandAll(List<DataVo> dataVoList, List<DataWrapper> dataWrapperList, int level, int parentPosition){
for (int i = 0; i < dataVoList.size(); i++) {
DataVo vo = dataVoList.get(i);
DataWrapper<DataVo> dataWrapper = new DataWrapper<>(false, false, level, parentPosition, i, vo);
if (vo.getDataVoList() != null && !vo.getDataVoList().isEmpty()) {
dataWrapper.setHasChild(true);
dataWrapper.setChildList(vo.getDataVoList());
addExpandAll(vo.getDataVoList(), dataWrapperList, level + 1, i);
}
dataWrapperList.add(dataWrapper);
}
}
//折叠全部
public void foldAll(int level, int parentPosition) {
Iterator<DataWrapper> iterator = list.iterator();
DataWrapper<DataVo> vo;
while (iterator.hasNext()) {
vo = iterator.next();
if (vo.getLevel() == level && vo.getParentPosition() == parentPosition) {
if (vo.isHasChild() && vo.isExpand()) {
removeList(iterator, vo.getLevel(), vo.getPosition(), parentPosition);
}
} else if (vo.getLevel() == level -1 && vo.getPosition() > parentPosition) {
break;
}
}
}
@Override
protected void onItemClick(View view, int position) {
Toast.makeText(mContext, "" + position, Toast.LENGTH_SHORT).show();
}
@Override
protected boolean onItemLongClick(View view, int position) {
return false;
}
@Override
protected void onChildClick(View view, int position) {
}
@Override
protected boolean onChildLongClick(View view, int position) {
return false;
}
}
后记
完整内容或源码呢?
伸手党就当路过没看见,本文很早之前的文章没写完,现在不想继续写了,以上贴出的代码已经给出关键的代码并有注释,请自行实现。
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。转载请保留作者及原文链接