Showing posts with label listview. Show all posts
Showing posts with label listview. Show all posts

Monday, April 7, 2014

Android: ListView inside a ScrollView

We shouldn’t use ListView inside the ScrollView because the list view is already ScrollView, the items in list view are already scrollable..

Problem / Issue

When we try to add the ListView into the ScrollView, we may face a problem that the list view will show only one item from the adapter.

To solve this issue, we should know the definition of ScrollView.

As per the Android Documentaion:

A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.
You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.

Solution

Calculate the size of the list items and set the height of the ListView programmatically.
Call setListViewHeightBasedOnChildren(listview)  method right after the listView.setAdapter.

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(00);
            totalHeight += listItem.getMeasuredHeight();
        }

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


Thanks for reading :) 
Whether this post is helpful?

Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? Share it in the comments.

Tuesday, June 26, 2012

Android : ListView with fast scroll using alphabets in the right side of the screen


Android ListView with fast scroll using alphabets in the right side of the screen

We have lot of questions like
Alphabetical sorted listview with Alphabetic section using Adapter in Android application.
Listview alphabetical index programmatically
In IPhone we are having Indexed UITableView
Android Listview like iphone contacts list (alphabets in right side of the list)

We are not having similar functionality in android but we are having android:fastScrollEnabled and alphaIndexer.
Without using android:fastScrollEnabled and alphaIndexer. Just with touch recognition, alphabetically sorting can be achieved easily.

If you want to implement the same feature in android and solution for all the above questions, we can try this approach

Code: indexTableCode

Screen Shot:





















Thanks for reading :)
If you have any other quick thoughts/hints that you think people will find useful, feel free to leave a comment.