Example usage for android.widget ListView getDividerHeight

List of usage examples for android.widget ListView getDividerHeight

Introduction

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

Prototype

public int getDividerHeight() 

Source Link

Usage

From source file:com.moonpi.tapunlock.MainActivity.java

public static void updateListViewHeight(ListView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();

    if (myListAdapter == null)
        return;//w w w .j  a  va 2  s .  c om

    // Get listView height
    int totalHeight = myListView.getPaddingTop() + myListView.getPaddingBottom();
    int adapterCount = myListAdapter.getCount();

    for (int i = 0; i < adapterCount; i++) {
        View listItem = myListAdapter.getView(i, null, myListView);

        if (listItem instanceof ViewGroup) {
            listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
        }

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

    // Change height of listView
    ViewGroup.LayoutParams paramsList = myListView.getLayoutParams();
    paramsList.height = totalHeight + (myListView.getDividerHeight() * (adapterCount - 1));
    myListView.setLayoutParams(paramsList);
}

From source file:com.lgallardo.qbittorrentclient.TorrentDetailsFragment.java

/**
 * *//from  ww  w .  j a va 2 s.  c  o m
 * Method for Setting the Height of the ListView dynamically. Hack to fix
 * the issue of not showing all the items of the ListView when placed inside
 * a ScrollView
 * **
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;

    for (int i = 0; i < listAdapter.getCount(); i++) {

        long numOfLines = 1;
        view = listAdapter.getView(i, view, listView);

        if (i == 0) {
            view.setLayoutParams(new LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
        }

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);

        TextView file = (TextView) view.findViewById(R.id.file);
        TextView percentage = (TextView) view.findViewById(R.id.percentage);
        ProgressBar progressBar1 = (ProgressBar) view.findViewById(R.id.progressBar1);

        if (view.getMeasuredWidth() > desiredWidth) {

            double viewWidthLong = Double.valueOf(view.getMeasuredWidth());
            double desiredWidthLong = Double.valueOf(desiredWidth);

            numOfLines = Math.round(viewWidthLong / desiredWidthLong) + 1;

            totalHeight += (file.getMeasuredHeight() * numOfLines) + percentage.getMeasuredHeight()
                    + progressBar1.getMeasuredHeight();

        } else {
            totalHeight += view.getMeasuredHeight();
        }

    }

    LayoutParams params = listView.getLayoutParams();

    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

    listView.setLayoutParams(params);
    listView.requestLayout();

}

From source file:com.lgallardo.youtorrentcontroller.TorrentDetailsFragment.java

/**
 * *//from   ww  w. j  a v a2s.co  m
 * Method for Setting the Height of the ListView dynamically. Hack to fix
 * the issue of not showing all the items of the ListView when placed inside
 * a ScrollView
 * **
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;

    for (int i = 0; i < listAdapter.getCount(); i++) {

        long numOfLines = 1;
        view = listAdapter.getView(i, view, listView);

        if (i == 0) {
            view.setLayoutParams(new LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
        }

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);

        TextView file = (TextView) view.findViewById(R.id.file);
        TextView info = (TextView) view.findViewById(R.id.info);

        if (view.getMeasuredWidth() > desiredWidth) {

            double viewWidthLong = Double.valueOf(view.getMeasuredWidth());
            double desiredWidthLong = Double.valueOf(desiredWidth);

            numOfLines = Math.round(viewWidthLong / desiredWidthLong) + 1;

            totalHeight += file.getMeasuredHeight() * numOfLines + info.getMeasuredHeight();

        } else {
            totalHeight += view.getMeasuredHeight();
        }

    }

    LayoutParams params = listView.getLayoutParams();

    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

    listView.setLayoutParams(params);
    listView.requestLayout();

}

From source file:com.wewow.MainActivity.java

public void fixListViewHeight(ListView listView) {
    // ??ListView?
    ListAdapter listAdapter = listView.getAdapter();
    int totalHeight = 0;
    if (listAdapter == null) {
        return;//from   w  w w  .  j a v a2s  .com
    }
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
        View listViewItem = listAdapter.getView(i, null, listView);
        // ?View 
        listViewItem.measure(0, 0);
        // ?
        totalHeight += listViewItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    // listView.getDividerHeight()??
    // params.heightListView?
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    viewPager.setFocusable(true);
    viewPager.setFocusableInTouchMode(true);
    viewPager.requestFocus();
}

