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

标签 android expandablelistview

我创建了一棵树作为双重可扩展 ListView ,其中一个项目的子项 (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;

}

树适配器.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;
                  }               

    }

}

树.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;
}


}

最佳答案

我没有测试过您的代码,但是查看您的CustExpListviewonMeasure() 方法,我发现您指定了最大宽度和高度。如果去掉最大高度,那么它应该能够容纳所有的 child 。

关于如果列表太大,Android 双可扩展 ListView 会隐藏项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12569109/

相关文章:

具有回收 View 和详细信息 View 的 fragment 之间的 Android 共享元素转换

带按钮的 Android ExpandableListView 父级

android - SimpleExpandableListAdapter 中的参数是如何利用的?

android - ExpandableListView in ViewFlipper显示问题?

android - PSPDFKit - 创建新文档时应用程序崩溃

php - 接收 JSON 列表时出错

android - 可以在 x 和 y 坐标上跟踪 SeekBar 的拇指吗?

android - Kotlin 协程执行令人困惑 : where to call await()?

android - 为 ExpandableListView 设置 onChildClickListener 时出现问题

java - ExpandableListView 更改第一个 subview 的布局