0

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 which is then populated by an adapter.

The problem that I am experiencing, is that android is excluding the listView from the scrollView as the scrollView already has a scroll-able function. I want the listView to be as long as the content is and for the master scroll view to be scroll-able.

i want to set the scrollView scroling if the listView has complete her scroling

@Gaurav Roy

  • https://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view This could help you as he has the same problem like yours. – Lakshya Gupta Jul 21 '17 at 18:35
  • You can use ListView and RecyclerView with multiple views instead of these hacks. – Rahul Jul 21 '17 at 18:36
  • Please refer to https://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view – Gaurav Roy Jul 21 '17 at 18:40
  • Possible duplicate of [Android list view inside a scroll view](https://stackoverflow.com/questions/18367522/android-list-view-inside-a-scroll-view) – Gaurav Roy Jul 21 '17 at 18:42

2 Answers2

0

Instead of embedding a ListView inside of a ScrollView, add the contents of the scrollview to a single viewgroup, then add those programatically to the top of the ListView with addHeaderView.

For instance, have the root element of your layout be a ListView, and inflate a separate layout with the rest of the views via a LayoutInflater. Then just pass that reference to the inflated view into the addHeaderView method.

John Leehey
  • 22,052
  • 8
  • 61
  • 88
0

You do not have to do anything special in layout.xml file nor handle anything on the parent ScrollView. You only have to handle the child ListView. You can also use this code to use any type of child view inside a ScrollView & perform Touch operations.

Just add these lines of code in your java class :

ListView lv = (ListView) findViewById(R.id.layout_lv);
lv.setOnTouchListener(new OnTouchListener() {
    // Setting on Touch Listener for handling the touch inside ScrollView
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    // Disallow the touch request for parent scroll on touch of child view
    v.getParent().requestDisallowInterceptTouchEvent(true);
    return false;
    }
});

If you put ListView inside a ScrollView then all the ListView does not stretch to its full height. Below is a method to fix this issue.

/**** Method for Setting the Height of the ListView dynamically.
 **** Hack to fix the issue of not showing all the items of the ListView
 **** when placed inside a ScrollView  ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), 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, MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

To use this method just pass the ListView inside this method :

ListView list = (ListView) view.findViewById(R.id.ls);
setListViewHeightBasedOnChildren(list);
For using with ExpandableListView - credit Benny

ExpandableListView: view = listAdapter.getView(0, view, listView);
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT, View.MeasureSpec.EXACTLY);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY);
view.measure(widthMeasureSpec, heightMeasureSpec);

For ListView with variable items height use the below link :

ListView inside ScrollView is not scrolling on Android

For Library to directly implementing in your code - credit Paolo Rotolo

https://github.com/PaoloRotolo/ExpandableHeightListView