How can I add blank space to the end of a ListView?

后端 未结 5 1968
一生所求
一生所求 2021-01-04 05:26

I have a number of elements in a ListView that scroll off the screen.

I would like there to be blank space at the end of the View. That is, the user should be able

5条回答
  •  礼貌的吻别
    2021-01-04 05:38

    1. Inflate any layout of your choice (this could be an XML of and ImageView with no drawable and with set height and width of your choice)
    2. Measure the screen height and create new LayoutParams and set the height of it to 1/2 of the screen height
    3. Set the new layout params on your inflated view
    4. Use the ListView's addFooterView() method to add that view to the bottom of your list (there is also an addHeaderView())

    Code to measure screen height

     WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
     Display display = wm.getDefaultDisplay();
     int screenHeight = display.getHeight();
    

    Code to set half screen height:

     View layout = inflater.inflate(R.layout.mylistviewfooter, container, false);
     ViewGroup.LayoutParams lp = layout.getLayoutParams();
     lp.height = screenHeight/2;
     layout.setLayoutParams(lp);
     myListView.addFooterView(layout);
    

    An Aside: When you add a footer or header view to any listview, it has to be done before adding the adapter. Also, if you need to get your adapter class after doing this you will need to know calling the listview's adapter by getAdapter() will return an instance of HeaderViewListAdapter in which you will need to call its getWrappedAdapter method Something like this :

     MyAdapterClassInstance myAdapter = (MyAdapterClassInstance) ((HeaderViewListAdapter) myListView.getAdapter()).getWrappedAdapter();
    

提交回复
热议问题