Android ListView的每一个item布局高度不一样,自动计算listView高度

一、android listView的每一个item布局高度不一样

1.获取listView的宽度

2.给item的measure设置参数是listView的宽度就可以获取到真正每一个item的高度

	public void setListViewHeightBasedOnChildren(ListView listView1) {
        BaseAdapter listAdapter = (BaseAdapter) listView1.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        //获取listView的宽度
        ViewGroup.LayoutParams params = listView1.getLayoutParams();
        int  listViewWidth  = getActivity().getWindowManager().getDefaultDisplay().getWidth();
         int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth, View.MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView1);
            //给item的measure设置参数是listView的宽度就可以获取到真正每一个item的高度
            listItem.measure(widthSpec, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        
        params.height = totalHeight
                + (listView1.getDividerHeight() * (listAdapter.getCount() + 1));
        listView1.setLayoutParams(params);
    }

实例:

 /**
     * 初始化列表
     */
    private void initListView() {
        List<CommentInfo> list = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            CommentInfo info;
            if(i%2==0){
                info =  new CommentInfo("idddx" + i, "2020-09-10 18:00:00", "快递师傅奇偶的实际发生了的会计法反倒是咖", 3.6f);
            }else{
                info =  new CommentInfo("idddx" + i, "2020-09-10 18:00:00", "快递师傅奇偶的实际发生了的会计法反倒是咖啡机收到啦反馈撒酒疯发送卡打了飞机撒飞机撒来得快dsfsdafsdaf倒是咖啡机收到啦反馈撒酒疯发送卡打了飞机撒飞机撒来得", 4.3f);
            }
            list.add(info);
        }
        m_listAdapter = new CommentListAdapter(this, list);
        m_listView.setAdapter(m_listAdapter);
        //setListViewHeight(m_listView, m_listAdapter);  //item高度测量不准确,如果每个item高度一样,可以使用该方法
        setListViewHeightBasedOnChildren(m_listView);    //item高度测量准确,当每个item高度不一样时,使用该方法
    }

    /**
     * 根据列表项的个数,设定listView的高度
     */
    protected void setListViewHeight(ListView listView, CommentListAdapter adapter) {
        try {
            int totalHeight = 0;
            for(int i = 0; i < adapter.getCount(); i++) {
                View viewItem = adapter.getView(i, null, listView);
                viewItem.measure(0, 0);
                totalHeight += viewItem.getMeasuredHeight();
            }
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listView.getCount() - 1));
            listView.setLayoutParams(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据列表项的个数,设定listView的高度
     */
    public void setListViewHeightBasedOnChildren(ListView listView1) {
        BaseAdapter listAdapter = (BaseAdapter) listView1.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        //获取listView的宽度
        ViewGroup.LayoutParams params = listView1.getLayoutParams();
        int listViewWidth = this.getWindowManager().getDefaultDisplay().getWidth();
        int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth, View.MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView1);
            //给item的measure设置参数是listView的宽度就可以获取到真正每一个item的高度
            listItem.measure(widthSpec, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        params.height = totalHeight
                + (listView1.getDividerHeight() * (listAdapter.getCount() + 1));
        listView1.setLayoutParams(params);
    }

完!!!

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页