Keywords: Android listview Header
Recently, I used the ophone SDK to develop an application. In the process of use, there is a problem. I personally think it is a bug of the underlying framework of ophone. However, there is no underlying support, and there is no way to do it, so you can only bypass it by yourself.
I. Use of listview Header
In the use of various controls, it is estimated that listview is the most commonly used one, And listview is used to display data in various markets. However, if you do not use listview and use liearlayout to add various components to it by yourself (I believe developers in the market for this implementation method are not doing well and will waste a lot of resources, use more memory ).
The following uses the app sink on the palm as an example:
It displays four recommended content above, and each item is used below to display each application.
Anyone who has used listview development knows that scrollview cannot be nested outside the listview, so it is very easy to display different headers and scroll with the listview scrolling. However, there are a lot of discussions on the Internet about how to implement them. I really don't know why.
There are two designs:
1. Fixed headers that do not scroll with listview.
Columns such as A, B, C, D, and E in an Excel file do not scroll with the content. The advantage is that it can always indicate the meaning of the content, and the disadvantage is that it will occupy the user's operation space.
2. The unfixed header does not scroll with the listview.
A column name, such as name, gender, and age, is invisible as the content rolls. Its benefits and disadvantages are exactly the opposite of fixed ones.
As shown in the figure above, the app collects data on the palm. Obviously, it cannot use a Fixed Header because the visible area of the device is small and the header occupies a large amount of space.
Two design implementations:
1. It is easy to implement a Fixed Header by placing the view outside the listview. For example:
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical">
<Include layout = "@ layout/fixed_header_view"/>
<Listview Android: Id = "@ ID/Android: List" Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<Textview Android: Id = "@ ID/Android: empty"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/empty_msg"/>
</Linearlayout>
2. The implementation of non-fixed headers is actually very simple.
In the application, inflate the header and call one of the following two methods of listview (For details, refer to api doc)
Public void addheaderview (view V );
Public void addheaderview (view V, object data, Boolean isselectable );
You can add a header for the listview, which will scroll with the listview. However, note that
2-1: addheaderview must be called before setadapter
2-2: Public void onitemclick (adapterview <?> In parent, view, int position, long ID), the header is calculated in position, that is, the data of the setadapter you give the listview is counted from headercount, so if you want to retrieve data from your adapter, You need to subtract headercount.
Example:
// Some code init listview and setheader
Listview = getlistview ();
View header = layoutinflater. From (this). Inflate (R. layout. scrolled_header_view, null );
// Do another init to set content for header and add event handler.
Listview. addheaderview (header );
Listview. setadapter (myadapter );
In the response to public void onitemclick (adapterview <?> Parent, view, int position, long ID)
Position-= listview. getheaderviewscount (); // You Need to subtract headercount
Myitem item = myadapter. getitem (position );
There is an introduction to header usage on the network. Most of them want to add a scrollview outside the listview, but this solution is not feasible. Therefore, you can use listview to add various views. These views must be created to occupy a large amount of valuable resources. In this way, the benefits of listview cannot be used and the code is not well maintained.
For example, if only one view is different from other projects, the header can be used. Because the status of listview is maintained by the system, it not only saves code, but also facilitates maintenance and saves memory usage. The next section describes how to implement multiple views in one listview.