Original [Android]listview's Headerview

Source: Internet
Author: User

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 voidonItemClick(AdapterView<?> parent, View view, int position, longid)

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(intresource, ViewGroup root, booleanattachToRoot)

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

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.