自己实现的android树控件treeview
本文导语: 1.开发原因在项目中经常需要一个需要一个树状框架,这是非常常见的控件。不过可能是谷歌考虑到android是手机系统,界面宽度有限,所以只提供了只有二级的ExpandableListView。虽然这个控件可以满足很多需求,但是无数级的树...
1.开发原因
在项目中经常需要一个需要一个树状框架,这是非常常见的控件。不过可能是谷歌考虑到android是手机系统,界面宽度有限,所以只提供了只有二级的ExpandableListView。虽然这个控件可以满足很多需求,但是无数级的树在某些情况下还是需要的,所以我花了一天时间(大部分时间都在调试动画去了,不过现在动画还有点问题,具体原因不明。。如果某位大神能找到原因灰常感谢)。
2.原理
网上很多都是扩展listview实现的,不过listview貌似不支持复杂控件的事件?而且做动画也不方便,所有我决定扩展linearlayout,在里面增加子节点的方式实现。
3.代码
TreeView.java:
package net.memornote.android.ui.view;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
public class TreeView extends LinearLayout{
// private List items;
private List sortedItems;
private int animTime;
public TreeView(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(LinearLayout.VERTICAL);
}
/**
* initialize data,you must make sure that each item has parent except the top ones.
* @param items the data to show
* @param index the index of the tree to insert to
* @param viewHeight each view's height
* @param animTime if you want expand animation,
* you can set the time(ms) of animation,otherwise you can set 0.
*
*/
public void initData(List items,int index){
if(items==null||items.size()==0){
return ;
}
sortItemList(items);
int size=sortedItems.size();
initAddIndex=index0){
List list=item.getChildrens();
for (TreeItem it : list) {
View view=it.getView();
if(!it.isShow){
continue;
}
// removeViewAt(viewIndex);
TranslateAnimation ta=new TranslateAnimation(0, 0, 0, 0);
ta.setFillAfter(true);
ta.setDuration(1000);
view.startAnimation(ta);
// viewIndex++;
removeCount++;
it.isShow=false;
view.setVisibility(View.GONE);
if(isAnim){
animHeight+=it.getViewHeight();
}
if(isRecurse){
removeChild(it,true);
}
}
}
}
private void animAdd(){
TranslateAnimation ta=new TranslateAnimation(
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, animHeight,
Animation.ABSOLUTE, 0);
ta.setFillBefore(true);
// ta.setFillAfter(false);
ta.setDuration(animTime);
for (int i = viewIndex+1; i < getChildCount(); i++) {
View view=getChildAt(i);
view.startAnimation(ta);
}
animHeight=0;
}
private void animRemove(){
TranslateAnimation ta=new TranslateAnimation(
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, animHeight,
Animation.ABSOLUTE, 0);
ta.setFillAfter(false);
ta.setFillBefore(true);
ta.setDuration(animTime);
int startAnimIndex;
startAnimIndex=viewIndex+1;
for (int i = startAnimIndex; i < getChildCount(); i++) {
View view=getChildAt(i);
view.startAnimation(ta);
}
animHeight=0;
}
public void expand(TreeItem item){
viewIndex=indexOfChild(item.getView());
addChild(item,false);
if(isAnim){
animAdd();
}
}
public void expandAllChildren(TreeItem item) {
viewIndex=indexOfChild(item.getView());
addChild(item,true);
if(isAnim){
animAdd();
}
}
public void expandAll(){
if(sortedItems==null){
return ;
}
for (TreeItem item : sortedItems) {
expandAllChildren(item);
}
}
public void contractAllChildren(TreeItem item) {
viewIndex=indexOfChild(item.getView())+1;
removeChild(item,true);
if(isAnim){
animRemove();
}
}
public void contractAll(){
if(sortedItems==null){
return ;
}
for (TreeItem item : sortedItems) {
contractAllChildren(item);
}
}
public void bind(TreeItem item) {
if(item.nextIsExpand){
expand(item);
}else {
contractAllChildren(item);
}
item.nextIsExpand=!item.nextIsExpand;
}
}
TreeItem.java
package net.memornote.android.ui.view;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
public class TreeItem {
private View view;
private TreeItem parent;
private List childrens=new ArrayList(0);
public boolean nextIsExpand=true;
public boolean isShow=false;
private int viewHeight;
public TreeItem(){}
public TreeItem(int id,View view, TreeItem parent) {
super();
this.view = view;
this.parent = parent;
}
public View getView() {
if(view!=null){
view.setPadding(getLevel()*20,0,0,0);
}
return view;
}
public void setView(View view) {
this.view = view;
}
public TreeItem getParent() {
return parent;
}
public void setParent(TreeItem parent) {
this.parent = parent;
}
/**
* 动态获取该节点的级数
* @return
*/
public int getLevel() {
int level=0;
TreeItem localParent=parent;
while (localParent!=null) {
level++;
localParent=localParent.getParent();
}
return level;
}
public List getChildrens() {
return childrens;
}
public int getViewHeight() {
if(view==null||view.getHeight()==0){
return viewHeight;
}
return view.getHeight();
}
public void setViewHeight(int viewHeight) {
this.viewHeight = viewHeight;
}
}
测试代码:
package net.memornote.android.ui;
import net.memornote.android.R;
import net.memornote.android.ui.fragment.MainFragment;
import net.memornote.android.ui.fragment.MenuFragment;
import com.actionbarsherlock.view.Menu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import android.os.Bundle;
import android.view.KeyEvent;
public class MainActivity extends SlidingFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slid_content_frame);
tv_notebook=(TreeView) findViewById(R.id.tv_notebook);
List items=new ArrayList();
initData(items);
tv_notebook.initData(items, 0);
tv_notebook.enabledAnim(500);
}
//初始化一些测试数据
public void initData(List items) {
for (int i=0;i