Recently, I was implementing a mail system. I wanted to show the following interface in listview to simulate the mail list interface that comes with Android.
According to the hierarchy Viewer analysis provided by Android, The listitem includes:
The leftmost view (red) distinguishes different mailboxes.
The checkbox imageview on the left .. I don't know why checkbox is not used.
Three textviews in the middle show the mail title content and date
The checkbox imageview star icon on the right is also implemented using imageview.
En · A complicated structure. Android provides several adapters for listview, but the structure is relatively simple and has fewer functions.
The above structure is improved by yourself. First, the XML file used to display listitem is constructed using XML.
View plaincopy to clipboardprint?
··· · 50 ······· · 90 ····· · 140 · 150
<? XML version = "1.0" encoding = "UTF-8"?>
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/widget37" Android: layout_width = "fill_parent"
Android: layout_height = "64dip">
<View Android: Id = "@ + ID/ListBox" Android: layout_width = "4dip"
Android: layout_height = "fill_parent" Android: Background = "# ff99ff00"/>
<Checkbox Android: Id = "@ + ID/listselected" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: layout_torightof = "@ ID/ListBox" Android: button = "@ layout/checkbox" Android: checked = "false" Android: layout_marginleft = "4dip" Android: layout_margintop = "1dip" Android: focusable = "false"/>
<Checkbox Android: Id = "@ + ID/liststared" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignparentright = "true" Android: button = "@ layout/Starbox" Android: checked = "false" Android: focusable = "false"/>
<Textview Android: Id = "@ + ID/listsubject" Android: layout_width = "wrap_content"
Android: layout_height = "36dip" Android: text = "title" Android: layout_alignparenttop = "true" Android: textcolor = "# ff888888"
Android: layout_toleftof = "@ ID/liststared" Android: textappearance = "? Android: ATTR/textappearancelarge"
Android: layout_torightof = "@ ID/listselected" Android: gravity = "bottom | left"/>
<Textview Android: Id = "@ + ID/listfrom" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: text = "mailfrom"
Android: layout_alignleft = "@ ID/listsubject"
Android: layout_below = "@ ID/listsubject" Android: textcolor = "# ff888888"
Android: textappearance = "? Android: ATTR/textappearancemedium"
/>
<Textview Android: Id = "@ + ID/listdate" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: text = "2010/2/0"
Android: layout_alignbaseline = "@ ID/listfrom" Android: layout_alignparentright = "true"
Android: layout_alignright = "@ ID/liststared" Android: textcolor = "# ff888888"
Android: textappearance = "? Android: ATTR/textappearancemedium "/>
</Relativelayout>
<? XML version = "1.0" encoding = "UTF-8"?>
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/widget37" Android: layout_width = "fill_parent"
Android: layout_height = "64dip">
<View Android: Id = "@ + ID/ListBox" Android: layout_width = "4dip"
Android: layout_height = "fill_parent" Android: Background = "# ff99ff00"/>
<Checkbox Android: Id = "@ + ID/listselected" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: layout_torightof = "@ ID/ListBox" Android: button = "@ layout/checkbox" Android: checked = "false" Android: layout_marginleft = "4dip" Android: layout_margintop = "1dip" Android: focusable = "false"/>
<Checkbox Android: Id = "@ + ID/liststared" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignparentright = "true" Android: button = "@ layout/Starbox" Android: checked = "false" Android: focusable = "false"/>
<Textview Android: Id = "@ + ID/listsubject" Android: layout_width = "wrap_content"
Android: layout_height = "36dip" Android: text = "title" Android: layout_alignparenttop = "true" Android: textcolor = "# ff888888"
Android: layout_toleftof = "@ ID/liststared" Android: textappearance = "? Android: ATTR/textappearancelarge"
Android: layout_torightof = "@ ID/listselected" Android: gravity = "bottom | left"/>
<Textview Android: Id = "@ + ID/listfrom" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: text = "mailfrom"
Android: layout_alignleft = "@ ID/listsubject"
Android: layout_below = "@ ID/listsubject" Android: textcolor = "# ff888888"
Android: textappearance = "? Android: ATTR/textappearancemedium"
/>
<Textview Android: Id = "@ + ID/listdate" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: text = "2010/2/0"
Android: layout_alignbaseline = "@ ID/listfrom" Android: layout_alignparentright = "true"
Android: layout_alignright = "@ ID/liststared" Android: textcolor = "# ff888888"
Android: textappearance = "? Android: ATTR/textappearancemedium "/>
</Relativelayout>
XML version = "1.0" encoding = "UTF-8"?>
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/widget37" Android: layout_width = "fill_parent"
Android: layout_height = "64dip">
<View Android: Id = "@ + ID/ListBox" Android: layout_width = "4dip"
Android: layout_height = "fill_parent" Android: Background = "# ff99ff00"/>
<Checkbox Android: Id = "@ + ID/listselected" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: layout_torightof = "@ ID/ListBox" Android: button = "@ layout/checkbox" Android: checked = "false" Android: layout_marginleft = "4dip" Android: layout_margintop = "1dip" Android: focusable = "false"/>
<Checkbox Android: Id = "@ + ID/liststared" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignparentright = "true" Android: button = "@ layout/Starbox" Android: checked = "false" Android: focusable = "false"/>
<Textview Android: Id = "@ + ID/listsubject" Android: layout_width = "wrap_content"
Android: layout_height = "36dip" Android: text = "title" Android: layout_alignparenttop = "true" Android: textcolor = "# ff888888"
Android: layout_toleftof = "@ ID/liststared" Android: textappearance = "? Android: ATTR/textappearancelarge"
Android: layout_torightof = "@ ID/listselected" Android: gravity = "bottom | left"/>
<Textview Android: Id = "@ + ID/listfrom" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: text = "mailfrom"
Android: layout_alignleft = "@ ID/listsubject"
Android: layout_below = "@ ID/listsubject" Android: textcolor = "# ff888888"
Android: textappearance = "? Android: ATTR/textappearancemedium"
/>
<Textview Android: Id = "@ + ID/listdate" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: text = "2010/2/0"
Android: layout_alignbaseline = "@ ID/listfrom" Android: layout_alignparentright = "true"
Android: layout_alignright = "@ ID/liststared" Android: textcolor = "# ff888888"
Android: textappearance = "? Android: ATTR/textappearancemedium "/>
Relativelayout>
The following results are achieved:
The next step is how to write the corresponding code to respond to each part.
1. The star is directly set by the bool type.
For the checkbox, If you directly use the cursoradapter method to map the boolean type to the checkbox, it will be displayed on the text part of the checkbox, rather than the display of the corresponding star. Therefore, we need to implement a viewbinder by ourselves. If this method encounters a checkbox during conversion, we will try to convert the bool type to the ischeck setting.
First, we inherit a simplecursoradapter and use setviewbinder during initialization to specify our own type conversion function.
View plaincopy to clipboardprint? ··· · 50 ······· · 90 ····· · 140 · 150setviewbinder (New simplecursoradapter. viewbinder () {public Boolean setviewvalue (view, cursor, int columnindex) {If (view instanceof checkbox) {checkbox v = (checkbox) view; string src = cursor. getstring (columnindex); If (SRC. equals ("true") {v. setchecked (true); Return true;} else if (SRC. equals ("false") {v. setchecked (true); Return true ;}} return false ;}}); setviewbinder (New simplecursoradapter. viewbinder (){
Public Boolean setviewvalue (view, cursor, int columnindex ){
If (view instanceof checkbox ){
Checkbox v = (checkbox) view;
String src = cursor. getstring (columnindex );
If (SRC. Equals ("true ")){
V. setchecked (true );
Return true;
}
Else if (SRC. Equals ("false ")){
V. setchecked (true );
Return true;
}
}
Return false;
}
});
First, determine whether it is a checkbox type, and then determine whether it can be converted to true or false. If yes, set the checked attribute of the checkbox.
If the value cannot be converted to true or false, false is returned. The adapter attempts to convert the value by other methods.
Multiple types of judgments can be added to a viewbinder. If the conversion succeeds, true is returned. If the conversion fails, false is returned. If the conversion fails, the adapter tries other possible conversions, if not, an exception is thrown.
2. Star Event Response
In order to increase the star event response, I found a lot of information and finally found a suitable setting method through several attempts. getview is responsible for the structure of each listitem in listview, modify the returned view and add an Event Response to set the corresponding event for each listitem.
View plaincopy to clipboardprint? ··· · 50 ······· · 90 ····· · 140 · 150public view getview (INT position, view convertview, viewgroup parent) {convertview = super. getview (Position, convertview, parent); checkbox cb = (checkbox) convertview. findviewbyid (R. id. liststared); CB. setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {// todo auto-generated method stub log. V ("12", this. tostring () ;}}); return convertview;} public view getview (INT position, view convertview, viewgroup parent ){
Convertview = super. getview (Position, convertview, parent );
Checkbox cb = (checkbox) convertview. findviewbyid (R. Id. liststared );
CB. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Log. V ("12", this. tostring ());
}
});
Return convertview;
}
As you only need to add a star response, the text display in listitem is completed by the parent class, so super is called first. getview (Position, convertview, parent); obtain the displayed view class, and then find the corresponding control in the view to add an event response.
3. listitem Event Response
This is simple. You can add a response to the listview. The only thing you need to note is to set the focusable to false for the checkbox, button, and other controls in the listitem, otherwise, because the checkbox or button occupies the focus of listitem, it does not respond to the click event of listitem.
Basically, a similar structure can be created, and the interface is still the same as the original simplecursoradapter, adding several processing methods, basically implementing a similar built-in mail list effect, however, we are still considering how each checkbox can be associated with its position...
For more information about adapter, see: adapter system http://hi.baidu.com/studymemo/blog/item/d9c3c518a7907972dbb4bd40.html
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/wangkemiao/archive/2010/05/19/5609268.aspx