How to add multiple headers throughout a single ListView with addHeaderView()?

前端 未结 4 793
花落未央
花落未央 2020-12-25 08:55

Can Android\'s addHeaderView() be used to add multiple headers throughout a single ListView? Can someone give an example of how to do this?

I was able to accomplish

相关标签:
4条回答
  • 2020-12-25 09:25

    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.

    0 讨论(0)
  • 2020-12-25 09:38

    You can also use this class as an example. It supports "sections" and "actions", where "sections" are just groups of "actions" (items), they have non-clickable and non-selectable item - header. "Actions" can specify onClick handler which will be called when this "action" will be pressed, if exist. It also contains ImageLoader to fetch images for the "actions" from the web.

    0 讨论(0)
  • 2020-12-25 09:43

    What you want Android developers might call a list separator or subheading ("headers" and "footers" live only at the top or bottom of the list). The gist of how you can do this is by using a ListAdapter that wraps other ListAdapters and is smart enough to return a header view type for certain rows and keep track of the offsets, or wraps those separator views in their own mini adapters.

    Take a look at Mark Murphy's SectionedListAdapter, GPL, which takes the first approach (based on code by Jeff Sharkey), or his MergeAdapter, and see this SO question.

    It's a far cry from the graceful handling of smart list subheadings on the iPhone, but it is fairly straightforward to use MergeAdapter and very flexible once you wrap your head around exactly what's going on inside the adapters.

    0 讨论(0)
  • 2020-12-25 09:45

    If you want to return different layouts (e.g. items & headers) you have to use getItemViewType(int position). So your adapter code should look like this:

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ICONIC = 1;
    
    @Override
    public int getViewTypeCount() {
        return 2;   // we have two types, so just return 2
    }
    
    @Override
    public int getItemViewType(int position) {
        // TODO: return TYPE_HEADER here if this position is a header, otherwise return TYPE_ICONIC
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO: return the appropriate layout
        // hint: you might call getItemViewType(position) yourself here to know of which type the layout is
    }
    

    That's basically how it works. To ensure the headers are not selectable, you might want to throw ArrayAdapter away and use ListAdapter instead, overriding isEnabled(int position) to return false for your header views. ;-)

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