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 fetching of controls such as their internal buttons while initializing item, and the method can be called in the root control of the custom item:
[Java] View plaincopy
- 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 parameter to false;
3, then add the header above said, add header when the Addheaderview method called must be placed in front of listview.setadapter, meaning very clear 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, and if it has been added, it will generate a new tempadapter, the new Tempadapter 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 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.
4, then the above Tempadapter 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.
[Java] View plaincopy
- @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:
[Java] View plaincopy
- public void Onitemclick (Adapterview
Both Headview and Footerview can respond to the Onitemclick method, Headview position is 0, Footerview is the largest position.
However, you can set Onclicklistener for Headview and Footerview to cover Onitemclick, so that You click Headview or Footerview will trigger Onclicklistener instead of Onitemclick ().
Considerations for Android ListView Addheaderview and Addfooterview