Search code examples
androidandroid-viewandroid-adapterandroid-adapterview

View that can handle adapters but is not scrollable


Does a View that can handle adapters (e.g. ArrayAdapter) but is not scrollable exist?

I would not like to go into workarounds (e.g. disabling a ListView's scrolling feature).


Solution

  • Sure you can. Calling getView on an adapter returns a View that corresponds to the information provided from the adapter's getItem method. So you can do something like this:

    LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear_layout);
    BaseAdapter adapter = new CustomAdapter(context, itemList);
    
    for (int i = 0; i < itemList.size(); i++) {
        // you can pass in a recycled view instead of null
        View itemView = adapter.getView(i, null, layout);
        layout.addView(itemView);
    }
    

    ViewGroups such as LinearLayout aren't scrollable by themselves so this should work. I'm not sure why you would want to use an Adapter in this situation but that's up to you.