效果如图: Item项包含标题、描述及一个Gallery.
主配置文件(main):
<?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):
<?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:
<?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项:
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,展示界面
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; } }}
源码下载链接:http://download.csdn.net/download/libei_march/4523293
没有下载分的童鞋请留下你的邮箱.- 2楼a59651075昨天 22:39
- [email protected]
- 1楼liweikang1208前天 16:29
- 很好哦,很实用哦