Android listview not scrolling

前端 未结 7 1405
离开以前
离开以前 2020-12-21 15:21

i have a layout whose starting tag(parent tag is ) is



        
7条回答
  •  一个人的身影
    2020-12-21 16:03

    Try below class, it may help you to work with ListView inside ScrollView

    Utility.java

    import android.view.View;
    import android.view.ViewGroup;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    
    public class Utility {
            public static void setListViewHeightBasedOnChildren(ListView listView) {
                  ListAdapter listAdapter = listView.getAdapter();
                if (listAdapter == null) {
                // pre-condition
                      return;
                }
    
                int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
                for (int i = 0; i < listAdapter.getCount(); i++) {
                     View listItem = listAdapter.getView(i, null, listView);
                     if (listItem instanceof ViewGroup) {
                        listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                     }
                     listItem.measure(0, 0);
                     totalHeight += listItem.getMeasuredHeight();
                }
    
                LayoutParams params = listView.getLayoutParams();
                params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
                          listView.setLayoutParams(params);
            }
         }
    

    Finally use this line just after setting adapter to your listview - Utility.setListViewHeightBasedOnChildren(your_listview);

提交回复
热议问题