Android list view inside a scroll view

后端 未结 30 2088
一向
一向 2020-11-21 13:43

I have an android layout which has a scrollView with a number of elements with in it. At the bottom of the scrollView I have a listView

30条回答
  •  醉酒成梦
    2020-11-21 13:46

    I had a similar problem to the issue posed by the Original Poster - how to make the listview scroll inside the scrollview - and this answer solved my problem. Disable scrolling of a ListView contained within a ScrollView

    I didn't call new fragments into existing layouts or anything like that, like the OP was doing, so my code would look something like this :

    
    
     
    
    
        
    
       
       
    
    
    
    
    

    Basically what I am doing is checking the length of the listview before I call it and when I call it I make it into that length. In your java class use this function:

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

    And call the function like this:

    justifyListViewHeightBasedOnChildren(listView);
    

    The result is a listview with no scrollbar, the whole length of the listview being displayed, that scrolls with the scroll bar of the scrollview.

提交回复
热议问题