如果列表太大,Android双扩展列表视图会隐藏项目

时间:2012-09-24 16:26:27

标签: android expandablelistview

我创建了一个树作为双重可扩展列表视图,其中一个项目比其他项目有更多的孩子(15),并没有显示他的所有孩子。

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

在第12个项目之后,列表被删除。我没有对列表的大小施加任何约束。 知道为什么会这样吗? 谢谢!

修改 这是第一个可扩展列表的代码。

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

在适配器getView()方法中添加了另一个子列表。

代码示例

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


}

1 个答案:

答案 0 :(得分:1)

我尚未测试您的代码,但查看onMeasure()的{​​{1}}方法,我发现您指定了最大宽度和高度。如果你移除了最大高度,那么它应该能够适合所有孩子。