无限级别列表视图

时间:2016-10-12 07:48:41

标签: java android listview expandablelistview

我正在尝试使用可扩展listview的自定义实现创建一个无限可扩展的列表视图。 我的代码基于How to display more than 3- levels of expandable List View?

不幸的是,我的listview只显示三个级别,即使树中包含更多嵌套项目,也无法扩展到该级别。

这是我的代码:

public class RootAdapter extends BaseExpandableListAdapter {

private UserObject root;

private final LayoutInflater listLayoutInflater;

public class Entry {
    public final CustExpListview cls;
    public final SecondLevelAdapter sadpt;

    public Entry(CustExpListview cls, SecondLevelAdapter sadpt) {
        this.cls = cls;
        this.sadpt = sadpt;
    }
}

public Entry[] lsfirst;

// you can change the constructor depending on which listeners you wan't to use.
public RootAdapter(Context context, UserObject root, ExpandableListView.OnGroupClickListener grpLst,
                   final ExpandableListView.OnChildClickListener childLst, ExpandableListView.OnGroupExpandListener grpExpLst) {
    this.root = root;
    this.listLayoutInflater = LayoutInflater.from(context);

    lsfirst = new Entry[root.mChildren.size()];

    for (int i = 0; i < root.mChildren.size(); i++) {
        final CustExpListview celv = new CustExpListview(context);
        SecondLevelAdapter adp = new SecondLevelAdapter(root.mChildren.get(i),context);
        celv.setLongClickable(true);
        celv.setAdapter(adp);
        celv.setGroupIndicator(null);
        celv.setOnChildClickListener(childLst);
        celv.setOnGroupClickListener(grpLst);
        celv.setOnGroupExpandListener(grpExpLst);

        lsfirst[i] = new Entry(celv, adp);
    }

}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return root.mChildren.get(groupPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                         View convertView, ViewGroup parent) {
    // second level list
    return lsfirst[groupPosition].cls;
}

@Override
public int getChildrenCount(int groupPosition) {
    return 1;
}

@Override
public UserObject getGroup(int groupPosition) {
    return root.mChildren.get(groupPosition);
}

@Override
public int getGroupCount() {
    return root.mChildren.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                         ViewGroup parent) {

    // first level

    View layout = convertView;
    GroupViewHolder holder;
    final UserObject item = (UserObject) getGroup(groupPosition);

    if (layout == null) {
        layout = listLayoutInflater.inflate(R.layout.root_element, parent, false);
        holder = new GroupViewHolder();
        holder.title = (TextView) layout.findViewById(R.id.rootTitle);
        layout.setTag(holder);
    } else {
        holder = (GroupViewHolder) layout.getTag();
    }

    holder.title.setText(item.mUserName.trim());

    return layout;
}

private static class GroupViewHolder {
    TextView title;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

接下来是我的第二级适配器实现(即:叶节点)

    public class SecondLevelAdapter extends BaseExpandableListAdapter {

public UserObject child;
Context mContext;
LayoutInflater inflater;

public SecondLevelAdapter(UserObject child,Context context) {
    this.child = child;
    this.mContext=context;
    inflater = LayoutInflater.from(mContext);
}

@Override
public UserObject getChild(int groupPosition, int childPosition) {
    return child.mChildren.get(groupPosition).mChildren.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

// third level
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                         View convertView, ViewGroup parent) {
    View layout = convertView;
    final UserObject item = (UserObject) getChild(groupPosition, childPosition);

    ChildViewHolder holder;

    if (layout == null) {
        layout = inflater.inflate(R.layout.item_child, parent, false);

        holder = new ChildViewHolder();
        holder.title = (TextView) layout.findViewById(R.id.childTitle);
        layout.setTag(holder);
    } else {
        holder = (ChildViewHolder) layout.getTag();
    }

    holder.title.setText(item.mUserName.trim());

    return layout;
}

@Override
public int getChildrenCount(int groupPosition) {
    return child.mChildren.get(groupPosition).mChildren.size();
}

@Override
public UserObject getGroup(int groupPosition) {
    return child.mChildren.get(groupPosition);
}

@Override
public int getGroupCount() {
    return child.mChildren.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

// Second level
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                         ViewGroup parent) {
    View layout = convertView;
    ViewHolder holder;

    final UserObject item = (UserObject) getGroup(groupPosition);

    if (layout == null) {
        layout = inflater.inflate(R.layout.root_element, parent, false);
        holder = new ViewHolder();
        holder.title = (TextView) layout.findViewById(R.id.rootTitle);
        layout.setTag(holder);
    } else {
        holder = (ViewHolder) layout.getTag();
    }

    holder.title.setText(item.mUserName.trim());

    return layout;
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {
    super.registerDataSetObserver(observer);
}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
    Log.d("SecondLevelAdapter", "Unregistering observer");
    if (observer != null) {
        super.unregisterDataSetObserver(observer);
    }
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

private static class ViewHolder {
    TextView title;
}

private static class ChildViewHolder {
    TextView title;
}

}

然后我在主要活动中调用此代码:

    public class Test extends navBar {
private List<UserObject> mUserObjects;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View base = getLayoutInflater().inflate(R.layout.activity_admin__tool, frameLayout);
     mUserObjects = new ArrayList<>();

