Android ListView Addheaderview and Addfooterview detailed

Source: Internet
Author: User

Addheaderview () Method: Mainly add layout to the head of the ListView
Addfooterview () Method: Basically add a layout to the bottom of the ListView

It is important to note that when adding a layout, you should add it from the parent container and not add the child controls in the parent container directly. For example: Add a button control from an XML layout file,
You can only add the entire layout XML file. Instead of just adding a button control alone.

Another overloaded method for adding head and bottom layouts is addheaderview (Headview, NULL, FALSE) and Addfooterview (Footerview, NULL, false) methods,
The difference between this method and the above method is that when you set a click event to the ListView, you can control whether the added layout can start hitting events. The difference is that the previous method can control whether the header can be
To be selected, if you do not want to be selected the third parameter is set to false;

The following is an example of the Addfooterview () method:
Addfooterview () method must be placed in front of Listview.setadapter, add header to the ListView must be added before binding adapter, otherwise it will be an error.
The reason is that when we call the Setadapter method, Android will determine if the current ListView has added a header, and if it has been added, it will generate a
New Tempadapter, this new tempadapter contains all of the adapter we've set and the header and footer of the ListView. So when I

When we add headers to the ListView, we return the tempadapter instead of the setadapter when we call Listview.getadapter in the program.

adapter in. If adapter is not set then Tempadapter is the same as our own adapter. The
  Listview.getadapter (). GetCount () method returns a value that is larger than we expected because the header was added.
  Our custom adapter inside the GetItem method inside the returned position is not included header, is our custom adapter data position number starting from 0,
  That means the list is the same as the one we passed in.
  Onitemclick method for the ListView in activity:
  public void Onitemclick (adapterview<?> arg0, View arg1, int Arg2,long arg3)
  ARG2 is the location of the current click, which is the position in the Tempadapter, starting with 0 if a header is added to the ListView, 0 represents the header.
  That is, when we use a click event, the position in the ListView column is arg2-1
  
  generally in development, To achieve the effect we want to add dynamically, the main practice is to add the required controls before Listview.setadapter and then remove the controls using the Removefooterview () method.
  It is important to note that each dynamic operation of a ListView will be Removefooterview () to remove the control. Otherwise, the ListView will automatically add blank lines, which can affect the effect of the display.
 ----------------------------------------------------------------------------------------------

Considerations for Android ListView Addheaderview and Addfooterview:

1, the item if there is a button and other controls, when listening to the ListView Onitemclick event, the focus will be in the item button, ImageButton and other controls snatched,
This causes the Onitemclick event to not be triggered after the ListView has been set. The workaround is to block the focus acquisition of controls such as internal buttons when the item is initialized.
The specific method can be called in the root control of the custom item:

Setdescendantfocusability (viewgroup.focus_block_descendants);
This can block the word control grab the focus, the ListView Onitemclick can be triggered correctly, and the item inside the button and other controls are not affected, they can be clicked to trigger their own click events.

2, when the ListView needs to add Headerview, you can call the ListView Addheaderview (Headview, NULL, False) method,
The method also has an overloaded method Addheaderview (Headview); the difference between the two methods is that the previous method can control whether the header can be selected, and if you do not want to be selected, set the third argument to False

3, then add the header above said, add header when the Addheaderview method called must be placed in front of Listview.setadapter,
It is very clear that if you want to add a header to the ListView, you must add it before you bind it to adapter, or you will get an error.
The reason is that when we call the Setadapter method, Android will determine if the current ListView has added headers,
If added, a new headerviewlistadapter is generated, and the new Headerviewlistadapter contains all of the adapter we have set and the header and footer of the ListView.
So when we call Listview.getadapter in the program after we add a header to the ListView, we return the tempadapter instead of the adapter we passed through Setadapter.
If adapter is not set then Headerviewlistadapter is the same as our own adapter. The Listview.getadapter (). GetCount () method returns a value that is larger than we expected because the header was added.

4, then the above Headerviewlistadapter said, we custom adapter inside the GetItem method inside the return position is not included header, is our custom adapter data position number starting from 0, That means the list is the same as the one we passed in.

@Override

Public View GetView (int position, View Convertview, ViewGroup parent) {

TODO auto-generated Method Stub

LOG.I ("adapter", "Position:" +position); This position is the real location of our data.

}

And in the Onitemclick method of the ListView:

public void onitemselected (adapterview<?> parent, view view, int position, long ID)

Position is the location of the current click, which is the position in the Headerviewlistadapter, starting with 0 if a header is added to the ListView, 0 represents the header. 4, then the above Headerviewlistadapter said, we custom adapter inside the GetItem method inside the return position is not included header, is our custom adapter data position number starting from 0, That means the list is the same as the one we passed in.


Public View GetView (int position, View Convertview, ViewGroup parent) {

TODO auto-generated Method Stub

LOG.I ("adapter", "Position:" +position); This position is the real location of our data.

}

And in the Onitemclick method of the ListView:

public void onitemselected (adapterview<?> parent, view view, int position, long ID)

Position is the location of the current click, which is the position in the Headerviewlistadapter, starting with 0 if a header is added to the ListView, 0 represents the header.

About adding and removing Footerview
* 3.1 always remove Footerview first time
* 3.2 If there is a need to add on Footerview



We usually load the data, in order to save traffic will not be all the time to complete the data, is generally a segmented download.
A multipart download typically puts a progress bar on the last side of the ListView to indicate that the data is loading, and when the data is loaded, we have to clear it again. It's time to pay attention.
Mloadinglayout = (framelayout) view.inflate (this, r.layout.load, null);
Listview.addfooterview (mloadinglayout);
Listview.requestfocus ();

This is a progress bar added to the tail of the ListView.
Listview.removefooterview (mloadinglayout);
This is the removal of the trailing progress bar.
Sometimes a null pointer is returned at the time of removal, but the ListView is not NULL, mloadinglayout is not NULL, but it is still a null pointer because the ListView is divided into three parts.
One is the head, the second is the middle part, the third is the tail. When you set the head or tail, you must have the middle part in order to really take effect. If you remove it without taking effect, you will be quoted a null pointer error.
So in
Listview.removefooterview (mloadinglayout);

You must first call Listview.setadapter (adapter); (Set center)

Adapter can have data of 0 but not null

Android ListView Addheaderview and Addfooterview detailed

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.