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.

The adapter that passed 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.
And the Onitemclick method of 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.
This means that when we use a click event, the position in the ListView column is arg2-1

Generally in development, in order to achieve the effect we want to dynamically add the main practice is to add the required control before Listview.setadapter, and then use the Removefooterview () method to remove the control.
It is important to note that the dynamic operation of each ListView will have to be Removefooterview () method 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, item if there is a button and other controls, when listening to the Onitemclick event of the ListView, the focus will be the button, ImageButton and other controls within the item are snatched,
This causes the Onitemclick event to not be triggered after the ListView has been set. The workaround is to block the focus fetching of controls such as their internal button when the item is initialized, and the
concrete method can be called in the root control of the custom item:

 setdescendantfocusability (viewgroup.focus_block_descendants);  
This will block the word control from grabbing the focus, The Onitemclick of the ListView can be triggered correctly, and there is no effect on the buttons and other controls within the item, and they can trigger their own click events when clicked.
 
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 parameter to false 
   
3, then add the header above, the Addheaderview method that is called when the header is added must precede the 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 a header,
if it has been added, it will generate a new headerviewlistadapter. This new headerviewlistadapter contains all the contents 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 that 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.