【问题标题】:Android Treeview安卓树视图
【发布时间】:2010-07-17 14:03:31
【问题描述】:

我知道有ExpandableListView,但它最多只支持 2 个级别。我需要一个真正的树视图垂直列表,至少有大约 5 个级别(越多越好)。

有什么建议吗?

编辑:

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

我有一个未排序的 ArrayList 对象,这些对象有一个 ID 和父 ID,我也动态地将项目添加到这个数组中。

谁能给我一些例子来说明我该如何去做?

【问题讨论】:

标签: java android treeview


【解决方案1】:

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

  • 它的 N 级树。

  • 节点的自定义样式

  • 旋转后保存状态

【讨论】:

  • 谢谢它的帮助,我可以添加一个水平图标的水平,
  • 可以动态修改视图吗?具体来说,如果我最初创建包含端点(叶)节点的树,我以后可以将其更改为文件夹吗?
【解决方案2】:

我们公司还为此开源了一个解决方案。它作为库提供,非常易于使用:http://code.google.com/p/tree-view-list-android/

【讨论】:

  • 你的开源项目中没有下载链接
【解决方案3】:

我为我解决了这个问题,在类似的帖子中发帖: other thread

【讨论】:

    【解决方案4】:

    回答我自己的问题,因为我们是在几个月前实施的。

    Our implementation 在一个开源项目中。

    【讨论】:

    【解决方案5】:

    我找到了一个更简单的解决方案来解决这个问题,因为我自己的编码技能有点中等。在我的情况下,我需要一个以 Windows 为主题的树视图,在集思广益之后,我几乎不用任何编码就能实现它!

    诀窍是:使用 WebView 和嵌入的 HTML 页面来显示自定义树视图,并使用 Android 超级方便的 JavaScript 通信接口来接收选择和点击:Proof of Concept Example on Android-er Blog

    有了这个功能,我们可以利用网络上大量的 JS/CSS 控件 sn-ps。 Themeable Windows7-Style jQuery TreeView control -- jsTree

    Android 提供了很多可能性和强大功能,祝您编码愉快!

    【讨论】:

    • 解决这个问题的最耗费资源的解决方案。但我担心这就是我们行业的未来。
    【解决方案6】:

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

    我认为您会发现该范例易于理解和实施。

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

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

    【讨论】:

    • 非常感谢您修改链接@Jarrod Dixon!自从 Oracle 删除了(最初)免费提供的知识资源(源自 MySQL 开发人员,MySQL 后来被 Sun 收购,而 Sun 又——不幸的是,如果你问我——被 Oracle 收购)我花了相当多的时间来寻找它的替代资源(但显然不够“可观的时间”)。再次感谢!
    【解决方案7】:

    我认为,如果多级可扩展列表做得正确,它实际上可以工作并且看起来很棒。 这是http://code.google.com/p/tree-view-list-android/的另一个例子

    【讨论】:

    • 嗨,多萨!我正在尝试从谷歌代码实现这个树视图。既然你已经实现了它,你能帮我解决一些关于这方面的问题吗?如果可以,请给我敲门 fahim.ahmed1988@gmail.com 。谢谢!
    【解决方案8】:

    我同意 pjv,至少对于手机大小的设备。最好将小部件组织为在 ListView 中一次显示一组兄弟姐妹。这可以在单个活动中完成,该活动跟踪其在树中的位置。它可以显示带有面包屑的标题,显示当前显示的项目的父项的路径。

    多级树形视图可能适用于平板设备,但手机没有足够的空间来支持建议的 5 级(所有东西都必须足够大以供手指使用)。

    尽管如此,如果您设置在树视图上,请不要查看子类化 ExpandableListView。它通过将父索引和子索引(每个都是 int)打包成一个 long 来在内部运行。这种内部表示几乎不可能扩展到超过 2 个级别。

    【讨论】:

      【解决方案9】:
      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;
          }
      }
      

      }

      【讨论】:

      • 我们可以在这里嵌套超过 2 次..??
      【解决方案10】:

      我首先要让数据结构更能代表它所包含的内容。由于您有一个项目数组,并且每个孩子都可以拥有自己的项目数组,每个项目都有自己的数组,等等。我会考虑使用一个有两个成员的类:一个表示这个特定项目的数据的对象和一个数组持有该项目的孩子。每个孩子本身都是该类的一个实例,因此它也可以有孩子。

      私人课程 ParentAndKids { 对象父级; 排列孩子; }

      然后,您的适配器将拥有一个 ParentAndKids 对象数组,这些对象代表顶层。您将根据展开的父项添加和删除列表项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多