Add headview and androidheadview to android listview
Click Event After headview is added to listview 1. view in the header: sets the click Time of the view to block the Click Event of the listview item.
<span style="white-space:pre"></span>View headerView = getLayoutInflater().inflate(R.layout.listhead, null);Button btn = (Button) headerView.findViewById(R.id.button1);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Toast.makeText(MainActivity.this, "bn", Toast.LENGTH_SHORT).show();}});
2. Add the header view: It must be placed before the setAdapter. Otherwise, an error will be reported.
lv.addHeaderView(headerView);
3. Fill in false data with listview
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();HashMap<String, String> map = new HashMap<String, String>();map.put("tv", "tv");list.add(map);list.add(map);list.add(map);list.add(map);list.add(map);list.add(map);
<span style="white-space:pre"></span>SimpleAdapter sa = new SimpleAdapter(this, list, R.layout.item_lv,<span style="white-space:pre"></span>new String[] { "tv" }, new int[] { R.id.tv });
4. Set the adapter: If the listview does not set the adapter, The headview will not be displayed.
<span style="white-space:pre"></span>lv.setAdapter(sa);
5. Set the item Click Event
Lv. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Arg0, View arg1, int position, long id) {// position if the first of headview position = 0 is headviewToast. makeText (MainActivity. this, "position:" + position, Toast. LENGTH_SHORT ). show (); // id: If headview or footview exists, the IDs of these two views are-1if (id =-1) {Toast. makeText (MainActivity. this, "head:" + id, Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (MainActivity. this, "id:" + id, Toast. LENGTH_SHORT ). show ();}}});
All main. java code:
Import java. util. arrayList; import java. util. hashMap; import java. util. list; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. button; import android. widget. listView; import android. widget. simpleAdapter; Import android. widget. toast; public class MainActivity extends Activity {ListView lv; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); lv = (ListView) findViewById (R. id. listView1); // header view: sets the click Time of the view to block the click event View headerView = getLayoutInflater () of the listview item (). inflate (R. layout. listhead, null); Button btn = (Button) HeaderView. findViewById (R. id. button1); btn. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {Toast. makeText (MainActivity. this, "bn", Toast. LENGTH_SHORT ). show () ;}}); // Add the header view: It must be placed in front of the adapter. Otherwise, an error is returned. addHeaderView (headerView); // false data List <HashMap <String, String> list = new ArrayList <HashMap <String, String> (); HashMap <String, string> map = new HashMap <String, String> (); Map. put ("TV", "TV"); list. add (map); list. add (map); list. add (map); list. add (map); list. add (map); list. add (map); SimpleAdapter sa = new SimpleAdapter (this, list, R. layout. item_lv, new String [] {"TV"}, new int [] {R. id. TV}); // set the adapter: If the listview does not set the adapter, The headview will not display the lv. setAdapter (sa); // set the item Click Event: lv. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Arg0, View arg1, int position, long id) {// position if the first of headview position = 0 is headviewToast. makeText (MainActivity. this, "position:" + position, Toast. LENGTH_SHORT ). show (); // id: If headview or footview exists, the IDs of these two views are-1if (id =-1) {Toast. makeText (MainActivity. this, "head:" + id, Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (MainActivity. this, "id:" + id, Toast. LENGTH_SHORT ). show () ;}}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
Recommended articles: Click to open the link