Example usage for android.widget ListView getAdapter

List of usage examples for android.widget ListView getAdapter

Introduction

In this page you can find the example usage for android.widget ListView getAdapter.

Prototype

@Override
public ListAdapter getAdapter() 

Source Link

Document

Returns the adapter currently in use in this ListView.

Usage

From source file:Main.java

/**
 * Set the ListView height based on the number of rows and their height
 * <p/>//from w w w. java2  s .  co m
 * Height for ListView needs to be set if used as a child of another ListView.
 * The child ListView will not display any scrollbars so the height needs to be
 * set so that all the rows are visible
 *
 * @param listView    the list view
 * @param extraHeight extra bottom padding
 */
@SuppressWarnings("SameParameterValue")
public static void setListViewHeightBasedOnChildren(ListView listView, int extraHeight) {
    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 = extraHeight + totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

From source file:Main.java

public static void getListViewSize(ListView listView, Context context) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null || listAdapter.getCount() < 2) {
        // pre-condition
        return;//w  w w . ja  v a  2s  .c om
    }

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int totalHeight = 0;
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(size.x, View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

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

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

From source file:Main.java

public static void setListViewHeightBasedOnChildren(ListView listView) {
    if (listView == null)
        return;//from w  ww .  ja v a  2  s .  com
    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);
}

From source file:Main.java

public static void overlay(ListView myListView, LinearLayout.LayoutParams paramsLinearLayout) {
    ListAdapter myListAdapter = myListView.getAdapter();
    if (myListAdapter == null) {
        //do nothing return null
        return;// w w w  .ja  v a 2  s. com
    }
    //set listAdapter in loop for getting final size
    int totalHeight = 0;
    for (int size = 0; size < myListAdapter.getCount(); size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    System.out.println("totalHeight :: " + totalHeight);
    //setting marginTop the next element
    if (totalHeight > 0) {
        int marginTop = (totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1))) * -1;
        paramsLinearLayout.setMargins(0, marginTop, 0, 0);
        System.out.println("marginTop :: " + marginTop);
    } else {
        System.out.println("nada :: totalHeight " + totalHeight);
        paramsLinearLayout.setMargins(0, 0, 0, 0);
    }
}

From source file:Main.java

public static void waitForItemCount(final ListView listView, final int expectedCount, final long timeout)
        throws InterruptedException {
    long start = System.currentTimeMillis();
    while (listView.getAdapter().getCount() != expectedCount) {
        Thread.sleep(50);// ww  w  .  ja v a2  s  .co m
        long now = System.currentTimeMillis();
        if (now - start > timeout) {
            throw new InterruptedException(
                    String.format("Timeout exceeded while waiting for an item count of %s", expectedCount));
        }
    }
}

From source file:com.laevatein.internal.ui.helper.AlbumListViewHelper.java

public static void setCursor(Fragment fragment, Cursor cursor) {
    ListView listView = (ListView) FragmentUtils.findViewById(fragment, R.id.l_list_album);
    CursorAdapter adapter = (CursorAdapter) listView.getAdapter();
    adapter.swapCursor(cursor);/*w  w w .j av a  2 s  . c o m*/
}

From source file:Main.java

/**
 * get ListView height according to every children
 * /*from w ww  . ja v a2 s . com*/
 * @param view
 * @return
 */
public static int getListViewHeightBasedOnChildren(ListView view) {
    int height = getAbsListViewHeightBasedOnChildren(view);
    ListAdapter adapter;
    int adapterCount;
    if (view != null && (adapter = view.getAdapter()) != null && (adapterCount = adapter.getCount()) > 0) {
        height += view.getDividerHeight() * (adapterCount - 1);
    }
    return height;
}

From source file:com.my.seams_carv.util.ScrollableViewUtil.java

/**
 * Return true if the given scrollable view is over scrolling in terms of
 * the given y momentum.//from   w ww. j ava 2 s.  com
 * <br/>
 * Usually it is called in the {@code dispatchTouchEvent}, {@code onTouchEvent},
 * or the {@code onInterceptTouchEvent}.
 *
 * @param view the scrollable view.
 * @param dy The y momentum.
 * @return true if the view is over scrolling in the vertical direction.
 */
