Search code examples
androidlistviewdynamicscrollandroid-scrollview

ListView inside ScrollView, bad idea? Than how to substitute a dynamically populated list inside a ScrollView?


I am having this issue, where I need to output a "list" dynamically, in one of my activities where a ScrollView is the root element.

I have found a lot of posts where people show workarounds and hacks, but most of them say it is a bad habit placing a listview inside a scrollview. I understand, but none of them show which is the right way? I can think of creating textviews dynamically and adding them to the layout but I am not sure if it would be the best way.

So, how to get this done right? Thanks!


Solution

  • Unfortunately I did not get the answer I was hoping for. I kept on searching and I found a nice solution which in my situation suits quite well!

    I have created a simple LinearLayout and connected my ArrayAdapter to it, while deleting the ListView I was using before. It is not recommended for long lists, but mine is less then 10 items long.

    ...
    
            //Creating a custom ArrayAdapter
            ArrayAdapter adapt2 = new DirectionsListAdapter(this, myList);      
    
            //Pointing to the LinearLayout
            LinearLayout testContainer = (LinearLayout) findViewById(R.id.testContainer);
            final int adapterCount = adapt2.getCount();
    
            //adding each adapter item to the LinearLayout
            for (int i = 0; i < adapterCount; i++) {
                  View item = adapt2.getView(i, null, null);
                  testContainer.addView(item);
                }
    
    ...
    

    So far it works really well, I have all the customisation options I had before, it is fast and clean and the best about it is that it adjust its height automatically even inside the root ScrollView.