Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Setting listview and pager together in a scrollview

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 283
    Comment on it

    Putting listview and viewpager or anyother view together in Scrollview is not good habit but sometimes it's necessary to do So in that case we can use that following method to set listview height at runtime so that we can scroll listview as well as other view together.

        public void setListViewHeightBasedOnChildren(ListView listView)
    {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null)
            return;
    
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
        int totalHeight=0;
        View view = null;
    
        for (int i = 0; i < listAdapter.getCount(); i++)
        {
            view = listAdapter.getView(i, view, listView);
    
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth,
                        ViewGroup.LayoutParams.MATCHPARENT));
    
            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
    
        }
    
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));
    
        listView.setLayoutParams(params);
        listView.requestLayout();
    
    }
    

    First get adapter of listview and then create a new measure specification that contain listview width and then get listview and other view combined height and then apply it to listview. Hope it will help you in scrolling.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: