加载列表之前确定的ListView高度 [英] Determine ListView height before loading the list

查看:140
本文介绍了加载列表之前确定的ListView高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能确定一个ListView的高度,它是在屏幕上呈现前?如果我的ListView是有,说3项,高度,只限于这3项(和ListView控件不占用整个屏幕的高度),我能确定的高度Android版会显示在屏幕上的项目之前,<? / p>

解决方案

使用下面的方法来获取和设置基于儿童present高度

 私有静态无效setListViewHeightBasedOnChildren(ListView控件iListView){
        ListAdapter listAdapter = iListView.getAdapter();
        如果(listAdapter == NULL)
            返回;        INT desiredWidth = View.MeasureSpec.makeMeasureSpec(iListView.getWidth(),View.MeasureSpec.UNSPECIFIED);
        INT totalHeight = 0;
        查看查看= NULL;
        的for(int i = 0; I&LT; listAdapter.getCount();我++){
            鉴于= listAdapter.getView(我认为,iListView);
            如果(我== 0)
                view.setLayoutParams(新ViewGroup.LayoutParams(desiredWidth,RelativeLayout.LayoutParams.WRAP_CONTENT));            view.measure(desiredWidth,View.MeasureSpec.UNSPECIFIED);
            totalHeight + = view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams PARAMS = iListView.getLayoutParams();
        params.height = totalHeight +(iListView.getDividerHeight()*(listAdapter.getCount() - 1));
        Log.d(TAG,高度的ListView ---+ params.height);
        iListView.setLayoutParams(PARAMS);
        iListView.requestLayout();
    }

Is it possible to determine the height of a ListView before it is rendered on the screen? If my ListView were to have, say 3 items, and the height is confined to these 3 items (and the ListView doesn't take up the entire screen's height), can I determine the height before Android displays the items on the screen?

解决方案

Use the following method to get and set the height based upon children present

 private static void setListViewHeightBasedOnChildren(ListView iListView) {
        ListAdapter listAdapter = iListView.getAdapter();
        if (listAdapter == null)
            return;

        int desiredWidth = View.MeasureSpec.makeMeasureSpec(iListView.getWidth(), View.MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            view = listAdapter.getView(i, view, iListView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, RelativeLayout.LayoutParams.WRAP_CONTENT));

            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = iListView.getLayoutParams();
        params.height = totalHeight + (iListView.getDividerHeight() * (listAdapter.getCount() - 1));
        Log.d("TAG", "ListView Height --- "+params.height);
        iListView.setLayoutParams(params);
        iListView.requestLayout();
    }

这篇关于加载列表之前确定的ListView高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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