Android: Listview inside ScrollView

后端 未结 4 1945
抹茶落季
抹茶落季 2021-01-23 17:35

I want to have a layout that can scroll and a listview inside it. The listview will expand it\'s height base on how many items in it. Only the ScrollView outside is scrollable.

4条回答
  •  自闭症患者
    2021-01-23 18:37

    I used below lines of code to scroll list item inside scroll view. It's working fine for my requirement, i hope it will help you.

    Thanks, Murali.M

    public static void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter(); 
            if (listAdapter == null) {
                //pre-condition
                return;
            }
    
            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
    
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
            listView.requestLayout();
        }
    

提交回复
热议问题