2016-10-12 4 views
0

私は、拡張可能なリストビューのカスタム実装を使用して無限にスケーリング可能なリストビューを作成しようとしています。 How to display more than 3- levels of expandable List View?に私のコードを基づいています。無限レベルリストビュー

残念ながら、私のリストビューは3つのレベルしか表示せず、ツリーにネストされたアイテムがさらに含まれていてもそれを超えることはできません。ここで

は私のコードです:

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"} 
     } 
}; 
} 

「テスト」をすべての第3レベルノードのノードとして追加して、第4レベルのテストを確実に行うようにしました。したがって、a-> aaa-> testの例があります。 テストは表示されず、aaaは常に最終ノードとして扱われます。 どのようにして3つのレベルを超えて行くことができますか?

答えて

2

チェックアウトこのライブラリー:AndroidTreeView

プロジェクトに依存関係を追加し

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

あなたのツリーを作成します

TreeNode root = TreeNode.root(); 

あなたのノードを追加します(コンストラクタのparamとして、カスタムオブジェクトを使用)

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());