1

I want to use a kind of ListView inside of a ScrollView. The list has a variable length, it is always possible to add/remove items. Is there an easy way to modify the default Android ListView, oder do I have to create a custom ListView?

Veaceslav Gaidarji
  • 4,213
  • 3
  • 33
  • 60
JensJensen
  • 957
  • 1
  • 10
  • 25
  • add a listview in a scrollview is bad practice.What you want to achieve ? – Biraj Zalavadia Nov 21 '13 at 09:03
  • create views in between scrollview by using a forloop – Invader Nov 21 '13 at 09:03
  • `ListView` is possible to add/remove items, and it can scroll by itself. So why do you want to have a new class that does the same thing? – Lawrence Choy Nov 21 '13 at 09:03
  • Like others pointed out, this is very bad practice. Check out this answer by the creator of the listview widget and the discussion in the comments for more info: http://stackoverflow.com/a/3496042/362298 – Ricardo Nov 21 '13 at 09:26
  • I have a quite big vertical layout, inside which I want to have, among others, a list of `TextView`s. Of course I can put a lot of single `TextView` inside my layout, but for me that seems not to be very practical too. – JensJensen Nov 21 '13 at 09:38

2 Answers2

6

xml file:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="19dp"
        android:text="@string/hello_world" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_launcher" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

</ScrollView>

Inside activity:

public class MainActivity extends Activity {
   private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE",
                 "SIX", "SEVEN", "EIGHT", "NINE", "TEN" };
   ListView myList;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          myList = (ListView) findViewById(R.id.listView);
          myList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listview_array));
          Helper.getListViewSize(myList);

   }
}

Helper class:

public class Helper {
    public static void getListViewSize(ListView myListView) {
        ListAdapter myListAdapter = myListView.getAdapter();
        if (myListAdapter == null) {
            //do nothing return null
            return;
        }
        //set listAdapter in loop for getting final size
        int totalHeight = 0;
        for (int size = 0; size < myListAdapter.getCount(); size++) {
            View listItem = myListAdapter.getView(size, null, myListView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
      //setting listview item in adapter
        ViewGroup.LayoutParams params = myListView.getLayoutParams();
        params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
        myListView.setLayoutParams(params);
        // print height of adapter on log
        Log.i("height of listItem:", String.valueOf(totalHeight));
    }
}
Pankaj Arora
  • 11,041
  • 2
  • 38
  • 62
  • Nice one but a little slow. I think you also you add paddings: params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1)) + listview.getPaddingBottom() + listview.getPaddingTop(); – ApollonDigital Dec 07 '14 at 23:00
0

The ListView should expand to fill as much vertical area as it needs when contained in a ScrollView so you shouldn't have a problem.

TechDragon
  • 139
  • 1
  • 7