7.3、代码实现:
树形控件的数据对象TreeNodeData:
/**
- 树形控件数据类(会用于页面间传输,所以需实现Serializable 或 Parcelable)
- 作者:齐行超
- 日期:2019.08.07
*/
public class TreeNodeData implements Serializable {
//名称
private String name;
//页码
private int pageNum;
//是否已展开(用于控制树形节点图片显示,即箭头朝向图片)
private boolean isExpanded;
//展示级别(1级、2级…,用于控制树形节点缩进位置)
private int treeLevel;
//子集(用于加载子节点,也用于判断是否显示箭头图片,如集合不为空,则显示)
private List subset;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public boolean isExpanded() {
return isExpanded;
}
public void setExpanded(boolean expanded) {
isExpanded = expanded;
}
public int getTreeLevel() {
return treeLevel;
}
public void setTreeLevel(int treeLevel) {
this.treeLevel = treeLevel;
}
public List getSubset() {
return subset;
}
public void setSubset(List subset) {
this.subset = subset;
}
}
树形控件适配器 : TreeAdapter
/**
- 树形控件适配器
- 作者:齐行超
- 日期:2019.08.07
*/
public class TreeAdapter extends RecyclerView.Adapter<TreeAdapter.TreeNodeViewHolder> {
//上下文
private Context context;
//数据
public List data;
//展示数据(由层级结构改为平面结构)
public List displayData;
//treelevel间隔(dp)
private int maginLeft;
//委托对象
private TreeEvent delegate;
/**
- 构造函数
- @param context 上下文
- @param data 数据
*/
public TreeAdapter(Context context, List data) {
this.context = context;
this.data = data;
maginLeft = UIUtils.dip2px(context, 20);
displayData = new ArrayList<>();
//数据转为展示数据
dataToDiaplayData(data);
}
/**
- 数据转为展示数据
- @param data 数据
*/
private void dataToDiaplayData(List data) {
for (TreeNodeData nodeData : data) {
displayData.add(nodeData);
if (nodeData.isExpanded() && nodeData.getSubset() != null) {
dataToDiaplayData(nodeData.getSubset());
}
}
}
/**
- 数据集合转为可显示的集合
*/
private void reDataToDiaplayData() {
if (this.data == null || this.data.size() == 0) {
return;
}
if(displayData == null){
displayData = new ArrayList<>();
}else{
displayData.clear();
}
dataToDiaplayData(this.data);
notifyDataSetChanged();
}
@Override
public TreeNodeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.tree_item, null);
return new TreeNodeViewHolder(view);
}
@Override
public void onBindViewHolder(TreeNodeViewHolder holder, int position) {
final TreeNodeData data = displayData.get(position);
//设置图片
if (data.getSubset() != null) {
holder.img.setVisibility(View.VISIBLE);
if (data.isExpanded()) {
holder.img.setImageResource(R.drawable.arrow_h);
} else {
holder.img.setImageResource(R.drawable.arrow_v);
}
} else {
holder.img.setVisibility(View.INVISIBLE);
}
//设置图片偏移位置
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.img.getLayoutParams();
int ratio = data.getTreeLevel() <= 0? 0 : data.getTreeLevel()-1;
params.setMargins(maginLeft * ratio, 0, 0, 0);
holder.img.setLayoutParams(params);
//显示文本
holder.title.setText(data.getName());
holder.pageNum.setText(String.valueOf(data.getPageNum()));
//图片点击事件
holder.img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//控制树节点展开、折叠
data.setExpanded(!data.isExpanded());
//刷新数据源
reDataToDiaplayData();
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//回调结果
if(delegate!=null){
delegate.onSelectTreeNode(data);
}
}
});
}
@Override
public int getItemCount() {
return displayData.size();
}
/**
- 定义RecyclerView的ViewHolder对象
*/
class TreeNodeViewHolder extends RecyclerView.ViewHolder {
ImageView img;
TextView tit