Remove header from listView

前端 未结 7 2066
执念已碎
执念已碎 2021-02-01 04:58

I\'m having some problems when trying to remove the header from a listView. At first I use addHeaderView() to add it, but when I change to another layo

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-01 05:30

    Most people don't like to use AddHeaderView, however I sometimes found it very convenient, to avoid modifying complex adapters, or if the headers are not very related to them.

    With this easy trick you will be able to seamlessly remove/add headers:

    I add an empty LinearLayout with orientation vertical, and height wrap_content, as the only Header View (let mListView be the target listView):

     LinearLayout mCustomHeaders=new LinearLayout(context);
     mCustomHeaders.setOrientation(LinearLayout.VERTICAL);
    
     mListView.addHeaderView(mCustomHeaders);
     mListView.setAdapter (.......)
    

    Thenafter, I can add random stuff, anywhere, to the list as header, even when the list is full:

     mCustomHeaders.add(myHeaderView1);
     mCustomHeaders.add(myHeaderView2);
     mCustomHeaders.add(myHeaderView3);
    

    You can also delete all headers, anytime:

     mCustomHeaders.removeAllViews(); // will erase all headers
    

    You get the idea .... Hope it helps !

提交回复
热议问题