Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Cannot get a clickable ListView header/footer

I'm trying to set a header and footer in my list view that are clickable buttons. The problem is that the OnClickListener isn't responding to anything and I can't figure out what I'm doing wrong.

$   View header = getLayoutInflater().inflate(R.layout.header_layout, null, true);
    getListView().addHeaderView(header);

    myAdapter = new myAdapter(this);

    header.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Do what I want when i click it
        }
    });

Update
The best solution I ultimately came up with was adding a separate button to the header layout, and then doing it like this:

View header = getLayoutInflater().inflate(R.layout.header_layout, null);
Button headerButton = (Button)header.findViewById(R.id.header_button);
getListView().addHeaderView(header);

headerButton.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
     // My Click Stuff
     }
});
like image 690
bbedward Avatar asked Jan 11 '12 21:01

bbedward


4 Answers

You need to handle the click in the ListView's onItemClick. Simply check if that's the first or last item in the adapter and handle the clicks that way. You need to treat it as an item in the ListView.

like image 152
LuxuryMode Avatar answered Oct 16 '22 06:10

LuxuryMode


I see a few issues:

  • when inflating the header, use getListView() as the second parameter (root, where you have null now)l
  • should the header be a View or a ViewGroup? I've ended up using ViewGroup in these situations.
  • finally -- perhaps you should be setting the click listener on the button in the header instead of the header itself?
like image 4
elijah Avatar answered Oct 16 '22 07:10

elijah


There is a way more easier solution:

Just set a "OnClickListener" to the applied View:

View view = inflater.inflate(R.layout.xxx, null);
view.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        //do something
    }
});

Very easy thing which solved it!

like image 3
FireDevil Avatar answered Oct 16 '22 06:10

FireDevil


Example with a footer:

mYourListView.addFooterView(footer, null, true);

Then in the OnItemClickListener you can check:

@Override
public void onItemClick(AdapterView<?> parent,
                            View view, final int position, final long id) {

    if (id != -1) {
        // do whatever you do with list items
    } else {
        // do what you need after the footer been clicked
    }

(If you need to handle and the header and the footer click, check position - 0 for the header and [adapter.getCount() - 1] for the footer)

This approach will provide the same visual effect while footer click as if the list item been clicked. (But if you do not need that effect just add OnClickListener to the footer and it will intercept all footer clicks)

like image 2
Andrew Avatar answered Oct 16 '22 05:10

Andrew