Android的Treeview

我知道有ExpandableListView但它只支持2级。 我需要一个真正的treeview垂直列表,至less达到5级(更多更好)。

有什么build议么?

编辑:

我看到有关使用自定义适配器并根据项目级别设置填充的说明。

我有一个未分类的具有ID和父ID的对象的ArrayList,我也dynamic地添加项目到这个数组。

任何人都可以给我一些如何去做这个事情的例子吗?

我遇到过同样的问题。 你可以看看我的实现AndroidTreeView 。

  • 它的N级树。

  • 自定义风格的节点

  • 旋转后保存状态

AndroidTreeView

我们公司也为此开放了一个解决scheme。 它可以作为图书馆使用,非常易于使用: http : //code.google.com/p/tree-view-list-android/

在这里输入图像说明

我解决了我的问题,在类似的线程发布: 其他线程

在这里输入图像说明

回答我自己的问题,因为我们在几个月前执行了这个问题。

我们在一个开源项目中的实现。

我发现下面的链接非常有用。 它描述了在二维数据结构(通常是数据库表)中存储树结构的替代方法。

我想你会发现容易理解和实施的范例。

http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

至于如何在Android中可视化这是另一个问题。 如果“填充”列表项解决scheme不足,我可能会从头开始编写自己的小部件。

我认为,如果多级可扩展列表正确完成,它实际上工作,看起来不错。 这是另一个http://code.google.com/p/tree-view-list-android/的例子;

在这里输入图像说明

我已经find了一个更容易解决这个问题的方法,因为我自己在编码技能方面有些中等。 在我的情况下,我需要一个以Windows为主题的树视图,经过头脑风暴的想法,我几乎没有任何编码就能实现!

这里有一个诀窍:使用WebView和embedded式HTML页面来显示自定义树视图,并使用Android超便利的JavaScript通信接口来接收select和点击: Android-er Blog上的概念validation示例

有了这个function,我们可以利用networking上大量的JS / CSS控件片断。 有风格的Windows7风格的jQuery TreeView控件 – jsTree

很多Android的可能性和力量在那里,快乐的编码!

我同意pjv,至less对于手机尺寸的设备。 最好是组织一个ListView中一次显示一组同胞的小部件。 这可以在跟踪其在树中的位置的单个活动中完成。 它可以显示一个包含面包屑的标题,显示当前正在显示的项目的父项的path。

多级树形图可能适用于平板电脑设备,但手机没有足够的空间来支持所提议的5个级别(所有事情都必须足够大,以支持手指)。

不过,如果你在树视图中设置,不要看ExpandableListView的子类。 它通过将父子索引(每个int)包装成一个长整数而在内部运行。 这种内在的performance使得事实上不可能超越两个层面。

我将首先让数据结构更具代表性地包含它的含义。 既然你有一个项目的数组,每个孩子可以有它自己的项目数组,每个都有自己的数组等。我会考虑使用一个有两个成员的类:一个对象,表示这个特定项目的数据和一个数组持有该项目的孩子。 每个孩子本身就是这个class级的一个实例,所以它也可以有孩子。

private class ParentAndKids {Object parent; 数组孩子; }

然后,您的适配器将有一个表示顶层的ParentAndKids对象的数组。 您将根据父母的展开项目来添加和移除列表项目。

package com.expand.search; import android.app.ExpandableListActivity; import android.os.Bundle; import android.view.ContextMenu; import android.view.Gravity; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; import android.widget.Toast; import android.widget.ExpandableListView.ExpandableListContextMenuInfo; /** Demonstrates expandable lists using a custom {@link ExpandableListAdapter} * from {@link BaseExpandableListAdapter}. */ public class expands extends ExpandableListActivity { ExpandableListAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set up our adapter mAdapter = new MyExpandableListAdapter(); setListAdapter(mAdapter); registerForContextMenu(getExpandableListView()); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("Sample menu"); menu.add(0, 0, 0, R.string.expandable_list_sample_action); } @Override public boolean onContextItemSelected(MenuItem item) { ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo(); String title = ((TextView) info.targetView).getText().toString(); int type = ExpandableListView.getPackedPositionType(info.packedPosition); if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos, Toast.LENGTH_SHORT).show(); return true; } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show(); return true; } return false; } /** A simple adapter which maintains an ArrayList of photo resource Ids. * Each photo is displayed as an image. This adapter supports clearing the * list of photos and adding a new photo. */ public class MyExpandableListAdapter extends BaseExpandableListAdapter { // Sample data set. children[i] contains the children (String[]) for groups[i]. private String[] groups = { "Category1", "Category2", "Category3", "Category4" }; private String[][] children = { { "Charity1", "Charity2", "Charity3", "Charity4" }, { "Charity5", "Charity6", "Charity7", "Charity8" }, { "Charity9", "Charity10" }, { "Charity11", "Charity12" } }; public Object getChild(int groupPosition, int childPosition) { return children[groupPosition][childPosition]; } public long getChildId(int groupPosition, int childPosition) { return childPosition; } public int getChildrenCount(int groupPosition) { return children[groupPosition].length; } public TextView getGenericView() { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 64); TextView textView = new TextView(expands.this); textView.setLayoutParams(lp); // Center the text vertically textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // Set the text starting position textView.setPadding(36, 0, 0, 0); return textView; } public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getGenericView(); textView.setText(getChild(groupPosition, childPosition).toString()); return textView; } public Object getGroup(int groupPosition) { return groups[groupPosition]; } public int getGroupCount() { return groups.length; } public long getGroupId(int groupPosition) { return groupPosition; } public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView textView = getGenericView(); textView.setText(getGroup(groupPosition).toString()); return textView; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } public boolean hasStableIds() { return true; } } 

}