Android 自定義ListView + Gallery 組合

效果如圖:  Item項包含標題、描述及一個Gallery.


    主配置文件(main):

[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"> 
    <!– 解決ListView拖拽背景圖片的bug: ListView中加入android:cacheColorHint="#00000000" –> 
    <!– 給ListView每一項添加分隔線:android:pider="@color/snow" android:piderHeight="1dip" –> 
    <ListView android:id="@+id/my_lst" android:layout_width="fill_parent" 
        android:layout_height="fill_parent" android:background="@drawable/bac" 
        android:pider="@color/snow" android:piderHeight="1dip" 
        android:cacheColorHint="#00000000"> 
    </ListView> 
</LinearLayout> 

    ListView 的Item配置文件(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" /> 
    <!– 設置Gallery每一項的間距: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> 

values文件夾下colors.xml:

[html]
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="white">#ffffff</color><!–白色 –> 
    <color name="snow">#fffafa</color><!–雪白色 –> 
    <color name="silver">#c0c0c0</color><!–銀色 –> 
    <color name="blue">#0000FF</color><!– 藍色 –> 
    <color name="skyblue">#87ceeb</color><!–天藍色 –>   
    <color name="darkgrey">#a9a9a9</color><!–暗灰色 –>   
</resources> 

JavaBean(ListItem),對應到每一項ListView的item項:

[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; 
        } 
 
    } 
 
    /***
     * 以最省內存的方式讀取本地資源的圖片
     * 
     * @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.inInputShareable = true; 
        // 獲取資源圖片 
        InputStream is = context.getResources().openRawResource(rid); 
        return BitmapFactory.decodeStream(is, null, options); 
    } 

 

MainActivity,展示界面
[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)); 
    } 
 
    //默認數據,可修改 
    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() { 
        // 初始化6個ListItem項 
        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; 
        } 
 
    } 
 

發佈留言