2012-09-24 19 views
5

He creado un árbol como una vista doble lista expansible, uno de los elementos que tiene demasiados hijos (15) más que los demás, no muestra todos sus hijos.La lista desplegable ampliable de Android oculta los elementos si la lista es demasiado grande

_ ______ 
+|_______| 
    _______ 
    +|_______| 
     _______ 
     |_______| 
     _______ 
     |_______| 
      ... 
      ... 
     _______ 
     |_______| // 11-th item 
     _______ // 12-th item can be seen a little and it-s clickable 
    _______ 
    +|_______| 
    _______ 
    +|_______| 
    ... 
    ... 
    _______ 
    +|_______| 

Después del 12 ° artículo, la lista se corta. No he puesto ninguna restricción en el tamaño de la lista. ¿Alguna idea de por qué sucede esto? Gracias!

EDIT Este es el código de la primera lista ampliable.

<ExpandableListView android:layout_width="fill_parent" android:id="@+id/ParentLevel" 
       android:groupIndicator="@null" android:choiceMode="singleChoice" 
       android:layout_height="fill_parent" 
       android:layout_above="@id/button_layout" 
       android:paddingRight="5dp"> 

La otra sublista se añaden en el método() adaptador getView.

ejemplo de código

//MainActivity.java 
public class MainActivity extends Activity implements OnChildClickListener { 

ExpandableListView elv; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // QUI SOTTO 
    Tree root = new Tree("root", 123); 

    for (int i = 0; i < 3; i++) { 
     Tree firstLevel = new Tree(("firstLevel" + i), i); 

     for (int j = 0; j < 6; j++) { 
      Tree secondLevel = new Tree("secondLevel" + j, j); 

      for (int z = 0; z < 27; z++) { 
       Tree thirdLevel = new Tree("thirdLevel" + z, z); 

       secondLevel.addChild(thirdLevel); 
      } 

      firstLevel.addChild(secondlevel); 
     } 

     root.addChild(firstLevel); 
    } 


    elv = new ExpandableListView(this); 
    final TreeAdapter treeAdapter = new TreeAdapter(this, root, this); 
    elv.setAdapter(treeAdapter); 
    elv.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE); 

    elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { 

     @Override 
     public boolean onGroupClick(ExpandableListView parent, View v, 
       int groupPosition, long id) { 

      for (int i = 0; i < treeAdapter.getGroupCount(); i++) { 
       elv.collapseGroup(i); 
      } 

      elv.expandGroup(groupPosition); 
      return true; 
     } 
    }); 

    setContentView(elv); 
    elv.expandGroup(0); 

    Entry e = treeAdapter.lsfirst[0]; 

    e.cls.expandGroup(1); 

    treeAdapter.selectedClass = 33; 

} 

TreeAdapter.java

public class TreeAdapter extends BaseExpandableListAdapter { 

final static String TAG = TreeAdapter.class.getSimpleName(); 


static public Integer selectedClass = null; 

static int CHILD_PADDING; 
static float TEXT_SIZE; 


private Tree tree; 

private OnChildClickListener lst; 

final private Context context; 


class Entry{ 
    final CustExpListview cls; 
    final SecondLevelAdapter sadtp; 
    public Entry(CustExpListview cls, SecondLevelAdapter sadtp) { 
     this.cls = cls; 
     this.sadtp = sadtp; 
    } 
} 

Entry[] lsfirst; 


public TreeAdapter(MainActivity ctx, Tree tree, OnChildClickListener lst){ 
    this.context = ctx; 
    this.tree = tree;  
    this.lst = lst; 
    TEXT_SIZE = 35; 
    CHILD_PADDING = 40; 

    lsfirst = new Entry[tree.getChilds().size()]; 

    for(int i=0; i<tree.getChilds().size();i++){ 
      CustExpListview SecondLevelexplv = new CustExpListview(context); 
      SecondLevelAdapter adp = new  
         SecondLevelAdapter(tree.getChilds().get(i)); 
      SecondLevelexplv.setAdapter(adp); 
      SecondLevelexplv.setGroupIndicator(null); 
      SecondLevelexplv.setOnChildClickListener(lst);// add listener 

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

    } 


} 



    @Override 
    public Object getChild(int arg0, int arg1){    
      return arg1; 
    } 

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

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

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

    @Override 
    public Object getGroup(int groupPosition) { 
    return groupPosition; 
    } 

    @Override 
    public int getGroupCount() { 
    return tree.getChilds().size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 

    return tree.getChilds().get(groupPosition).getId(); 
    } 

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

