Disable scrolling of a ListView contained within a ScrollView
Asked Answered
A

7

36

I want to show a Profile screen for my users.

It must have three views (2 Buttons and a ImageView) and a ListView to show the content made by that user.

However, I don't want the ListView to scroll. Instead, I want it to be as big as needed, and to put all my views inside a ScrollView, so the three first views scroll out with the ListView. This, of course, does not work as intended.

All my three items are inside a LinearLayout. I thought of making them the first item in the ListView, but this leads to them being selectable as the first item, and having to do some unneeded coding.

Is there a way to do this the easy way or will I have to stick with making the Layout the first item in my ListView?

Antonio answered 31/8, 2012 at 9:48 Comment(6)
check this: #4338685Belisle
Yeah, but if I use LinearLayout instead of ListView I don't get a OnItemSelected event, which leads to more unneeded coding.Antonio
hi check the link and read it carefully. use listview in LinearLayout. set ScrollContainer false and You can bind an OnClickListener or OnTouchListener to a view which will fire when it is clicked or touchedBelisle
possible duplicate of Disable scrolling in listviewFluor
@santirivera92 again, if you don't want to use the scrolling provided by ListView, then don't use ListView. Just use a ScrollView with a LinearLayout inside. ListView adds a lot of complexity to your app backend and it is worth it only when you have huge, dynamic lists.Precis
I know it's a bit late. Best solution for the problem is this.Genie
S
9

Adding them to the ListView as first Item seems like a pretty good solution.

To make the View unselectable just get the view and .setClickable(false).

Stiegler answered 31/8, 2012 at 10:20 Comment(0)
K
53

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 static 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 modidication 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.

Kudu answered 7/1, 2015 at 11:39 Comment(1)
Given method works but after doing so my scroll gets to the bottom most. The whole page gets scrolled down. Any solution ?Francenefrances
B
13

You can do this by

listView.setScrollContainer(false);

for more please check

How to get a non scrollable ListView?

Belisle answered 31/8, 2012 at 10:2 Comment(3)
This does not work and the link you provided contradicts your answer.Romalda
@MattW I gave this reference cause of this answer "Don't put a listview in a scrollview, it doesn't work. If you want a list of items that doesn't scroll, it's called a linearlayout" . the above code will not work actually list view has problem with scrollview layout.Belisle
@SaifuddinSarker but what if I have dynamic number of items that I want to show in list that's not scrollable?Fulgurite
S
9

Adding them to the ListView as first Item seems like a pretty good solution.

To make the View unselectable just get the view and .setClickable(false).

Stiegler answered 31/8, 2012 at 10:20 Comment(0)
A
5

I would add a View with invisible background on top of ListView. Set a View.OnTouchListener() to it. And consume the event by returning true in onTouch() method of View.OnTouchListener().

When you want the list to be scrolling back again, remove the touch listener set on the transparent View

Atwekk answered 18/9, 2013 at 12:54 Comment(0)
F
4

if you have to show limited number of items in list view and want to stop list view from scrolling then you must keep listview height greater then the items total height.

for example you want to show 3 items. (height of row is 30). then items total height becomes 3 x 30dp = 90dp,

so now you have to set listview height greater then 90. e.g: 100dp. so now your listview will not scroll in any case.

Fives answered 7/10, 2013 at 11:30 Comment(0)
C
2

I think the best way would be to put the 2 buttons and the image view in a LinearLayout (or any layout that suits your need) and add this layout as the list header using the addHeaderView method:

http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)

Carbonari answered 31/8, 2012 at 23:37 Comment(0)
F
1

with the following instruction:

name_lista.getLayoutParams (). height = new_size

new_size is a variable that you will calculate according to the number of elements of your list, for example:

new_size = 100 * list_size;
Forego answered 9/5, 2018 at 18:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.