获取setadapter后的ListView高度 [英] Get ListView Height after setadapter

查看:177
本文介绍了获取setadapter后的ListView高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,我试图让我的ListView的高度后,我改变它的数据,但它总是返回之前的高度,而不是实际的。

所以,当我设置setadapter,并得到旧值。
例如:

 的ActualHeight = 100
 变更数据(过滤器) - GT; NewHeight = 60
 ListView.GetHeight仍然返回100。 再次 的ActualHeight = 60
 变更数据(过滤器) - GT; NewHeight = 20
 ListView.GetHeight仍然返回60。

在code我使用的是:

  INT宽度,高度= 0;                    的EditText edt_search;
                    ListView的lv_marca;                    清单<&马卡GT; list_marca =新的ArrayList<&马卡GT;();
                    清单<&马卡GT; list_marca_search =新的ArrayList<&马卡GT;();                    字符串文本= edt_search.getText()的toString()。
                    list_marca_search.clear();                    如果(text.startsWith(。)){
                        文本= text.replace(,。);
                        对于(马卡玛:list_marca){
                            如果(将String.valueOf(m.getCd_marca())。等于(文本)){
                                list_marca_search.add(米);
                            }
                        }
                    }其他{
                        对于(马卡玛:lista_marca){
                            如果(m.getDs_marca()。包含(文本)){
                                list_marca_search.add(米);
                            }
                        }
                    }                    ArrayAdapter<&马卡GT; adapter_marca =新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();
                    身高= lv_marca.getHeight()+ getSupportActionBar()的getHeight();                    如果(高度> = height_window){
                        身高=(INT)(hei​​ght_window * 0.95);
                    }                    getWindow()的setLayout(宽度,高度)。


解决方案

好吧,我找到了解决办法。

当我改变SetAdapter在我的ListView我得到的测量高度,这意味着一条线,并通过行号的行号加分频器高度繁衍了。

下面是一个例子,我怎么做,我不知道,如果是更好的方式,但它完美^^:

 的ListView lv_marca;    lv_marca.setAdapter(adapter_marca);    INT list_height = getListViewHeight(lv_marca);    私人诠释getListViewHeight(ListView控件列表){
          ListAdapter适配器= 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());          返回listviewHeight;
    }

well, I'm trying to get my listview height after I change it data, but it always return the prior height, not the actual.

So when I set the setadapter, and it get the old value. e.g:

 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.

The code i'm using is it:

                    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);

解决方案

Ok, I found the solution.

When I change the SetAdapter in my ListView I get the measure height that means one line, and multiply by the line numbers plus the divider height by the line numbers too.

Here is a example how I did, I don't know if is the better way but it works perfectly ^^:

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

这篇关于获取setadapter后的ListView高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