I am trying to create a listview that is infinitely scaleable using a custom implementation of expandable listview. I have based my code on How to display more than 3- levels of expandable List View?.
Unfortunately, my listview only displays three levels, and cannot expand beyond that even if the tree contains more nested items.
Here is my code:
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;
}
}
And then the following for my second-level adapter implementation (ie: leaf node)
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;
}
}
I then call this code in the main activity:
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);
}
}
Lastly, constants are set up like this:
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"}
}
};
}
I added "test" as a node to all third level nodes to ensure that it would test for fourth level. So an example would be a->aa->aaa->test Test never displays, and aaa is always treated as the final node. How do I go past three levels?