Effect: The Item contains the title, description, and a Gallery.
Main configuration file ):
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<! -- Solved the bug of dragging background images in ListView: Adding android: cacheColorHint = "#00000000" in ListView -->
<! -- Add a separator to each item of ListView: android: divider = "@ color/snow" android: dividerHeight = "1dip" -->
<ListView android: id = "@ + id/my_lst" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" android: background = "@ drawable/bac"
Android: divider = "@ color/snow" android: dividerHeight = "1dip"
Android: cacheColorHint = "#00000000">
</ListView>
</LinearLayout>
ListView Item configuration file (items ):
[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout android: id = "@ + id/items"
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<TextView android: id = "@ + id/item_title" android: layout_width = "wrap_content"
Android: textSize = "20dip" android: layout_height = "wrap_content"
Android: textColor = "@ color/snow" android: layout_marginLeft = "10dip"
Android: layout_alignParentLeft = "true" android: gravity = "bottom"/>
<TextView android: id = "@ + id/item_describe" android: textColor = "@ color/snow"
Android: layout_width = "wrap_content" android: textSize = "15dip"
Android: layout_marginTop = "5dip" android: layout_height = "wrap_content"
Android: layout_marginLeft = "20dip" android: layout_toRightOf = "@ + id/item_title"
Android: gravity = "bottom"/>
<! -- Set the spacing of each Gallery item: android: spacing = "10dip" -->
<Gallery android: id = "@ + id/item_gallery" android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: spacing = "10dip"
Android: layout_below = "@ + id/item_title" android: layout_marginTop = "20dip"
Android: layout_marginBottom = "20dip"/>
</RelativeLayout>
Colors. xml in the values Folder:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Color name = "white" ># ffffff </color> <! -- White -->
<Color name = "snow"> # fffafa </color> <! -- Snow White -->
<Color name = "silver"> # c0c0c0 </color> <! -- Silver -->
<Color name = "blue" ># 0000FF </color> <! -- Blue -->
<Color name = "skyblue"> #87 ceeb </color> <! -- Sky blue -->
<Color name = "darkgrey"> # a9a9a9 </color> <! -- Dark gray -->
</Resources>
JavaBean (ListItem) corresponds to the item items of each ListView:
[Java]
Package com. march. view;
Import java. io. InputStream;
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. BitmapFactory. Options;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. ImageView;
Public class ListItem {
Public String title, describe;
Public int [] itemImages;
Public ImageAdapter adapter;
Public void initAdapter (Context context ){
This. adapter = new ImageAdapter (context );
}
Public class ImageAdapter extends BaseAdapter {
Private Context mContext;
Public ImageAdapter (Context context ){
This. mContext = context;
}
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return Integer. MAX_VALUE;
}
@ Override
Public Object getItem (int I ){
// TODO Auto-generated method stub
Return I;
}
@ Override
Public long getItemId (int I ){
// TODO Auto-generated method stub
Return I;
}
@ Override
Public View getView (int I, View view, ViewGroup viewgroup ){
View = new ImageView (mContext );
(ImageView) view). setImageBitmap (ListItem. readBitmap (mContext,
ItemImages [I % itemImages. length]);
Return view;
}
}
/***
* Read images of local resources in the most memory-saving manner
*
* @ Param context
* @ Param resId
* @ Return
*/
Public static Bitmap readBitmap (Context context, int rid ){
BitmapFactory. Options options = new BitmapFactory. Options ();
Options. inPreferredConfig = Bitmap. Config. RGB_565;
Options. inPurgeable = true;
Options. ininputretriable = true;
// Obtain the resource Image
InputStream is = context. getResources (). openRawResource (rid );
Return BitmapFactory. decodeStream (is, null, options );
}
}
MainActivity, display interface
[Java]
Package com. march. view;
Import java. util. ArrayList;
Import java. util. List;
Import android. app. Activity;
Import android. content. Context;
Import android. OS. Bundle;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. Gallery;
Import android. widget. ListView;
Import android. widget. TextView;
Import com. march. define. R;
Public class MainActivity extends Activity {
/** Called when the activity is first created .*/
Private ListView myLst;
Private List <ListItem> items = new ArrayList <ListItem> ();
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MyLst = (ListView) findViewById (R. id. my_lst );
InitItems ();
MyLst. setAdapter (new CustomSimpleAdapter (this, items ));
}
// Default data, which can be modified
Private static final String [] titles = new String [] {"title one ",
"Title two", "title three", "title four", "title five", "title six "};
Private static final String [] describes = new String [] {"describe one ",
"Describe two", "describe three", "describe four", "describe five ",
"Describe six "};
Private static final int [] images = new int [] {R. drawable. first,
R. drawable. second, R. drawable. three, R. drawable. four,
R. drawable. five, R. drawable. six };
Private void initItems (){
// Initialize 6 ListItem items
ListItem item = null;
For (int I = 0; I <6; I ++ ){
Item = new ListItem ();
Item. title = titles [I];
Item. describe = describes [I];
Item. itemImages = images;
Item. initAdapter (this );
Items. add (item );
}
}
Public class CustomSimpleAdapter extends BaseAdapter {
Private List <ListItem> items;
Private LayoutInflater layoutInflater;
Public CustomSimpleAdapter (Context context, List <ListItem> items ){
// TODO Auto-generated constructor stub
This. items = items;
LayoutInflater = (LayoutInflater) (Activity) context)
. GetSystemService (LAYOUT_INFLATER_SERVICE );
}
@ Override
Public int getCount (){
Return items. size ();
}
@ Override
Public Object getItem (int position ){
Return position;
}
@ Override
Public long getItemId (int position ){
Return position;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// If (null = convertView ){
ConvertView = layoutInflater. inflate (R. layout. items, null );
ListItem item = this. items. get (position );
TextView title = (TextView) convertView
. FindViewById (R. id. item_title );
Title. setText (item. title );
TextView describe = (TextView) convertView
. FindViewById (R. id. item_describe );
Describe. setText (item. describe );
Gallery gallery = (Gallery) convertView
. FindViewById (R. id. item_gallery );
Gallery. setAdapter (item. adapter );
Gallery. setSelection (Integer. MAX_VALUE/2 );
ConvertView. setTag (convertView );
/*} Else {
ConvertView = (View) convertView. getTag ();
Log. I ("test", "go here convertView ");
}*/
Return convertView;
}
}
}