From source file:ua.mkh.settings.full.MainActivity.java

public static void setListViewHeightBasedOnChildren(ListView lv) {
    ListAdapter listAdapter = lv.getAdapter();
    if (listAdapter == null)
        return;/*w  ww  .j av  a2s. c  om*/

    int desiredWidth = MeasureSpec.makeMeasureSpec(lv.getWidth(), MeasureSpec.UNSPECIFIED);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, lv);
        if (i == 0)
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));

        view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = lv.getLayoutParams();
    params.height = totalHeight + (lv.getDividerHeight() * (listAdapter.getCount() - 1));
    lv.setLayoutParams(params);
    lv.requestLayout();
}

From source file:com.amagi82.kerbalspaceapp.MissionPlanner.java

/**
 * This method animates all other views in the ListView container (not including ignoreView) into their final positions. It is called
 * after ignoreView has been removed from the adapter, but before layout has been run. The approach here is to figure out where
 * everything is now, then allow layout to run, then figure out where everything is after layout, and then to run animations between all
 * of those start/end positions./*ww w . ja va2  s  .co  m*/
 */
private void animateRemoval(final ListView listview, View viewToRemove) {
    int firstVisiblePosition = listview.getFirstVisiblePosition();
    for (int i = 0; i < listview.getChildCount(); ++i) {
        View child = listview.getChildAt(i);
        if (child != viewToRemove) {
            int position = firstVisiblePosition + i;
            long itemId = mAdapter.getItemId(position);
            mItemIdTopMap.put(itemId, child.getTop());
        }
    }
    // Delete the item from the adapter
    int position = mListView.getPositionForView(viewToRemove);
    mAdapter.remove(mAdapter.getItem(position));
    mAdapter.notifyDataSetChanged();
    refreshDeltaV();

    final ViewTreeObserver observer = listview.getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            observer.removeOnPreDrawListener(this);
            boolean firstAnimation = true;
            int firstVisiblePosition = listview.getFirstVisiblePosition();
            for (int i = 0; i < listview.getChildCount(); ++i) {
                final View child = listview.getChildAt(i);
                int position = firstVisiblePosition + i;
                long itemId = mAdapter.getItemId(position);
                Integer startTop = mItemIdTopMap.get(itemId);
                int top = child.getTop();
                if (startTop != null) {
                    if (startTop != top) {
                        int delta = startTop - top;
                        child.setTranslationY(delta);
                        child.animate().setDuration(MOVE_DURATION).translationY(0);
                        if (firstAnimation) {
                            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                                child.animate().setListener(new AnimatorListenerAdapter() {
                                    @Override
                                    public void onAnimationEnd(Animator animation) {
                                        mBackgroundContainer.hideBackground();
                                        mSwiping = false;
                                        mListView.setEnabled(true);

                                    }
                                });
                            } else {
                                child.animate().withEndAction(new Runnable() {
                                    @Override
                                    public void run() {
                                        mBackgroundContainer.hideBackground();
                                        mSwiping = false;
                                        mListView.setEnabled(true);
                                    }
                                });
                                firstAnimation = false;
                            }
                        }
                    }
                } else {
                    // Animate new views along with the others. The catch is that they did not
                    // exist in the start state, so we must calculate their starting position
                    // based on neighboring views.
                    int childHeight = child.getHeight() + listview.getDividerHeight();
                    startTop = top + (i > 0 ? childHeight : -childHeight);
                    int delta = startTop - top;
                    child.setTranslationY(delta);
                    child.animate().setDuration(MOVE_DURATION).translationY(0);
                    if (firstAnimation) {
                        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                            child.animate().setListener(new AnimatorListenerAdapter() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    mBackgroundContainer.hideBackground();
                                    mSwiping = false;
                                    mListView.setEnabled(true);

                                }
                            });
                        } else {
                            child.animate().withEndAction(new Runnable() {
                                @Override
                                public void run() {
                                    mBackgroundContainer.hideBackground();
                                    mSwiping = false;
                                    mListView.setEnabled(true);
                                }
                            });
                            firstAnimation = false;
                        }
                    }
                }
            }
            mItemIdTopMap.clear();
            return true;
        }
    });

}