Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a non scrollable ListView?

Tags:

android

I should want a non scrollable ListView, and show the entire ListView. It's because my entire screen is a ScrollView, and I dispatch widgets with a RelativeLayout, so I don't need the ListView scroll.

I set my ui with code, not with xml.

I've used listView.setScrollContainer(false), but it's not work, I don't understand why.

Thanks.

like image 410
Istao Avatar asked Dec 02 '10 17:12

Istao


People also ask

How do you make a list view non scrollable?

I found a very simple solution for this. Just get the adapter of the listview and calculate its size when all items are shown. The advantage is that this solution also works inside a ScrollView. Please note to call this function after you have set the adapter to the listview.

How do I make my screen not scroll in flutter?

For this case, you can wrap your children widget inside Align and set alignment property. Show activity on this post. You can also Wrap the parent Column in a Container and then wrap the Container with the SingleChildscrollView widget. (This solved my issue).

How do I make ListView scroll smoothly?

The key to a smoothly scrolling ListView is to keep the application's main thread (the UI thread) free from heavy processing. Ensure you do any disk access, network access, or SQL access in a separate thread. To test the status of your app, you can enable StrictMode .


1 Answers

I found a very simple solution for this. Just get the adapter of the listview and calculate its size when all items are shown. The advantage is that this solution also works inside a ScrollView.

Example:

public void justifyListViewHeightBasedOnChildren (ListView listView) {

    ListAdapter adapter = listView.getAdapter();

    if (adapter == null) {
        return;
    }
    ViewGroup vg = listView;
    int totalHeight = 0;
    for (int i = 0; i < adapter.getCount(); i++) {
        View listItem = adapter.getView(i, null, vg);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

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

Call this function passing over your ListView object:

justifyListViewHeightBasedOnChildren(myListview);

The function shown above is a modification of a post in: Disable scrolling in listview

Please note to call this function after you have set the adapter to the listview. If the size of entries in the adapter has changed, you need to call this function as well.

like image 183
Chris623 Avatar answered Sep 18 '22 17:09

Chris623