Add headview to android listview

Source: Internet
Author: User

Add headview 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.

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
 
  > list = new ArrayList
  
   >();HashMap
   
     map = new HashMap
    
     ();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 });

4. Set the adapter: If the listview does not set the adapter, The headview will not be displayed.
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
 
  
> List = new ArrayList
  
   
> (); HashMap
   
    
Map = new HashMap
    
     
(); 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 ;}}
    
   
  
 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.