Android NestedScrollView showing only one Item in ListView

前端 未结 1 756
梦如初夏
梦如初夏 2021-01-22 19:02

Anyone knows what\'s wrong in my layout? I\'m not able to figure out if why is my ListView is showing only one item.

Layout.xml



        
相关标签:
1条回答
  • 2021-01-22 19:40

    As mentioned here,

    You can call this method after listView.setAdapter(dataAdapter);

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null)
            return;
    
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(
                listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
    
        View view = null;
    
        for (int i = 0; i < listAdapter.getCount(); i++) {
            view = listAdapter.getView(i, view, listView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(
                        desiredWidth, LayoutParams.WRAP_CONTENT));
    
            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
    
    0 讨论(0)
提交回复
热议问题