Using ListView : How to add a header view?

前端 未结 3 2032
无人及你
无人及你 2020-11-27 03:54

I looke at the ListView API and I saw the method:

addHeaderView(View v)

What I want to do is to have a layout above the list,

相关标签:
3条回答
  • 2020-11-27 04:44

    You can add as many headers as you like by calling addHeaderView() multiple times. You have to do it before setting the adapter to the list view.

    And yes you can add header something like this way:

    LayoutInflater inflater = getLayoutInflater();
    ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, myListView, false);
    myListView.addHeaderView(header, null, false);
    
    0 讨论(0)
  • 2020-11-27 04:52

    I found out that inflating the header view as:

    inflater.inflate(R.layout.listheader, container, false);
    

    being container the Fragment's ViewGroup, inflates the headerview with a LayoutParam that extends from FragmentLayout but ListView expect it to be a AbsListView.LayoutParams instead.

    So, my problem was solved solved by inflating the header view passing the list as container:

    ListView list = fragmentview.findViewById(R.id.listview);
    View headerView = inflater.inflate(R.layout.listheader, list, false);
    

    then

    list.addHeaderView(headerView, null, false);
    

    Kinda late answer but I hope this can help someone

    0 讨论(0)
  • 2020-11-27 04:53

    You simply can't use View as a Header of ListView.

    Because the view which is being passed in has to be inflated.

    Look at my answer at Android ListView addHeaderView() nullPointerException for predefined Views for more info.

    EDIT:

    Look at this tutorial Android ListView and ListActivity - Tutorial .

    EDIT 2: This link is broken Android ListActivity with a header or footer

    0 讨论(0)
提交回复
热议问题