0

I am using ListView inside ScrollView and as we all know It creates problem.

I got good solution from this website : How to calculate total row heights of listView in android?

But still for some items, It does not show Listview properly. Is there any improved solution ?

My Code :

     public static void getListViewSize(ListView myListView, Context context) {
        ListAdapter myListAdapter = myListView.getAdapter();

        if (myListAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {

            View listItem = myListAdapter.getView(size, null, myListView);
            if (listItem instanceof ViewGroup)
                listItem.setLayoutParams(new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT));

            WindowManager wm = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            @SuppressWarnings("deprecation")
            int screenWidth = display.getWidth();
            int listViewWidth = screenWidth - 20;
            int widthSpec = MeasureSpec.makeMeasureSpec(listViewWidth,
                    MeasureSpec.AT_MOST);
            listItem.measure(widthSpec, 0);

            totalHeight += listItem.getMeasuredHeight();

            Log.e("height of listItem:", String.valueOf(totalHeight));
        }

        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        params.height = totalHeight
                + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
        myListView.requestLayout();
    }
Jeeten Parmar
  • 5,568
  • 15
  • 62
  • 111
  • refer to this http://stackoverflow.com/questions/9386669/listview-in-scroll-view-my-scrollview-moves-to-top-of-listview-how-do-i-prevent/21803960#21803960 – Ahmed Zayed Mar 13 '14 at 07:32
  • @AhmedHafez, I have already tried it and my solution is better than that one. – Jeeten Parmar Mar 13 '14 at 07:39

1 Answers1

0
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();

The core of the function is these three lines, it try to measure each view. The 0 in listItem.measure(0, 0) is equals to MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)

Mostly it will calculate the accurate height of the listview. There is one exception, when the view content is too much and will wrap line, i.e. there are to many lines of text. In such situation, you should specified a accurate widthSpec to measure(). So change listItem.measure(0, 0) to

// try to give a estimated width of listview
int listViewWidth = screenWidth - leftPadding - rightPadding; 
int widthSpec = MeasureSpec.makeMeasureSpec(listViewWidth, MeasureSpec.AT_MOST);
listItem.measure(listViewWidth, 0)

UPDATE about the formula here

int listViewWidth = screenWidth - leftPadding - rightPadding; 
enter code here

It's just an example to show how you can estimate the width of the width of listview, the formula is based on the fact that width of listview ≈ width of screen. The padding is set by your self, maybe 0 here (Get screen dimensions in pixels). This page tells how to get screen width. In general, it's just a sample, and you can write your own formula here.

Community
  • 1
  • 1
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49