public static boolean ifOverScrollingVertically(View view, float dy) {
    if (view == null || dy == 0)
        return false;

    // TODO: Test it.
    if (view instanceof ScrollView) {
        final ScrollView scrollView = (ScrollView) view;
        final View child = scrollView.getChildAt(0);
        if (scrollView.getHeight() >= child.getHeight())
            return true;

        if (dy > 0) {
            // Slide the thumb down to scroll up the list.
            return scrollView.getScrollY() == 0;
        } else {
            // Slide the thumb up to scroll down the list.
            MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();

            return child.getHeight() - scrollView.getScrollY() == scrollView.getHeight()
                    - (params.topMargin + params.bottomMargin);
        }
        //        } else if (view instanceof WebView) {
        //            WebView webView = (WebView) view;
        //
        //            // TODO: Complete it.
        //            if (dy > 0) {
        //                // Slide the thumb down to scroll up the list.
        //                return webView.computeHorizontalScrollOffset() == 0.f;
        //            } else {
        //                // Slide the thumb up to scroll down the list.
        //                return false;
        //            }
    } else if (view instanceof NestedScrollView) {
        final NestedScrollView scrollView = (NestedScrollView) view;
        final int scrollRange = scrollView.computeVerticalScrollRange()
                - scrollView.computeVerticalScrollExtent();

        if (scrollRange <= 0)
            return true;

        if (dy > 0) {
            // Slide the thumb down to scroll up the list.
            return scrollView.computeVerticalScrollOffset() == 0.f;
        } else {
            // Slide the thumb up to scroll down the list.
            return scrollView.computeVerticalScrollOffset() == scrollRange;
        }
    } else if (view instanceof ListView) {
        final ListView listView = (ListView) view;
        if (listView.getAdapter().getCount() == 0)
            return true;

        // TODO: Complete it.
        if (dy > 0) {
            // Slide the thumb down to scroll up the list.
            return listView.getScrollY() == 0;
        } else {
            // Slide the thumb up to scroll down the list.
            return false;
        }
    } else if (view instanceof RecyclerView) {
        final RecyclerView recyclerView = (RecyclerView) view;
        if (recyclerView.getAdapter().getItemCount() == 0)
            return true;

        final int scrollRange = recyclerView.computeVerticalScrollRange()
                - recyclerView.computeVerticalScrollExtent();

        if (dy > 0) {
            // Slide the thumb down to scroll up the list.
            return recyclerView.computeVerticalScrollOffset() == 0.f;
        } else {
            // Slide the thumb up to scroll down the list.
            return recyclerView.computeVerticalScrollOffset() == scrollRange;
        }
    }
    // TODO: Support more scrollable view.

    // Return true for the unsupported view because the dy is non-zero.
    return true;
}

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

/**
 * Clips ListViews to fit within the drawer's boundaries.
 *///from w  ww .  java 2  s.c  o  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.jefftharris.passwdsafe.lib.view.GuiUtils.java

/**
 * Set the height of a ListView based on all of its children
 *///w  ww . j  a  v  a2  s .  c o  m
public static void setListViewHeightBasedOnChildren(final ListView listView) {
    final ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    // Defer measurement so listview is rendered to get its width
    listView.post(new Runnable() {
        @Override
        public void run() {
            int width = View.MeasureSpec.makeMeasureSpec(listView.getMeasuredWidth(), View.MeasureSpec.AT_MOST);
            int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int totalHeight = 0;
            int numItems = listAdapter.getCount();
            for (int i = 0; i < numItems; i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(width, height);
                totalHeight += listItem.getMeasuredHeight();
            }

            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight;
            if (numItems > 0) {
                params.height += listView.getDividerHeight() * (numItems - 1);
            }
            listView.setLayoutParams(params);
        }
    });
}