-1

I understand that putting a ListView inside a ScrollView is not recommmender. But I must use a ListView, simply because I'm using an adapter and various other methods.

I have used this code, which I found on StackOverflow and it shows me the majority of my list. However, in portrait mode some items are cut off from the bottom and in landscape mode the same.

How can this be rectified? To clarify, I've got a ScrollView containing a LinearLayout containing some elements and my ListView in my XML. In addition, if I go to a new activity, then return I can briefly see the scroll bars for listview show up and disappear. How can this be rectified too?

public static boolean setListViewHeightBasedOnItems(ListView listView) {

        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter != null) {

            int numberOfItems = listAdapter.getCount();

            // Get total height of all items.
            int totalItemsHeight = 0;
            for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
                View item = listAdapter.getView(itemPos, null, listView);
                item.measure(0, 0);
                totalItemsHeight += item.getMeasuredHeight();
            }

            // Get total height of all item dividers.
            int totalDividersHeight = listView.getDividerHeight() *
                    (numberOfItems - 1);

            // Set list height.
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalItemsHeight + totalDividersHeight;
            listView.setLayoutParams(params);
            listView.requestLayout();

            return true;

        } else {
            return false;
        }

    }
aqibjr1
  • 651
  • 5
  • 21

2 Answers2

0

Use recycler-view or list-view and make use of view-type to inflate different layouts instead of using scroolview and nesting up other viewgroups inside.

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
  • That seems overly-complex for my relatively simple activity. I have some radio buttons and titles at the top. With a search fiter and listview after. – aqibjr1 Aug 01 '16 at 17:45
  • No it will simplify your problem and break it into several cohesive and sub-optimal components (layouts). – Nayan Srivastava Aug 01 '16 at 17:49
0

I think your question is a bit misleading. How on earth can a listview displays its all items without scrolling as long as we are dealing with these tiny mobile screens?

Maybe you want that scroll bar to not show up, then you can set attribute like this on your ScrollView:

android:scrollbars="none"
Myo Ko
  • 108
  • 8