2

I am currently developing an Android app based on an iOS version, and I have to implement the following layout for the main menu:

a top hub with a few buttons, and then a ViewPager under it. This ViewPager has a bar over it (between itself and the hub) to display pages' titles. Inside the ViewPager is a ListView to display articles.

The expected scrolling behaviour is the following: When you scroll down from the very top, it should scroll normally and hide the hub, and then when you have scrolled enough to where the title bar reaches the top it should snap the title bar in place and you should still be able to scroll the ListView.

I have no idea how to add the hub to all of this and have the desired behaviour. Everything is already implemented and functions properly without the hub. But I have no idea how to add it and make it work.

I tried putting everything in a ScrollView, but this does not seem to work. The scrolling still works on the ListView of articles but it does not scroll the hub out of the way.

According to most people, you should never have a scrolling element inside another one. But I don't see any other way to do this.

Fatalize
  • 3,513
  • 15
  • 25

2 Answers2

1

I had multiple listviews inside a scrollview and to make it work I set the height of every list view using the following method. Things used to work fine.

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);
    listView.requestLayout();
}
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
0

I've implemented this component what works as a ListView, so you don't need to adapt your external code and it solves the problem with ListView inside Scrollview together with other elements...I named it StretchedListView, No external hacks required. It is in an answer to another post: https://stackoverflow.com/a/21878703

Enjoy!

Community
  • 1
  • 1
Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74