android提供的ListView在很多情况下满足不了我们的展现需求,这里我以一个图书列表为例,实现自定义的ListView
先看下要实现的效果,左侧显示图片,右边显示标题以及章节等信息,实现步骤如下:
1. 创建一个用于控制每行显示效果的layout,名称为bookshelf
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/book_image" android:layout_width="80dip" android:layout_height="80dip" android:padding="5dip" android:paddingLeft="0dip" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/book_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dip" android:layout_marginTop="2dip" android:textIsSelectable="true" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="5dip" > <TextView android:id="@+id/book_no_read_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textIsSelectable="true" android:textSize="12sp" /> <ImageView android:id="@+id/book_has_update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_marginTop="3dip" android:visibility="gone" android:contentDescription="@string/has_update" android:src="@drawable/ic_new" /> </LinearLayout> <TextView android:id="@+id/book_lasttitle" android:layout_width="240dip" android:layout_height="wrap_content" android:ellipsize="end" android:paddingRight="5dip" android:singleLine="true" android:textIsSelectable="true" android:textSize="12sp" /> </LinearLayout> </TableRow> </TableLayout>
2. 创建一个新的ListViewAdapter,名称为bookshelfListViewAdapter
package com.brook.freenovelread.service; import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.brook.freenovelread.R; import com.brook.freenovelread.object.BookData; import com.brook.freenovelread.utility.HttpUtility; public class bookshelfListViewAdapter extends BaseAdapter { private ArrayList<BookData> bookshelfList = null; private Context context = null; /** * 构造函数,初始化Adapter,将数据传入 * @param bookshelfList * @param context */ public bookshelfListViewAdapter(ArrayList<BookData> bookshelfList, Context context) { this.bookshelfList = bookshelfList; this.context = context; } @Override public int getCount() { return bookshelfList == null ? 0 : bookshelfList.size(); } @Override public Object getItem(int position) { return bookshelfList == null ? null : bookshelfList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //装载view LayoutInflater layoutInflater = LayoutInflater.from(this.context); View view = layoutInflater.inflate(R.layout.bookshelf, null); //获取控件 ImageView bookImageView = (ImageView) view.findViewById(R.id.book_image); TextView bookNameTextView = (TextView) view.findViewById(R.id.book_name); TextView bookNoReadNumTextView = (TextView) view.findViewById(R.id.book_no_read_num); TextView bookLastTitleView = (TextView) view.findViewById(R.id.book_lasttitle); ImageView bookHasUpdateImageView = (ImageView) view.findViewById(R.id.book_has_update); //对控件赋值 BookData bookData = (BookData) getItem(position); if (bookData != null) { bookImageView.setImageBitmap(HttpUtility.getHttpBitmap(bookData.getImageUrl())); bookNameTextView.setText(bookData.getName()); Integer noReadNum = bookData.getTotalNum() - bookData.getCurrentNum(); if (noReadNum > 0) { bookNoReadNumTextView.setText(noReadNum + "章节未读"); //显示更新小图标 bookHasUpdateImageView.setVisibility(View.VISIBLE); } else { bookNoReadNumTextView.setText("无未读章节"); //隐藏更新小图标 bookHasUpdateImageView.setVisibility(View.GONE); } bookLastTitleView.setText("更新至:" + bookData.getLastTitle()); } return view; } }
主要是对getView方法的重写,将数据插入到R.layout.bookshelf的各控件中,这里还用到了一个将网络图片下载的工具类HttpUtility,以下是工具类的代码
package com.brook.freenovelread.utility; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.Map; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class HttpUtility { /** * 图片资源缓存 */ private static Map<String, Bitmap> bitmapCache = new HashMap<String, Bitmap>(); /** * 获取网落图片资源 * @param url * @return */ public static Bitmap getHttpBitmap(String url) { //先从缓存里找 Bitmap bitmap = bitmapCache.get(url); if (bitmap != null) { return bitmap; } //从网络上下载 URL myFileURL; try { myFileURL = new URL(url); //获得连接 HttpURLConnection conn = (HttpURLConnection) myFileURL.openConnection(); //设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制 conn.setConnectTimeout(6000); //连接设置获得数据流 conn.setDoInput(true); //不使用缓存 conn.setUseCaches(false); //这句可有可无,没有影响 //conn.connect(); //得到数据流 InputStream is = conn.getInputStream(); //解析得到图片 bitmap = BitmapFactory.decodeStream(is); //关闭数据流 is.close(); } catch (Exception e) { e.printStackTrace(); } if (bitmap != null) { bitmapCache.put(url, bitmap); } return bitmap; } }
3.在activity的layout文件中加上一个ListView控件
<ListView android:id="@+id/listview_bookshelf" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" />
4.在activity中调用我们写的ListViewAdapter
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置主页面的标题栏 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar); //更新标题栏中按钮的字体大小 Button btnManage = (Button) findViewById(R.id.btn_manage); btnManage.setTextSize(10); //获取书架列表数据 ArrayList<BookData> bookshelf = new ArrayList<BookData>(); BookData bookData = new BookData(); bookData.setAuthor("天蚕土豆"); bookData.setCurrentNum(1); bookData.setDescription("修炼一途,乃窃阴阳,夺造化,转涅盘,握生死,掌轮回。武之极,破苍穹,动乾坤!"); bookData.setId(1); bookData.setImageUrl("http://www.easou.org/files/article/image/0/308/308s.jpg"); bookData.setLastTitle("第一千两百九十四章 魔皇之手"); bookData.setName("武动乾坤"); bookData.setTotalNum(1294); BookData bookData2 = new BookData(); bookData2.setAuthor("忘语"); bookData2.setCurrentNum(2343); bookData2.setDescription("一个普通的山村穷小子,偶然之下,进入到当地的江湖小门派,成了一名记名弟子。他以这样的身份,如何在门派中立足?又如何以平庸的资质,进入到修仙者的行列?和其他巨枭魔头,仙宗仙师并列于山海内外?希望书友们喜欢本书!"); bookData2.setId(2342); bookData2.setImageUrl("http://www.easou.org/files/article/image/0/289/289s.jpg"); bookData2.setLastTitle("第十一卷 真仙降世 第两千三百四十三章 九目血蟾"); bookData2.setName("凡人修仙传"); bookData2.setTotalNum(2343); bookshelf.add(bookData); bookshelf.add(bookData2); bookshelf.add(bookData); bookshelf.add(bookData2); bookshelf.add(bookData); bookshelf.add(bookData2); bookshelfListViewAdapter bookshelfListViewAdapter = new bookshelfListViewAdapter(bookshelf, this); ListView listView = (ListView) findViewById(R.id.listview_bookshelf); listView.setAdapter(bookshelfListViewAdapter); }
这样就ok了