Android: Cannot get clickable ListView header / footer

I am trying to set the header and footer in my list, which are buttons that can be clicked. The problem is that OnClickListener is not responding to anything, and I cannot understand 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 that I eventually came up with is to add a separate button to the header layout, and then do 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
     }
});
+5
source share
7 answers

I see several problems:

  • when inflating the header, use getListView () as the second parameter (root, where you now have null) l
  • View ViewGroup? ViewGroup.
  • - , ?
+3

ListView onItemClick. , adapter . ListView.

+8

:

"OnClickListener" View:

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

, !

+3

:

mYourListView.addFooterView(footer, null, true);

OnItemClickListener :

@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
    }

( , , - 0 [adapter.getCount() - 1] )

, , . ( , OnClickListener , )

+2

, , . onTouchListener .

. , , -:

mylistView.addFooterView(footerView, null, false);

false , . , . , .

0

:

, , , subview

myHeaderView.findViewById(R.id.myButton).setOnClickListener(new OnClickListener() { ... } );

, listView, , true

mListView.addHeaderView(myHeaderView, null, true);
0

@LuxuryMode, ListView onItemClick. :

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

// if statement

}

:

if (position == 0) { )

:

if (position == adapter.getCount()) { ) // if there is no header
if (position == adapter.getCount() + 1) { ) // if there is a header

. , :

arrayList.size() // number of items in the array (if your adapter is using an array)
cursor.getCount() // number of data items in the cursor (if your adapter is using a cursor)
adapter.getCount() // number of data items passed in by the cursor (or at least that is what a custom adapter should report)
listview.getCount() // number of data items from the adapter + a header and/or footer view

So using a header or footer will make listview.getCount () more by 1 than cursor.getCount (). If you use both the header and footer, it will be 2 more.

0
source

All Articles