当前位置:操作系统 > 安卓/Android >>

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: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配置文件(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 % itemIm

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,