我知道有ExpandableListView,但它只支持2级。我需要一个真正的树形结构垂直列表,至少支持5级(支持更多更好)。
有什么建议吗?
编辑:
我看到有人谈论使用自定义适配器,并根据项目级别设置填充。
我有一个未排序的对象ArrayList,其中包含ID和父ID,并且我也会动态添加项目到此数组中。
有人可以给我一些关于如何做到这一点的示例吗?
我知道有ExpandableListView,但它只支持2级。我需要一个真正的树形结构垂直列表,至少支持5级(支持更多更好)。
有什么建议吗?
编辑:
我看到有人谈论使用自定义适配器,并根据项目级别设置填充。
我有一个未排序的对象ArrayList,其中包含ID和父ID,并且我也会动态添加项目到此数组中。
有人可以给我一些关于如何做到这一点的示例吗?
我们公司也开源了一个解决方案。它作为库可供使用,非常容易使用:http://code.google.com/p/tree-view-list-android/
回答自己的问题,因为我们在很多月前实施了这个。
我发现下面的链接非常有用,它描述了将树结构存储在二维数据结构中(通常是数据库表)的替代方法。
我认为您会发现这种范例易于理解和实现。
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
如何在Android中可视化这个问题,则另说。如果"填充"列表项的解决方案不足够,我可能会从头开始编写自己的小部件。
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;
}
}
}
我同意pjv的观点,至少对于手机尺寸的设备来说是这样。最好的方法是在ListView中组织小部件,一次只显示一个兄弟姐妹组。这可以在单个活动中完成,该活动跟踪其在树中的位置。它可以显示面包屑导航的标题,显示当前显示项目的父项路径。
多级树视图可能适用于平板电脑,但手机没有足够的空间来支持所提议的5个级别(每个级别都必须足够大,以适应手指)。
尽管如此,如果您坚持要使用树形视图,请不要考虑子类化ExpandableListView。它通过将父项和子项索引(每个int)打包到单个长整型中来进行内部操作。这种内部表示使得扩展超过2个级别几乎不可能。