Example usage for android.widget ListAdapter getView

List of usage examples for android.widget ListAdapter getView

Introduction

In this page you can find the example usage for android.widget ListAdapter getView.

Prototype

View getView(int position, View convertView, ViewGroup parent);

Source Link

Document

Get a View that displays the data at the specified position in the data set.

Usage

From source file:Main.java

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);//from  w  w w  .  j a  va2 s .  c  o m
    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;

}

From source file:Main.java

public static void setDynamicHeight(ListView mListView) {
    ListAdapter mListAdapter = mListView.getAdapter();
    if (mListAdapter == null) {
        // when adapter is null
        return;//from   ww  w.ja v a  2 s.c o m
    }
    int height = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(mListView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    for (int i = 0; i < mListAdapter.getCount(); i++) {
        View listItem = mListAdapter.getView(i, null, mListView);
        listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        height += listItem.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = mListView.getLayoutParams();
    params.height = height + (mListView.getDividerHeight() * (mListAdapter.getCount() - 1));
    mListView.setLayoutParams(params);
    mListView.requestLayout();
}

From source file:Main.java

public static void setAbsListViewHeight(AbsListView absListView, int lineNumber, int verticalSpace) {

    if (lineNumber == 0) {
        return;//  ww  w  . j  a  v a 2  s .  co  m
    }
    int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    ListAdapter mListAdapter = absListView.getAdapter();
    if (mListAdapter == null) {
        return;
    }
    int totalHeight = 0;
    int count = mListAdapter.getCount();
    ViewGroup.LayoutParams params = absListView.getLayoutParams();
    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) {
            params.height = verticalSpace;
        } else {
            params.height = totalHeight + (((ListView) absListView).getDividerHeight() * (count - 1));
        }

    } else if (absListView instanceof GridView) {
        int remain = count % lineNumber;
        if (remain > 0) {
            remain = 1;
        }
        if (mListAdapter.getCount() == 0) {
            params.height = verticalSpace;
        } else {
            View listItem = mListAdapter.getView(0, null, absListView);
            listItem.measure(w, h);
            int line = count / lineNumber + remain;
            params.height = line * listItem.getMeasuredHeight() + (line + 1) * verticalSpace;
        }

    }

    ((MarginLayoutParams) params).setMargins(0, 0, 0, 0);
    absListView.setLayoutParams(params);

}

From source file:Main.java

public static void setListViewSize(ListView list) {
    ListAdapter myListAdapter = list.getAdapter();
    if (myListAdapter == null) {
        //do nothing return null
        return;//  ww w .j a va 2  s.  co  m
    }

    //set listAdapter in loop for getting final size
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(list.getWidth(), View.MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < myListAdapter.getCount(); i++) {
        view = myListAdapter.getView(i, view, list);
        if (i == 0) {
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }

    //setting listview item in adapter
    ViewGroup.LayoutParams params = list.getLayoutParams();
    params.height = totalHeight + (list.getDividerHeight() * (myListAdapter.getCount() - 1));
    list.setLayoutParams(params);
}

From source file:com.aniruddhc.acemusic.player.Drawers.NavigationDrawerFragment.java

/**
 * Clips ListViews to fit within the drawer's boundaries.
 *//*ww  w . j  a va 2 s. co  m*/
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

From source file:com.google.cast.samples.games.gamedebugger.DebuggerFragment.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;//w ww  .jav a  2  s.c  o m
    }

    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

From source file:com.bobomee.android.common.util.ScreenUtil.java

/**
 * http://stackoverflow.com/questions/12742343/android-get-screenshot-of-all-listview-items
 *//*  w w  w .  j  a v a  2s.  co m*/
public static Bitmap shotListView(ListView listview) {

    ListAdapter adapter = listview.getAdapter();
    int itemscount = adapter.getCount();
    int allitemsheight = 0;
    List<Bitmap> bmps = new ArrayList<Bitmap>();

    for (int i = 0; i < itemscount; i++) {

        View childView = adapter.getView(i, null, listview);
        childView.measure(View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
        childView.setDrawingCacheEnabled(true);
        childView.buildDrawingCache();
        bmps.add(childView.getDrawingCache());
        allitemsheight += childView.getMeasuredHeight();
    }

    Bitmap bigbitmap = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight,
            Bitmap.Config.ARGB_8888);
    Canvas bigcanvas = new Canvas(bigbitmap);

    Paint paint = new Paint();
    int iHeight = 0;

    for (int i = 0; i < bmps.size(); i++) {
        Bitmap bmp = bmps.get(i);
        bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
        iHeight += bmp.getHeight();

        bmp.recycle();
        bmp = null;
    }

    return bigbitmap;
}

From source file:Main.java

public static void getListViewSize(GridView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();
    if (myListAdapter == null) {
        //do nothing return null
        return;/*from   ww w .  ja va 2s. com*/
    }
    //set listAdapter in loop for getting final size
    int totalHeight = 0;
    int listval = myListAdapter.getCount();
    if (listval % 3 == 0) {
        listval = myListAdapter.getCount() / 3;
    } else {
        listval = myListAdapter.getCount() / 3 + 1;
    }
    for (int size = 0; size < listval; size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    System.out.println("Height  " + totalHeight + "    " + myListAdapter.getCount());
    //setting listview item in adapter
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight + ((myListView.getVerticalSpacing() / 3) * (myListAdapter.getCount() - 1));
    myListView.setLayoutParams(params);
    // print height of adapter on log
    Log.i("height of listItem:", String.valueOf(totalHeight));
}

From source file:Main.java

public static void setGridViewSize(GridView grid) {
    ListAdapter myListAdapter = grid.getAdapter();
    if (myListAdapter == null) {
        //do nothing return null
        return;// ww  w .  jav a  2 s .  c  o m
    }

    //set listAdapter in loop for getting final size
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(grid.getWidth() / grid.getNumColumns(),
            View.MeasureSpec.AT_MOST);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < ((myListAdapter.getCount() / grid.getNumColumns())
            + myListAdapter.getCount() % grid.getNumColumns()); i++) {
        view = myListAdapter.getView(i, view, grid);
        if (i == 0) {
            view.setLayoutParams(new GridView.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        view.measure(desiredWidth, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        totalHeight += view.getMeasuredHeight();
    }

    //setting listview item in adapter
    ViewGroup.LayoutParams params = grid.getLayoutParams();
    params.height = totalHeight
            + (grid.getHorizontalSpacing() * (((myListAdapter.getCount() - 1) / grid.getNumColumns())
                    + (myListAdapter.getCount() % grid.getNumColumns())));
    grid.setLayoutParams(params);
}

From source file:com.optimusinfo.elasticpath.cortex.common.Utils.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;/*  w  ww .j  a  va2  s.co m*/
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}