1. When a subclass of linearlayout is defined as the itemview of listview
When we need to use the list to display data, listview and adapter are required APIs. The getview method in the adapter serves to create itemview for the listview and how to layout the itemview, the getview method determines what content to display. How should we deploy itemview? Generally, we create a layout subclass, for example
Private Class Hahaitem Extends Linearlayout {
Public Int ID;
Public Textview mdate;
Public Textview mcontent;
Public Textview mpublisher;
Public Imageview musericon;
Public Hahaitem (context ){
Super (Context );
Setupui ();
}
Private Void Setupui (){
View. Inflate (getcontext (), R. layout. list_haha_item, This );
Mdate = (Textview) This . Findviewbyid (R. Id. haha_pubdate );
Mcontent = (Textview) This . Findviewbyid (R. Id. haha_content );
Mpublisher = (Textview) This . Findviewbyid (R. Id. haha_publisher );
/**
* Set the font of Chinese characters to bold. In textview, textstyle = bold is only valid for English characters and must be used.
* Set the Chinese font to bold as follows:
* */
Textpaint TP = Mpublisher. getpaint ();
TP. setfakeboldtext ( True );
Musericon = (Imageview) This . Findviewbyid (R. Id. haha_head_img );
}
}
r. layout. list_haha_item Layout Method:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_height = "wrap_content" Android: layout_width = "fill_parent"> <textview Android: id = "@ + ID/haha_content" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: textsize = "17sp" Android: textstyle = "normal" Android: typeface = "monospace" Android: paddingleft = "18dip" Android: paddingright = "18dip" Android: layout_margintop = "18dip" Android: linespacingextra = "5dip" Android: textscalex = "1.1" Android: maxlines = "5" Android: layout_marginbottom = "18dip" Android: textcolor = "@ color/dark_gray"/> <relativelayout Android: id = "@ + ID/haha_bottombar" Android: layout_width = "fill_parent" Android: layout_height = "46dip" Android: Background = "@ drawable/test" Android: layout_margintop = "2dip"> <linearlayout Android: Id = "@ + ID/pub" Android: Orientation = "horizontal" Android: layout_width = "wrap_content" Android: layout_height = "fill_parent" Android: layout_alignparentleft = "true"> <imageview Android: Id = "@ + ID/haha_head_img" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: maxheight = "25dip" Android: adjustviewbounds = "true" Android: maxwidth = "25dip" Android: baselinealignbottom = "true" Android: layout_margintop = "7dip" Android: layout_marginleft = "5dip"/> <textview Android: Id = "@ + ID/haha_publisher" Android: layout_marginleft = "10dip" Android: paddingleft = "5dip" Android: layout_width = "wrap_content" Android: layout_height = "fill_parent" Android: singleline = "true" Android: textstyle = "bold" Android: textcolor = "#889db6" Android: gravity = "center_vertical"/> </linearlayout> <textview Android: Id = "@ + ID/haha_pubdate" Android: layout_width = "wrap_content" Android: layout_height = "fill_parent" Android: singleline = "true" Android: layout_alignparentright = "true" Android: textstyle = "normal" Android: gravity = "center_vertical" Android: textcolor = "# 7b7b7b" Android: layout_alignparentbottom = "true" Android: textsize = "14sp"/> </relativelayout> </linearlayout>
In this way, you can create an object in getview TO USECodeIt becomes neat.
But there will be a problem, that is, a linearlayout is nested under the hahaitem, and a layer of linearlayout is added to the view layer, which is unnecessary. How can we remove this layer?
Currently, there are two methods. One is to completely customize the hahaitem and make it into a control similar to the system view, such as textview. This is good, but it is very troublesome.
Another method is to use the merge tag to change the top-layer linearlayout in the XML layout to merge.Android: Orientation = "vertical" Android: layout_height = "wrap_content"Android: layout_width = "fill_parent and other attributes are removed, because it does not work even if written.
Use the code in the setupui method of hahaitem to dynamically set its attributes. This will reduce the redundant linearlayout layer.
2. When using code to create a control, how do I set its layoutparams?
Start with the following error code:
Public View getview ( Int Position, view convertview, viewgroup parent ){
Textview item;
If (Convertview = Null ){
Item = New Textview (getcontext ());
Item. setlayoutparams ( New Abslistview. layoutparams (abslistview. layoutparams. fill_parent, 60 ));
} Else {
Item = (Textview) convertview;
}
Final Jifengiteminfo info = Minfolist. Get (position );
Item. settext (info. Name );
Return Item;
}
Item. setlayoutparams (New viewgroup. layoutparams (
Viewgroup. layoutparams. fill_parent, 60); this is incorrect. Why?
First look at a picture:
From this figure, we can see that the layoutparams of the View control is determined by its parent container. That is to say, when setting the layout attribute for a view, the layoutparams of its parent container must be set, this is why the above Code mustItem. setlayoutparams (NewAbslistview. layoutparams (abslistview. layoutparams. fill_parent,60); In this way, the parent container of item is listview, so the layouparams of listview must be set.
3. classcastexception in the getview method of arrayadapter
@ Override
Public View getview ( Int Position, view convertview, viewgroup parent ){
Textview TV;
If (Convertview = Null ){
TV = (Textview) view. Inflate (getcontext (), mresource, Null );
} Else {
TV = (Textview) convertview;
}
Jifengiteminfo info = Getitem (position );
TV. settag (getitem (position ));
TV. settext (info. Name );
Return TV;
}
Note:
1. the XML layout file represented by mresource is a complex layout, and the top layer is linearlayout, which contains a textview
2. The getview method is the method in arrayadapter.
Why is a class conversion exception?
Although the getview method returns textview, The convertview type is linearlayout.TV=(Textview) convertview; Class conversion exception is thrown.
Textview TV is a child control in linearlayout. It depends on linearlayout. Although this textview object can be obtained, it cannot be obtained separately and stored in listview, the above Code actually stores the itemview in the listview as the linearlayout type.
This mistake is easy to make. Pay attention to it next time.