细解ListView之自定义适配器

下面我们将以一个例子来讲述ListView之自定义适配器

首先我们看一下效果图:

listView

【分析】

首先:需要创建一个ListView控件,自定义适配器是为了实现自定义ListViewListView_Item,所以我们应该在创建一个Item

然后书写自定义适配器,自定义适配器的时候需要继承BaseAdapter,然后实现它的未实现的4个方法,然后在实现自定义适配器的构造方法,其构造方法的参数为山下文对象,数据源,必须实现的4个方法主要是对数据源进行过操作,

然后考虑优化方面,我们使用了ViewHolder Pattern,用它的优点(我们来看一下来自国外网友的看法):

   Now let’s see how it works with the ViewHolder pattern.

        1.The first time it was loaded, convertView is null. We’ll have to inflate our list item layout, instantiate the ViewHolder, find the TextView via

        findViewById·() and assign it to the ViewHolder, and set the ViewHolder as tag of convertView.

       2.The second time it was loaded, convertView is not null, good! We don’t have to inflate it again. And here’s the sweet thing, we won’t have to call

      findViewById() since we can now access the TextView via its ViewHolder.

       3.The following time it was loaded, convertView is definitely not null. The findViewById() is never called again, and that makes our smooth ListView scrolling.

                                                                 附上原文http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html  】

以下来自国内网友的看法:

ViewHolder将需要缓存的view封装好,convertViewsetTag才是将这些缓存起来供下次调用。 当你的listview里布局多样化的时候 viewholder的作用体现明显,效率再一次提高。 ViewfindViewById()方法也是比较耗时的,因此需要考虑只调用一次,之后就用View.getTag()方法来获得 ViewHolder对象。(小编比较赞同这个)                                                        

                                                               【附上原文http://www.xker.com/page/e2014/1217/150764.html

【编码】

首先,我们先看自定义适配器这部分的代码:

package cn.edu.bzu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import org.w3c.dom.Text;

import java.util.List;

import cn.edu.bzu.entity.Bean;
import cn.edu.bzu.listview_demo.R;

/**
 * Created by monster on 2015/4/19.
 */
public class MyListViewAdapter extends BaseAdapter {
    private LayoutInflater mInflater; //LayoutInflater是用来找layout下xml布局文件,并且实例化
    private List<Bean> mlist;
    public MyListViewAdapter(Context context,List<Bean> list) {
        mInflater=LayoutInflater.from(context); //得到初始化上下文
        mlist=list;
    }
    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            convertView=mInflater.inflate(R.layout.listview_item,null);  //将布局转换成视图
            holder=new ViewHolder();
            holder.mTitle=(TextView)convertView.findViewById(R.id.id_title);
            holder.mDesc=(TextView)convertView.findViewById(R.id.id_desc);
            holder.mDate=(TextView)convertView.findViewById(R.id.id_time);
            holder.mPhone=(TextView)convertView.findViewById(R.id.id_phone);
            convertView.setTag(holder);
        }else {
            //ViewHolder被复用
            holder=(ViewHolder)convertView.getTag();
        }
        holder.mTitle.setText(mlist.get(position).getTitle());
        holder.mDesc.setText(mlist.get(position).getDesc());
        holder.mPhone.setText(mlist.get(position).getPhone());
        holder.mDate.setText(mlist.get(position).getDate());
        return convertView;
    }
    private class ViewHolder{
        TextView mTitle;
        TextView mDesc;
        TextView mDate;
        TextView mPhone;
    }
}

 然后我们看一下MainActivity.java

package cn.edu.bzu.listview_demo;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ListView;

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

import cn.edu.bzu.adapter.MyListViewAdapter;
import cn.edu.bzu.entity.Bean;


public class MainActivity extends ActionBarActivity {
    private ListView listView;
    private List<Bean> list;
    private MyListViewAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getData();
        initView();
    }

    private void initView() {
        listView=(ListView)findViewById(R.id.ListView);
        listView.setAdapter(adapter);
    }

    private void getData() {
        list=new ArrayList<Bean>();
        list.add(new Bean("Android新技能1","Android打造万能的ListView和GridView适配器1","2015-04-19","10086"));
        list.add(new Bean("Android新技能2","Android打造万能的ListView和GridView适配器2","2015-04-19","10086"));
        list.add(new Bean("Android新技能3","Android打造万能的ListView和GridView适配器3","2015-04-19","10086"));
        list.add(new Bean("Android新技能4","Android打造万能的ListView和GridView适配器4","2015-04-19","10086"));
        adapter=new MyListViewAdapter(MainActivity.this,list);
    }
}

--------------------------------------------------------------------------------------------------------------------------------------------

【总结】

创建自定义适配器的步骤:

1.继承BaseAdapter,实现BaseAdapter未实现的方法,包括(getCount(),getItem(),getItemId(),getView())

2.创建含参的构造方法,参数包括(Context ,List集合)

3.创建ViewHolder类,ViewHolder主要包括listView_Item的控件类型

4.书写getView方法,包括实例化控件,控件赋值

【项目托管】

URL:(https://github.com/monsterLin/ListView_Custom-adapter);

--来自Android Stdio编写

posted @ 2015-04-19 13:19  monsterLin  阅读(3802)  评论(1编辑  收藏  举报
小弟博客地址迁移,欢迎戳这里查看新博客~