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?

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90
  • check this: http://stackoverflow.com/questions/4338185/how-to-get-a-non-scrollable-listview – Saifuddin Sarker Aug 31 '12 at 09:59
  • Yeah, but if I use LinearLayout instead of ListView I don't get a OnItemSelected event, which leads to more unneeded coding. – Charlie-Blake Aug 31 '12 at 10:01
  • 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 touched – Saifuddin Sarker Aug 31 '12 at 10:04
  • 1
    possible duplicate of [Disable scrolling in listview](http://stackoverflow.com/questions/7611085/disable-scrolling-in-listview) – Lalit Poptani Aug 31 '12 at 10:05
  • @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. – Radu Simionescu Sep 26 '14 at 11:36
  • I know it's a bit late. Best solution for the problem is [this](https://stackoverflow.com/questions/18813296/non-scrollable-listview-inside-scrollview). – AndroidNewbie Nov 09 '17 at 03:04

7 Answers7

52

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.

Community
  • 1
  • 1
Chris623
  • 2,464
  • 1
  • 22
  • 30
  • 1
    Given method works but after doing so my scroll gets to the bottom most. The whole page gets scrolled down. Any solution ? – Hammad Nov 18 '16 at 19:18
13

You can do this by

listView.setScrollContainer(false);

for more please check

How to get a non scrollable ListView?

Community
  • 1
  • 1
Saifuddin Sarker
  • 853
  • 3
  • 8
  • 26
  • 9
    This does not work and the link you provided contradicts your answer. – Matt W Nov 12 '14 at 19:23
  • 1
    @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. – Saifuddin Sarker Jan 17 '15 at 13:32
  • @SaifuddinSarker but what if I have dynamic number of items that I want to show in list that's not scrollable? – Micer Jul 20 '17 at 08:49
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).

Yalla T.
  • 3,707
  • 3
  • 23
  • 36
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

Sudarshan Bhat
  • 3,772
  • 2
  • 26
  • 53
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.

Usman Nisar
  • 3,031
  • 33
  • 41
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)

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;
fredy
  • 11
  • 1