Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

android - How to make the header or footer of a ListView not clickable

I'm adding a footer and header view to a ListView by using the methods setHeaderView() and setFooterView() and a ViewInflater. That works quite well.

But how could I prevent the header or footer view from firing onListItemClick events? Of course I can catch the event and check whether it came from a header or footer, but this only solves one part of the problem, as header and footer got still focused when clicked.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Simply use the ListView#addHeaderView(View v, Object data, boolean isSelectable); and matching addFooter() method.


The purpose of Object data parameter.

The ListView source code describes the data parameter as:

The data backing the view. This is returned from ListAdapter#getItem(int).

Which means if I use listView.getAdapter().getItem(0); it will return the data Object from our header.


I'll elaborate this with an example:

listView = (ListView) findViewById(R.id.list);
String[] array = new String[] {"one", "two", "three"};
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);

Next let's add a header and set the adapter:

listView.addHeaderView(view, "Potato", false);
listView.setAdapter(adapter);

Later if we ask:

Log.v("ListAdapter", listView.getAdapter().getItem(0));  // output: "Potato" 
Log.v("ArrayAdapter", adapter.getItem(0));               // output: "one"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...