草庐IT

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

coder 2023-12-17 原文

我创建了一棵树作为双重可扩展 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/

有关如果列表太大,Android 双可扩展 ListView 会隐藏项目的更多相关文章

  1. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  2. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  3. ruby - 使用 C 扩展开发 ruby​​gem 时,如何使用 Rspec 在本地进行测试? - 2

    我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当

  4. ruby - RVM 使用列表[0] - 2

    是否有类似“RVMuse1”或“RVMuselist[0]”之类的内容而不是键入整个版本号。在任何时候,我们都会看到一个可能包含5个或更多ruby的列表,我们可以轻松地键入一个数字而不是X.X.X。这也有助于rvmgemset。 最佳答案 这在RVM2.0中是可能的=>https://docs.google.com/document/d/1xW9GeEpLOWPcddDg_hOPvK4oeLxJmU3Q5FiCNT7nTAc/edit?usp=sharing-知道链接的任何人都可以发表评论

  5. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  6. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  7. ruby-on-rails - 新 Rails 项目 : 'bundle install' can't install rails in gemfile - 2

    我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="

  8. ruby - 如果指定键的值在数组中相同,如何合并哈希 - 2

    我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat

  9. ruby-on-rails - 如果我将 ruby​​ 版本 2.5.1 与 rails 版本 2.3.18 一起使用会怎样? - 2

    如果我使用ruby​​版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby​​1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更

  10. Ruby 从大范围中获取第 n 个项目 - 2

    假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit

随机推荐