优化使用的BaseAdapter

ListView在使用自定的BaseAdapter时,为了加载快速,用到了封装item的方法和ViewHolder的方法。代码如下:

首先想要在显示一个ListView,在activity_mian.xml中加入一个ListView:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.chase.baseadapterwell.MainActivity" >

    <ListView 
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >ListView>

RelativeLayout>

ListView的每个Item的布局item.xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center"
        android:layout_toEndOf="@+id/iv_image"
        android:text="Title"
        android:textSize="25sp" />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_toEndOf="@+id/iv_image"
        android:layout_below="@+id/tv_title"
        android:gravity="center_vertical"
        android:text="Content"
        android:textSize="25sp" />

RelativeLayout>

将上面的布局封装为ItemBean,新建一个类ItemBean

package com.chase.baseadapterwell;

public class ItemBean {

    public int ItemImageResid;
    public String ItemTitle;
    public String ItemContent;

    //形成构造方法在eclipse中是Source-Generate Constructor using Fields...
    public ItemBean(int itemImageResid, String itemTitle, String itemContent) {
        super();
        ItemImageResid = itemImageResid;
        ItemTitle = itemTitle;
        ItemContent = itemContent;
    }

}

新建MyAdapter类继承BaseAdapter

package com.chase.baseadapterwell;

import java.util.List;

import android.content.Context;
import android.provider.Telephony.Sms.Conversations;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

    private List mList; // 创建一个成员变量用来保存传进来的数据
    private LayoutInflater mInflater; // 要将布局转为view 用到inflater

    public MyAdapter(Context context, List list) {
        mList = list; // 创建的成员变量=传进来的
        // 所以在数据适配器中,只需要对mlist进行操作就可以了
        mInflater = LayoutInflater.from(context); // 用context对minflater初始化
    }

    // 返回ListView需要的数据数量
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mList.size();
    }

    // 指定索引对于数据项
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mList.get(position);
    }

    // 返回对应索引
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item, null); // 将xml文件转为view

            viewHolder = new ViewHolder();
            // 用viewHolder来保存findviewByid找到的对象
            viewHolder.imageView = (ImageView) convertView
                    .findViewById(R.id.iv_image);
            viewHolder.title = (TextView) convertView
                    .findViewById(R.id.tv_title);
            viewHolder.content = (TextView) convertView
                    .findViewById(R.id.tv_content);
            convertView.setTag(viewHolder); // 将viewHolder与convertView绑定
        } else {
            viewHolder = (ViewHolder) convertView.getTag();// getTag取出关联的ViewHolder
                                                            // 通过ViewHolder找到对应控件
        }

        ItemBean bean = mList.get(position);
        viewHolder.imageView.setImageResource(bean.ItemImageResid);
        viewHolder.title.setText(bean.ItemTitle);
        viewHolder.content.setText(bean.ItemContent);
        return convertView;
    }

    // 创建内部类ViewHolder 对应布局中的控件
    class ViewHolder {
        public ImageView imageView;
        public TextView title;
        public TextView content;
    }
}

MainActivity中 做一个演示用的数据源 并且listview设定适配器`

package com.chase.baseadapterwell;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //制造一个演示用的数据源
        ListitemBeanList = new ArrayList();
        for (int i = 0; i < 20; i++) {
            itemBeanList.add(new ItemBean(R.drawable.ic_launcher, 
                    "我是标题"+i, "我是内容"+i));
        }

        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(new MyAdapter(this, itemBeanList)); //(上下文,数据源)

    }
}

你可能感兴趣的:(优化使用的BaseAdapter)