I.准备工作
1.建立一个Activity,对应处理一个XML。如果在这个XML中需要显示树。那么首先需要的条件是:
(1)在XML中必须空一个位置用于放置树,如(以教室布置作业为例),这个空间应该是一个ListView:
<!-- 左边树 -->
<RelativeLayout android:layout_width="110dip"
android:layout_height="fill_parent">
<!-- 滚动条 -->
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_alignParentLeft="true">
<LinearLayout android:layout_width="match_parent"
android:id="@+id/linearLayout1" android:layout_height="match_parent">
<ListView android:id=" :id/list"
android:layout_width="wrap_content" android:layout_height="400dip" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
(2)控制该XML的Activity类必须继承自ListActivity即(批改作业为实例):
public class CorrectingHomework extends ListActivity{
......
}
II.树的数据结构(类)
1.一个树的结点需要包括的数据至少有以下几个,加入将树结点类定于为:TreePoint,那么需要的属性有:
private String id; //树的id,用于找到是哪个结点
private String outlineTitle ; //结点的标题
private boolean mhasParent; //该结点是否有父结点
private boolean mhasChild ; //该结点是否有孩子结点
private String parent; //父结点id
private int level; //该结点在书中处于那个等级
III.为树添加数据以及树的显示
1.树的数据需要用两个列表存储。第一个存储当前需要显示的所有数据,第二个列表我们存储这颗树的所有结点数据
(1)存储当前需要显示的类的列表和存储所有结点列表声明如下:
private ArrayList<TreePoint> mTreeBoyList = new ArrayList<TreePoint>(); //需要显示的
private ArrayList<TreePoint> ArrayList = new ArrayList<TreePoint>(); //所有结点
(2)装载数据过程和显示过程
// 树的建立
// 初始化数据
CorrectingHomeworkTreePoint CorrectingHomeworkTreePoint = new CorrectingHomeworkTreePoint(
this, mTreeBoyList, ArrayList);
// 适配器配置
treeViewAdapter = new CorrectingHomeworkTreeAdapter(this,
R.layout.treenull, mTreeBoyList);
setListAdapter(treeViewAdapter);
(3)细看树的数据初始化过程详情查看文件:package com.rt.jxufe.ui.correctinghomework大致内容:
在数据库中查找想要在树状结构中显示的数据,将第一次需要显示的数据放入mTreeBoyList中,将所有需要显示的数据放入ArrayList中。
//建立头结点
int i = 1;
for(HomeworkAssign eg_homeworkassign:object_homeworkassign){
//创建节点的ID,标题
String treep_id = Integer.toString(i);
String treep_outlineTitle = eg_homeworkassign.getHomeworkTitle();
//创建新节点 并初始化:是否有父节点=false,是否有孩子结点=false,level=0,扩展=false
TreePoint treep = new TreePoint(treep_id,treep_outlineTitle,false,false,"00",0,false);
headnode.add(treep);
arraylisttreepoint.add(treep);
i++;
}
//建立其他所有节点
for(HomeworkScore eg_homeworkscore:object_homeworkscore){
for(TreePoint treepoint:headnode){
//找到属于哪一个节点的孩子
if(eg_homeworkscore.getHomeworkAssign().getHomeworkTitle().equals(treepoint.getOutlineTitle())){
//创建节点的ID,标题,是否有父亲节点,父亲节点ID
String treep_id = Integer.toString(i++);
String treep_outlineTitle = eg_homeworkscore.getStudent().getId()+":"+eg_homeworkscore.getStudent().getName();
boolean mhasParent = true;
String parent = treepoint.getId();
treepoint.setMhasChild(true);//将父亲节点有孩子设置为真
//创建新节点 并初始化:是否有孩子结点=false,level=0,扩展=false
TreePoint treep = new TreePoint(treep_id,treep_outlineTitle,mhasParent,false,parent,1,false);
arraylisttreepoint.add(treep);
break;
}
}
}
(4)再来看看适配器的配置详情:
<1>这里面用到一个:R.layout.treenull,这个treenull的XML是给树配置的一个显示的模版东东。没什么大勇但是必须要的。内容如下:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="20px"
android:textSize="14sp"
>
</TextView>
<2>用到一个树结点布局的XML,这个布局决定树每个结点排布和显示方式,比如左边是图片,右边是文字的形式如下:<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView android:id="@+id/icon" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_marginRight="6.0dip"
android:layout_alignParentTop="true" android:layout_alignParentBottom="true" />
<TextView android:textAppearance="?android:textAppearanceMedium"
android:gravity="center_vertical" android:id="@+id/text" android:textColor="#000000"
android:textSize="12px"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:singleLine="true" android:layout_toRightOf="@id/icon"
android:layout_alignParentTop="true" android:layout_alignParentBottom="true" />
</RelativeLayout>
<3>适配器设置主要是为了显示需要显示的那部分数据,显示的形式以及显示结点的形式:
class CorrectingHomeworkTreeAdapter extends ArrayAdapter {
public CorrectingHomeworkTreeAdapter(Context context,
int textViewResourceId, List objects) {
super(context, textViewResourceId, objects);
mInflater = LayoutInflater.from(context);
mfilelist = objects;
mIconCollapse = BitmapFactory.decodeResource(context.getResources(),
R.drawable.outline_list_collapse);
mIconExpand = BitmapFactory.decodeResource(context.getResources(),
R.drawable.outline_list_expand);
}
private LayoutInflater mInflater;
private List<TreePoint> mfilelist;
private Bitmap mIconCollapse;
private Bitmap mIconExpand;
public int getCount() {
return mfilelist.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;// class ViewHolder {TextView text;ImageView icon;}
/* if (convertView == null) { */
convertView = mInflater/* 布局充气 */.inflate(R.layout.treepoint, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);// 将holder添加到画布
/*
* } else { holder = (ViewHolder) convertView.getTag(); }
*/
int level = mfilelist.get(position).getLevel();
holder.icon.setPadding(25 * (level + 1), holder.icon.getPaddingTop(),
0, holder.icon.getPaddingBottom());
holder.text.setText(mfilelist.get(position).getOutlineTitle());
if (mfilelist.get(position).isMhasChild() // 右孩子且没有扩展,那么需要设置左边有图且为加好
&& (mfilelist.get(position).isExpanded() == false)) {
holder.icon.setImageBitmap(mIconCollapse);
} else if (mfilelist.get(position).isMhasChild()
&& (mfilelist.get(position).isExpanded() == true)) {
// 有孩子且已经扩展了,则需要设置左边的图为减号holder.icon.setImageBitmap(mIconExpand);
} else if (!mfilelist.get(position).isMhasChild()) { // 没有孩子则设置图片不可见
holder.icon.setImageBitmap(mIconCollapse);
holder.icon.setVisibility(View.INVISIBLE);
}
return convertView;
}
class ViewHolder {
TextView text;
ImageView icon;
}
}
相关技术帖子: