ListView in ScrollView potential workaround

前端 未结 2 1435
暖寄归人
暖寄归人 2020-12-01 13:45

I\'ve done all of the research on the matter. I know that Google thinks it\'s pointless and that the developers, know that it\'s not. I also know that there is no known work

2条回答
  •  鱼传尺愫
    2020-12-01 14:23

    Ok, as far as I got your needs I think you may just use the ListView.addFooterView(View v) method:

    http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)

    It will allow you to have all your list items + "a few buttons" footer to be scrolled as a single block.

    So the code should be smth like that:

    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.widget.ArrayAdapter;
    import android.widget.LinearLayout;
    
    public class YourActivity extends ListActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            LayoutInflater factory = getLayoutInflater();
            LinearLayout footer = 
                (LinearLayout) factory.inflate(R.layout.your_a_few_buttons_footer, null);
    
            getListView().addFooterView(footer);
    
            String[] array = new String[50];
            for (int i = 0; i < 50;) { array[i] = "LoremIpsum " + (++i); }
    
            setListAdapter(
                new ArrayAdapter(this, R.layout.list_item, array)
            );
        }   
    }
    

    Note, the doc says addFooterView() should be called BEFORE the setListAdapter().

    UPDATE: to add a View at the top of the list use ListView.addHeaderView(View v). Note that, for instance, LinearLayout is also a View. So you can put anything you want as a header or a footer and it'll be scrolled with the list as an indivisible block.

提交回复
热议问题