     //firstlevel 

    TextView tv = new TextView(context); 
    tv.setText(tree.getChilds().get(groupPosition).getName()); 
    tv.setPadding(3, 7, 7, 7); 

    return tv; 
    } 

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

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


    ///////////////////////////////////////////////////////////////////////////// 

    //_____________-------------________----------________---------_______-----// 

    ///////////////////////////////////////////////////////////////////////////// 


    public class CustExpListview extends ExpandableListView{ 

      int intGroupPosition, intChildPosition, intGroupid; 

      public CustExpListview(Context context) 
      { 
      super(context); 

      this.setSelector(R.drawable.divider_gray); 

      } 

      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
      { 
      widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST); 
      heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST); 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
      } 
      } 

      public class SecondLevelAdapter extends BaseExpandableListAdapter{ 

       Tree tree2; 
       public SecondLevelAdapter(Tree tr) { 
        this.tree2 = tr; 
       } 

        //Returns the id of the selected class 
        @Override 
        public Object getChild(int groupPosition, int childPosition) {     
    return tree2.getChilds().get(groupPosition).getChilds().get(childPosition).getId(); 
        } 

        @Override 
        public long getChildId(int groupPosition, int childPosition) { 
         return tree2.getChilds().get(groupPosition).getChilds().get(childPosition).getId(); 
        } 

        @Override 
        public View getChildView(int groupPosition, int childPosition, 
              boolean isLastChild, View convertView, ViewGroup parent) { 
         //thirdlevel 
         TextView tv = new TextView(context); 
         tv.setText(tree2.getChilds().get(groupPosition).getChilds().get(childPosition).getName()); 

         if(selectedClass!=null && selectedClass ==tree2.getChilds().get(groupPosition).getChilds().get(childPosition).getId()){ 
          tv.setBackgroundResource(R.drawable.divider_gray); 

         } 
         tv.setPadding(2*CHILD_PADDING, 5, 300, 5); 

        return tv; 
        } 

        @Override 
        public int getChildrenCount(int groupPosition){ 
        return tree2.getChilds().get(groupPosition).getChilds().size(); 
        } 

        @Override 
        public Object getGroup(int groupPosition) { 
        return groupPosition; 
        } 

        @Override 
        public int getGroupCount() { 
        return tree2.getChilds().size(); 
        } 

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

        @Override 
        public View getGroupView(int groupPosition, boolean isExpanded, 
        View convertView, ViewGroup parent) 
        {// secondlevel 
        TextView tv = new TextView(context); 
        tv.setText(tree2.getChilds().get(groupPosition).getName()); 
        tv.setPadding(CHILD_PADDING, 7, 200, 7); 

        return tv; 
        } 

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

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

    } 

} 

Tree.java

public final class Tree { 

final static String TAG=Tree.class.getSimpleName(); 

final private String name; 
final private int id; 

private Tree parent; 
private LinkedList<Tree> childs = new LinkedList<Tree>(); 


public Tree(String name, int id) { 
    parent = null; 
    this.name = name; 
    this.id = id; 
} 

public void addChild(Tree child){ 
    child.parent = this; 
    childs.add(child); 
} 


public int getId() { 
    return id; 
} 

public String getName() { 
    return name; 
} 

public Tree getParent() { 
    return parent; 
} 

public LinkedList<Tree> getChilds() { 
    return childs; 
} 



@Override 
public String toString() { 
    Iterator<Tree> iter = childs.iterator(); 
    String childs = "["; 
    while(iter.hasNext()){ 
     Tree ch = iter.next(); 
     childs = childs.concat(ch+","); 
    } 
    childs = childs.concat("]"); 
    return name + " "+ id + childs; 
} 


} 
+0

¿Podría compartir sus archivos xml de diseño? Gracias. – UgglyNoodle

+0

Hola, agregué el código, si puedes probar y ver qué sucede, tiene que mostrar 26 niños pero muestra solo 21 y hace desaparecer a todos los otros niños del segundo nivel ... No entiendo este comportamiento ... gracias – linnal

Respuesta

1

No he probado el código, pero mirando el método de onMeasure() su CustExpListview, veo que ha especificado un ancho y altura máximos. Si elimina la altura máxima, entonces debería poder adaptarse a todos los niños.

+0

muchas gracias! No noté esa parte del código: \ – linnal

+0

@eu problema ur se resolvió o no también estoy enfrentando el mismo problema – sri

+0

@sri Sí, era un problema del método onMeasure(), como se dijo anteriormente – linnal

Cuestiones relacionadas