    UserObject  obj = new UserObject();
    obj.mChildren =  new ArrayList<>();
    for(int i = 0;i<Constant.state.length;i++) {
        root = new UserObject();
        root.mUserName = Constant.state[i];
        root.mChildren = new ArrayList<>();
        for (int j = 0; j < Constant.parent[i].length; j++) {
            UserObject parent = new UserObject();
            parent.mUserName = Constant.parent[i][j];
            parent.mChildren = new ArrayList<>();
            for (int k = 0; k < Constant.child[i][j].length; k++) {
                UserObject child = new UserObject();
                child.mUserName = Constant.child[i][j][k];
                UserObject test = new UserObject();
                test.mUserName = "test";
                child.mChildren.add(test);
                parent.mChildren.add(child);
            }
            root.mChildren.add(parent);
        }
        obj.mChildren.add(root);
    }
    if (!obj.mChildren.isEmpty()) {
        final ExpandableListView elv = (ExpandableListView) base.findViewById(R.id.expandableListView);
    /* Item click listeners below */

        try {
            // First level items in the ExpandableListView
            elv.setLongClickable(true);
            elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                @Override
                public boolean onGroupClick(ExpandableListView eListView, View view, int groupPosition,
                                            long id) {
                    ImageView tv = (ImageView) view.findViewById(R.id.rootImage);
                    if(!eListView.isGroupExpanded(groupPosition)){
                        tv.setImageResource(R.drawable.ic_expand_less_black_24dp);
                    }
                    else
                        tv.setImageResource(R.drawable.ic_expand_more_black_24dp);

                    return false /* or true depending on what you need */;
                }
            });

            elv.setOnItemLongClickListener(new ExpandableListView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View childView, int flatPos, long id) {
                    if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
                        // do whatever you want with groupPos and childPos here - I used these to get my object from list adapter.
                        return false;
                    }
                    return true;
                }
            });

        }
        catch (Exception e){
            Log.e("Admin_Tool",e.getMessage());
        }
        // Second level items in the ExpandableListView
        ExpandableListView.OnGroupClickListener grpLst = new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView eListView, View view, int groupPosition,
                                        long id) {
                ImageView tv = (ImageView) view.findViewById(R.id.rootImage);
                if(!eListView.isGroupExpanded(groupPosition)){
                    tv.setImageResource(R.drawable.ic_expand_less_black_24dp);
                }
                else
                    tv.setImageResource(R.drawable.ic_expand_more_black_24dp);
                return false /* or true depending on what you need */;
            }
        };

        // Third (and last) level items in the ExpandableListView
        ExpandableListView.OnChildClickListener childLst = new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView eListView, View view, int groupPosition,
                                        int childPosition, long id) {
                // TODO: whatever you need
                return false /* or true depending on what you need */;
            }
        };

        ExpandableListView.OnGroupExpandListener grpExpLst = new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
            /* this one is not required of course, you can delete it from the RootAdapter Constructor
             * it is just an example as to how to implement Listeners on the second level items */
            }
        };

        final RootAdapter adapter = new RootAdapter(this, obj, grpLst, childLst, grpExpLst);
        elv.setAdapter(adapter);
    }

}

最后,常量设置如下:

    public class Constant {
static String[] state = {"A","B","C"};
static  String[][] parent = {
        {"aa","bb","cc","dd","ee"},
        {"ff","gg","hh","ii","jj"},
        {"kk","ll","mm","nn","oo"}
};

static  String[][][] child = {
        {
                {"aaa","aab","aac","aad","aae"},
                {"bba","bbb","bbc","bbd","bbe"},
                {"cca","ccb","ccc","ccd","cce","ccf","ccg"},
                {"dda","ddb","dddc","ddd","dde","ddf"},
                {"eea","eeb","eec"}
        },
        {
                {"ffa","ffb","ffc","ffd","ffe"},
                {"gga","ggb","ggc","ggd","gge"},
                {"hha","hhb","hhc","hhd","hhe","hhf","hhg"},
                {"iia","iib","iic","iid","iie","ii"},
                {"jja","jjb","jjc","jjd"}
        },
        {
                {"kka","kkb","kkc","kkd","kke"},
                {"lla","llb","llc","lld","lle"},
                {"mma","mmb","mmc","mmd","mme","mmf","mmg"},
                {"nna","nnb","nnc","nnd","nne","nnf"},
                {"ooa","oob"}
        }
};
}

我将“test”添加为所有第三级节点的节点,以确保它将测试第四级节点。因此,一个例子是a-&gt; aa&gt; aaa&gt;测试 测试永远不会显示,并且aaa始终被视为最终节点。 我如何超越三个级别?

2 个答案:

答案 0 :(得分:2)

结帐此库:AndroidTreeView

为项目添加依赖项

compile 'com.github.bmelnychuk:atv:1.2.+'

创建树

TreeNode root = TreeNode.root();

添加节点(使用自定义对象作为构造函数参数)

TreeNode parent = new TreeNode("MyParentNode");
TreeNode child0 = new TreeNode("ChildNode0");
TreeNode child1 = new TreeNode("ChildNode1");
parent.addChildren(child0, child1);
root.addChild(parent);

将树视图添加到布局

AndroidTreeView tView = new AndroidTreeView(getActivity(), root);
containerView.addView(tView.getView());

答案 1 :(得分:0)

您好here is a full tutorial for listview and cache items

我希望这篇文章可以帮到你。祝你好运