Android ListView Size Get getAbsListViewHeight(AbsListView absListView, int lineNumber, int verticalSpace)

Here you can find the source of getAbsListViewHeight(AbsListView absListView, int lineNumber, int verticalSpace)

Description

get Abs List View Height

License

Apache License

Declaration

public static int getAbsListViewHeight(AbsListView absListView,
        int lineNumber, int verticalSpace) 

Method Source Code

//package com.java2s;
/****************************************************************************
 *            Copyright (C) 2014 www.apkdv.com
 *    Author: LengYue  ProjectName: DvTools
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.//from   ww  w. j  a  va2 s.  co  m
 *****************************************************************************/

import android.view.View;
import android.view.View.MeasureSpec;

import android.widget.*;

public class Main {

    public static int getAbsListViewHeight(AbsListView absListView,
            int lineNumber, int verticalSpace) {
        int totalHeight = 0;
        int w = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        int h = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

        absListView.measure(w, h);

        ListAdapter mListAdapter = absListView.getAdapter();

        if (mListAdapter == null) {
            return totalHeight;
        }

        int count = mListAdapter.getCount();

        if (absListView instanceof ListView) {
            for (int i = 0; i < count; i++) {
                View listItem = mListAdapter.getView(i, null, absListView);

                listItem.measure(w, h);
                totalHeight += listItem.getMeasuredHeight();
            }

            if (count == 0) {
                totalHeight = verticalSpace;
            } else {
                totalHeight = totalHeight
                        + (((ListView) absListView).getDividerHeight() * (count - 1));
            }
        } else if (absListView instanceof GridView) {
            int remain = count % lineNumber;

            if (remain > 0) {
                remain = 1;
            }

            if (mListAdapter.getCount() == 0) {
                totalHeight = verticalSpace;
            } else {
                View listItem = mListAdapter.getView(0, null, absListView);

                listItem.measure(w, h);

                int line = count / lineNumber + remain;

                totalHeight = line * listItem.getMeasuredHeight()
                        + (line - 1) * verticalSpace;
            }
        }

        return totalHeight;
    }
}

Related

  1. getListViewSize(ListView myListView)
  2. getAbsListViewHeight(AbsListView absListView, int lineNumber, int verticalSpace)
  3. getListViewHeight(AbsListView absListView, int lineNumber, int verticalSpace)
  4. getListViewSize(ListView myListView)