kevingoos
kevingoos

Reputation: 4303

Android listview not scrollable

I have a profile page contains a linearlayout with some textviews and imageviews and a listview. (See picture)

enter image description here

The problem is that I want to make the whole page scrollable like in the twitter app instead of only the listview. So the listview need to extend to max height.

How can i force the listview to extend to max size and not be scrollable.

I linearlayout would be a second option but then it need to be possible to add a custom arrayadapter with a custom row like in the listview.

Upvotes: 1

Views: 5880

Answers (3)

Muhammad Aamir Ali
Muhammad Aamir Ali

Reputation: 21087

Yes you can put the scroll view as parent view and make your custom class for your list view. You can check my answer by this link

list view in scroll view

Upvotes: 1

Rethinavel
Rethinavel

Reputation: 3952

You can make this in two ways:

#1 Adding Header to your Listview

  View header = inflater.inflate(R.layout.your_header_layout, null);
  yourlistview.addHeader(header);

#2 You can set the list view height based on children.Call this fxn with your listview and keep all your widgets inside scrollview.

    public static void setListViewHeightBasedOnChildren(ListView listView) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }

    int totalHeight = 0;
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0); 
        totalHeight += listItem.getMeasuredHeight();
    }

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

Upvotes: 9

Keya
Keya

Reputation: 849

Use ScrollView to wrap your layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

   ......
</LinearLayout
</ScrollView>

Keep in mind that ScrollView takes in only one child. So adjust your original layout accordingly.

Upvotes: 0

Related Questions