Catalogue [-]
- (1) Dimension layout is ignored after adding headerview.
- (2) Position shift of onitemclicklistener caused by adding headerview
- (3) Layoutinflater's Infalte ()
(1) Dimension layout is ignored after adding headerview. The usual way to add headers is to
?
123 |
LayoutInflater lif = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View headerView = lif.inflate(R.layout.header, null ); mListView.addHeaderView(headerView); |
Reason:
Lif.inflate (R.layout.header, null) loses the layoutparam of the root view in the XML layout and should use the
?
1 |
lif.inflate(R.layout.header, mListView, false ); |
(2) Position shift of onitemclicklistener caused by adding headerview Onitemclicklistener interface method:
?
1 |
void onItemClick(AdapterView<?> parent, View view, int position, long id) |
Position usually starts at 0, but after adding headerview, position also calculates the number of Headerview.
Several solutions:
1. Manually calculate the true position position:
?
12345678 |
final headerCount =
1
;
mListView.setOnItemClickListener(
new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position,
long id) {
Item item = myAdapter.getItem(position - headerCount);
}
});
|
2, in fact the above steps of the ListView has been provided for us, so can be rewritten as:
?
1234567 |
mListView.setOnItemClickListener(
new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position,
long id) {
Item item = parent.getAdapter().getItem(position);
}
});
|
Reason in the source code has a relatively clear explanation:
When a headerview is added, the adapter that is actually passed to the ListView is wrapped, Parent.getadapter () returns the adapter (Headerviewlistadapter) that the actual ListView uses, The headerviewlistadapter getitem (int) method deals with the position problem.
(3) Layoutinflater's Infalte () to echo the first question. Layoutinflater's role is simply to "translate" the XML layout file into the corresponding view object, and for performance reasons, Layoutinflater can only process the compiled XML file, not the normally plaintext encoded XML file.
One of the most common methods:
?
1 |
View inflate( int resource, ViewGroup root, boolean attachToRoot) |
which
Resource is the layout file ID
Root is the parent ViewGroup object,
Attachtoroot is whether to add the "translated" view to the root above
Root and Attachtoroot work together:
1, with Root and attachtoroot to False, then inflate () returns the view that "translation" gets
2, with Root and attachtoroot true, then inflate () is the "translated" view added to root, and then back to root
3, no root, and Attachtoroot is false, then inflate () returns the "translation" of the resulting view.
4, no root, while Attachtoroot is true, error.
Also, Root has an important role to add the appropriate layoutparam to the view that the "translation" gets, and if you do not want to add the resulting view to root, it is not required to pass the root, such as:
?
123 |
view View = Mlayoutinflater.inflate (R.layout.header, new listview (mcontext), false ); view View = Mlayoutinflater.inflate (R.layout.header, linearlayout (mcontext), false view View = Mlayoutinflater.inflate (R.layout.header, relativelayout (mcontext), false Code class= "Java Plain"); |
The view above gets, The contents are identical except for the view Layoutparam, respectively, Abslistview.layoutparams,linearlayout.layoutparams,relativelayout.layoutparams.
Original [Android]listview's Headerview