android多级树形列表,阿里面试100%会问到的JVM
}}
@Override
public void onCheckChange(Node node, int position,
List<Node> checkedNodes) {
StringBuffer sb = new StringBuffer();
for (Node n : checkedNodes) {
int pos = n.getId() - 1;
sb.append(mDatas.get(pos).getName()).append("---")
.append(pos + 1).append(";");
}
Toast.makeText(getApplicationContext(), sb.toString(),
Toast.LENGTH_SHORT).show();
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
treeLv.setAdapter(adapter);
}
private void initDatas() {
mDatas.add(new MyNodeBean(1, 0, "中国古代"));
mDatas.add(new MyNodeBean(2, 1, "唐朝"));
mDatas.add(new MyNodeBean(3, 1, "宋朝"));
mDatas.add(new MyNodeBean(4, 1, "明朝"));
mDatas.add(new MyNodeBean(5, 2, "李世民"));
mDatas.add(new MyNodeBean(6, 2, "李白"));
mDatas.add(new MyNodeBean(7, 3, "赵匡胤"));
mDatas.add(new MyNodeBean(8, 3, "苏轼"));
mDatas.add(new MyNodeBean(9, 4, "朱元璋"));
mDatas.add(new MyNodeBean(10, 4, "唐伯虎"));
mDatas.add(new MyNodeBean(11, 4, "文征明"));
mDatas.add(new MyNodeBean(12, 7, "赵建立"));
mDatas.add(new MyNodeBean(13, 8, "苏东东"));
mDatas.add(new MyNodeBean(14, 10, "秋香"));
}
}
Adapter
这个adapter是继承了自己的定义的一个TreeListViewAdapter,核心实现都是在TreeListViewAdapter这个里面
package com.example.customtreeviewdemo;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListVi
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整资料开源分享
ew;
import android.widget.TextView;
import com.example.customtreeviewdemo.tree.Node;
import com.example.customtreeviewdemo.tree.TreeListViewAdapter;
public class MyTreeListViewAdapter<T> extends TreeListViewAdapter<T> {
public MyTreeListViewAdapter(ListView mTree, Context context,
List<T> datas, int defaultExpandLevel,boolean isHide)
throws IllegalArgumentException, IllegalAccessException {
super(mTree, context, datas, defaultExpandLevel,isHide);
}
@SuppressWarnings("unchecked")
@Override
public View getConvertView(Node node, int position, View convertView,
ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.icon = (ImageView) convertView
.findViewById(R.id.id_treenode_icon);
viewHolder.label = (TextView) convertView
.findViewById(R.id.id_treenode_name);
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.id_treeNode_check);
convertView.setTag(viewHolder);
} else
{
viewHolder = (ViewHolder) convertView.getTag();
}
if (node.getIcon() == -1)
{
viewHolder.icon.setVisibility(View.INVISIBLE);
} else
{
viewHolder.icon.setVisibility(View.VISIBLE);
viewHolder.icon.setImageResource(node.getIcon());
}
if(node.isHideChecked()){
viewHolder.checkBox.setVisibility(View.GONE);
}else{
viewHolder.checkBox.setVisibility(View.VISIBLE);
setCheckBoxBg(viewHolder.checkBox,node.isChecked());
}
viewHolder.label.setText(node.getName());
return convertView;
}
private final class ViewHolder
{
ImageView icon;
TextView label;
CheckBox checkBox;
}
/**
[*]checkbox是否显示
[*]@param cb
[*]@param isChecked
*/
private void setCheckBoxBg(CheckBox cb,boolean isChecked){
if(isChecked){
cb.setBackgroundResource(R.drawable.check_box_bg_check);
}else{
cb.setBackgroundResource(R.drawable.check_box_bg);
}
}
}
自定义TreeListViewAdapter ?这个是整个树形结构的一个适配器,这里面主要是实现对Node节点的操作 点击,选中改变 更新等
package com.example.customtreeviewdemo.tree;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
/**
[*]tree适配器
[*]@param <T>
*/
public abstract class TreeListViewAdapter<T> extends BaseAdapter {
protected Context mContext;
/**
[*]存储所有可见的Node
*/
protected List<Node> mNodes;
protected LayoutInflater mInflater;
/**
[*]存储所有的Node
*/
protected List<Node> mAllNodes;
/**
[*]点击的回调接口
*/
private OnTreeNodeClickListener onTreeNodeClickListener;
public interface OnTreeNodeClickListener {
/**
[*]处理node click事件
[*]@param node
[*]@param position
*/
void onClick(Node node, int position);
/**
[*]处理checkbox选择改变事件
[*]@param node
[*]@param position
[*]@param checkedNodes
*/
void onCheckChange(Node node, int position,List<Node> checkedNodes);
}
public void setOnTreeNodeClickListener(
OnTreeNodeClickListener onTreeNodeClickListener) {
this.onTreeNodeClickListener = onTreeNodeClickListener;
}
/**
[*]
[*]@param mTree
[*]@param context
[*]@param datas
[*]@param defaultExpandLevel
[*]默认展开几级树
[*]@throws IllegalArgumentException
[*]@throws IllegalAccessException
*/
public TreeListViewAdapter(ListView mTree, Context context, List<T> datas,
int defaultExpandLevel, boolean isHide)
throws IllegalArgumentException, IllegalAccessException {
mContext = context;
/**
[*]对所有的Node进行排序
*/
mAllNodes = TreeHelper
.getSortedNodes(datas, defaultExpandLevel, isHide);
/**
[*]过滤出可见的Node
*/
mNodes = TreeHelper.filterVisibleNode(mAllNodes);
mInflater = LayoutInflater.from(context);
/**
[*]设置节点点击时,可以展开以及关闭;并且将ItemClick事件继续往外公布
*/
mTree.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
expandOrCollapse(position);
if (onTreeNodeClickListener != null) {
onTreeNodeClickListener.onClick(mNodes.get(position),
position);
}
}
});
}
/**
[*]相应ListView的点击事件 展开或关闭某节点
[*]
[*]@param position
/
public void expandOrCollapse(int position) {
Node n = mNodes.get(position);
if (n != null)// 排除传入参数错误异常
{
if (!n.isLeaf()) {
n.setExpand(!n.isExpand());
mNodes = TreeHelper.filterVisibleNode(mAllNodes);
notifyDataSetChanged();// 刷新视图
}
}
}
@Override
public int getCount() {
return mNodes.size();
}
@Override
public Object getItem(int position) {
return mNodes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Node node = mNodes.get(position);
convertView = getConvertView(node, position, convertView, parent);
// 设置内边距
convertView.setPadding(node.getLevel() 30, 3, 3, 3);
if (!node.isHideChecked()) {
//获取各个节点所在的父布局
RelativeLayout myView = (RelativeLayout) convertView;
//父布局下的CheckBox
CheckBox cb = (CheckBox) myView.getChildAt(1);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
TreeHelper.setNodeChecked(node, isChecked);
List<Node> checkedNodes = new ArrayList<Node>();
for(Node n:mAllNodes){
if(n.isChecked()){
checkedNodes.add(n);
}
}
onTreeNodeClickListener.onCheckChange(node,position,checkedNodes);
TreeListViewAdapter.this.notifyDataSetChanged();
}
});
}
return convertView;
}
public abstract View getConvertView(Node node, int position,
View convertView, ViewGroup parent);
/**
[*]更新
[*]@param isHide
*/
public void updateView(boolean isHide){
for(Node node:mAllNodes){
node.setHideChecked(isHide);
}
this.notifyDataSetChanged();
}
}
node 模型类
package com.example.customtreeviewdemo.bean;
public class MyNodeBean {
/**
[*]节点Id
*/
private int id;
/**
[*]节点父id
*/
private int pId;
/**
[*]节点name
*/
private String name;
/**
[*]
*/
private String desc;
/**
[*]节点名字长度
*/
private long length;
public MyNodeBean(int id, int pId, String name) {
super();
this.id = id;
this.pId = pId;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pId;
}
public void setPid(int pId) {
this.pId = pId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
}
TreeHelper这个也是核心操作类,主要功能是将业务数据和节点数据进行匹配
package com.example.customtreeviewdemo.tree;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import com.example.customtreeviewdemo.R;
public class TreeHelper {
/**
[*]根据所有节点获取可见节点
[*]
[*]@param allNodes
[*]@return
*/
public static List<Node> filterVisibleNode(List<Node> allNodes) {
List<Node> visibleNodes = new ArrayList<Node>();
for (Node node : allNodes) {
// 如果为根节点,或者上层目录为展开状态
if (node.isRoot() || node.isParentExpand()) {
setNodeIcon(node);
结尾
好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。
这里放上一部分我工作以来以及参与过的大大小小的面试收集总结出来的一套进阶学习的视频及面试专题资料包,主要还是希望大家在如今大环境不好的情况下面试能够顺利一点,希望可以帮助到大家~
https://s2.51cto.com/images/20211217/1639744542552777.jpg
https://s2.51cto.com/images/20211217/1639744543422123.jpg
本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录
</div>
<div id="asideoffset"></div>
https://blog.51cto.com/u_15450974/4812252
页:
[1]