在setadapter之后获取ListView高度

时间:2012-09-13 16:48:17

标签: android android-listview

好吧,我在更改数据后尝试获取listview高度,但它总是返回先前的高度,而不是实际高度。

所以当我设置setadapter时,它会获得旧值。 例如:

 ActualHeight = 100
 Change data (filter) -> NewHeight = 60
 ListView.GetHeight still returns 100.

 Again

 ActualHeight = 60
 Change data (filter) -> NewHeight = 20
 ListView.GetHeight still returns 60.

我正在使用的代码是:

                    int width, height = 0;

                    EditText edt_search;
                    ListView lv_marca;

                    List<Marca>  list_marca        = new ArrayList<Marca>();
                    List<Marca>  list_marca_search = new ArrayList<Marca>();

                    String text = edt_search.getText().toString(); 


                    list_marca_search.clear();

                    if(text.startsWith(".")){
                        text = text.replace(".", "");
                        for (Marca m : list_marca) {
                            if (String.valueOf(m.getCd_marca()).equals(text)){
                                list_marca_search.add(m);
                            }
                        }
                    } else {
                        for (Marca m : lista_marca) {
                            if (m.getDs_marca().contains(text)){
                                list_marca_search.add(m);
                            }
                        }
                    }

                    ArrayAdapter<Marca> adapter_marca = new CustomAdapter_Marca(MyDialog.this, R.layout.layout_consulta_estoque_marca_lista, list_marca_search);

                    lv_marca.setAdapter(adapter_marca);

                    int height_window  = getWindowManager().getDefaultDisplay().getHeight();
                    height             = lv_marca.getHeight() + getSupportActionBar().getHeight();

                    if (height >= height_window) {
                        height = (int) (height_window * 0.95);
                    }

                    getWindow().setLayout(width, height);

2 个答案:

答案 0 :(得分:13)

好的,我找到了解决方案。

当我在ListView中更改SetAdapter时,我得到的测量高度表示一行,并乘以行号加上除号高度除以行号。

这是我做的一个例子,我不知道它是否是更好的方式,但它完美地运作^^:

    ListView lv_marca; 

    lv_marca.setAdapter(adapter_marca);

    int list_height = getListViewHeight(lv_marca);



    private int getListViewHeight(ListView list) {
          ListAdapter adapter = list.getAdapter();

          int listviewHeight = 0;

          list.measure(MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED), 
                       MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

          listviewHeight = list.getMeasuredHeight() * adapter.getCount() + (adapter.getCount() * list.getDividerHeight());

          return listviewHeight;
    }

答案 1 :(得分:1)

您必须在列表视图中添加OnHierarchyChangeListener,以便在列表中添加或删除子项时可以获取列表视图的新高度值:

lv_marca.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() 
{
    @Override
    public void onChildViewRemoved(View parent, View child) 
    {
        // This will give you the height when your new adapter contains 
        // lesser number of items than the previous one
        int heightAfterItemRemoved=parent.getHeight();
        Log.i("list_view_height_change","Height after removing item : "+heightAfterItemRemoved);
    }

    @Override
    public void onChildViewAdded(View parent, View child) 
    {
        // This will give you the height when your new adapter contains 
        // greater number of items than the previous one
        int heightAfterItemAdded=parent.getHeight();
        Log.i("list_view_height_change","Height after adding item : "+heightAfterItemAdded);                
    }
});

有一个问题 - 这只有在列表视图的'layout_height'属性设置为'wrap_content'时才有效。在这种情况下,列表视图的高度分别随着项目的添加或删除而增加或减少。列表视图完全填充后,列表视图的高度达到最大值。如果向列表视图添加更多项目,它将溢出并可滚动,但高度值不会更改,因为它已占用其父视图中的最大允许高度。

如果将'layout_height'设置为'fill_parent'或'match_parent',列表视图将从一开始就占据其父视图中的最大允许高度,因此,删除或添加项目不会导致其高度发生任何变化