kkk

1 篇文章 0 订阅
1 篇文章 0 订阅

1. ArrayAdapter(数组适配器)

适用:用于绑定格式单一的数据; 
数据源:可以使集合或数组。

<code class=" hljs java" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">MainActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">extends</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">Activity</span> {</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> ListView listView;
<span class="hljs-comment" style="color: rgb(136, 0, 0);">// 1. 新建一个数据适配器</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> ArrayAdapter<String> arr_aAdapter; 

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView)findViewById(R.id.listView1);
        <span class="hljs-comment" style="color: rgb(136, 0, 0);">//  创建适配器对象时将数据加载到适配器里</span>
   <span class="hljs-javadoc" style="color: rgb(136, 0, 0);">/**new ArrayAdapter<T>(context, textViewResourceId)
         * context--  上下文,一般为this
         * textViewResourceId-- 当前ListView加载的每一个列表项所对应的布局文件【这里采用系统默认的一个布局android.R.layout.simple_list_item_1】
      */</span>
      <span class="hljs-comment" style="color: rgb(136, 0, 0);">//  2. 添加数据源到适配器</span>
        String[] arr_data = {<span class="hljs-string" style="color: rgb(0, 136, 0);">"fanff"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"fan"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"tencent"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"QQ"</span>};<span class="hljs-comment" style="color: rgb(136, 0, 0);">// 创建的数据源</span>
        arr_aAdapter = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> ArrayAdapter<String>(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>, android.R.layout.simple_list_item_1, arr_data);
        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 3. 视图(ListView)加载适配器</span>
        listView.setAdapter(arr_adAdapter);
    }    
}</code>

2. SimpleAdapter(简单适配器)

适用:绑定格式复杂的数组; 
数据源:只能是特定泛型的集合。

<code class=" hljs java" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">MainActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">extends</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">Activity</span> {</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> ListView listView;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> SimpleAdapter sim_aAdapter; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 1. 新建一个数据适配器</span>

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> List<Map<String, Object>>dataList; <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 数据源</span>

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listView = (ListView)findViewById(R.id.listView1); 

        <span class="hljs-javadoc" style="color: rgb(136, 0, 0);">/** SimpleAdapter(context, data, resource, from, to)
         *  context: 上下文
         *  data: 数据源(List<? extends Map<String, ?>> data),一个Map所组成的List集合
         *        每个Map都会对应ListView列表中的一行,Map是由键【必须包含所有在from中所指定的键】值对组成
         *  resource:列表中的布局文件的ID,此处的布局是自定义的
         *  from:Map中的键名
         *  to:绑定数据视图中的ID,与from成对应关系
         */</span>  
        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 2. 适配器加载数据源</span>
        dataList = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> ArrayList<Map<String, Object>>();
        sim_aAdapter = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> SimpleAdapter(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>, getData(), R.layout.item, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> String[]{<span class="hljs-string" style="color: rgb(0, 136, 0);">"pic0"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"text0"</span>}, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span>[]{R.id.pic, R.id.text}); 
        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 3. 视图(ListView)加载适配器</span>
        listView.setAdapter(sim_aAdapter);        
    }

   <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> List<Map<String, Object>> <span class="hljs-title">getData</span>(){
       <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>; i < <span class="hljs-number" style="color: rgb(0, 102, 102);">20</span>; i++){
           Map<String, Object>map = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> HashMap<String, Object>();
           map.put(<span class="hljs-string" style="color: rgb(0, 136, 0);">"pic0"</span>, R.drawable.ic_launcher);
           map.put(<span class="hljs-string" style="color: rgb(0, 136, 0);">"text0"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"fanff"</span>+i);
           dataList.add(map);
       }
       <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> dataList;
   }   
}</code>

一般来讲,简单适配器的数据源是一个集合,所以一般写一个方法来处理(例如getData())。

其中自定义的item.xml布局

<code class=" hljs xml" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-pi" style="color: rgb(0, 102, 102);"><?xml version="1.0" encoding="utf-8"?></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">LinearLayout</span> <span class="hljs-attribute" style="color: rgb(102, 0, 102);">xmlns:android</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"http://schemas.android.com/apk/res/android"</span>
    <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"match_parent"</span>
    <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"match_parent"</span>
    <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:orientation</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"horizontal"</span> ></span>

    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">ImageView
</span>        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@+id/pic"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"wrap_content"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"wrap_content"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_marginLeft</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"15dp"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:src</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@drawable/ic_launcher"</span>
        /></span>

    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">TextView
</span>        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@+id/text"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_marginTop</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"12dp"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"wrap_content"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"wrap_content"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:textSize</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"20sp"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:textColor</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"#000000"</span>
        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:text</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"hello"</span> 
        /></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">LinearLayout</span>></span></code>

3.继承BaseAdapter的自定义的适配器

这个玩法比较多,这里先不介绍,直接见下面的。

4. 监听器

(1). 监听器是程序和用户(或系统)交互的桥梁,这里不多讲了,毕竟用的多。ListView中的两个常用监听器:OnItemClickListener和OnScrollListener。 
(2). OnItemClickListener可以处理视图中单个条目的点击事件;OnScrollListener监测滚动的变化,可以用于视图在滚动中加载数据。

<code class=" hljs java" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">MainActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">extends</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">Activity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">implements</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">OnItemClickListener</span>,
        <span class="hljs-title" style="color: rgb(102, 0, 102);">OnScrollListener</span> {</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> ListView listView;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> ArrayAdapter<String> arr_aAdapter;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> SimpleAdapter sim_aAdapter;

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> List<Map<String, Object>> dataList;

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listView = (ListView) findViewById(R.id.listView1);

        <span class="hljs-javadoc" style="color: rgb(136, 0, 0);">/**
         * 1. 新建一个数据适配器 context: 上下文 data: 数据源(List<? extends Map<String, ?>>
         * data),一个Map所组成的List集合
         * 每个Map都会对应ListView列表中的一行,Map是由键【必须包含所有在from中所指定的键】值对组成 from:Map中的键名
         * resource:列表中的布局文件的ID to:绑定数据视图中的ID,与from成对应关系
         */</span>
        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 2. 适配器加载数据源</span>
        dataList = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> ArrayList<Map<String, Object>>();
        sim_aAdapter = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> SimpleAdapter(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>, getData(), R.layout.item,
                <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> String[] { <span class="hljs-string" style="color: rgb(0, 136, 0);">"pic0"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"text0"</span> }, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span>[] { R.id.pic,
                        R.id.text });
        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 3. 视图(ListView)加载适配器</span>
        listView.setAdapter(sim_aAdapter);

        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 监听器</span>
        listView.setOnItemClickListener(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0);">// 单击单个条目</span>
        listView.setOnScrollListener(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0);">// 视图在滚动中加载数据</span>
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> List<Map<String, Object>> <span class="hljs-title">getData</span>() {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>; i < <span class="hljs-number" style="color: rgb(0, 102, 102);">20</span>; i++) {
            Map<String, Object> map = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> HashMap<String, Object>();
            map.put(<span class="hljs-string" style="color: rgb(0, 136, 0);">"pic0"</span>, R.drawable.ic_launcher);
            map.put(<span class="hljs-string" style="color: rgb(0, 136, 0);">"text0"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"fanff"</span> + i);
            dataList.add(map);
        }
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> dataList;
    }

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onScroll</span>(AbsListView view, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> firstVisibleItem,
            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> visibleItemCount, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> totalItemCount) {
        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// TODO Auto-generated method stub</span>

    }

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onScrollStateChanged</span>(AbsListView view, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> scrollState) {
        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// TODO Auto-generated method stub</span>
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">switch</span> (scrollState) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">case</span> SCROLL_STATE_FLING:
            Log.i(<span class="hljs-string" style="color: rgb(0, 136, 0);">"ScrollState"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"用户在手指离开屏幕之前,由于用力滑了一下,视图仍依靠惯性在继续滑行"</span>);
            Map<String, Object> map = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> HashMap<String, Object>();
            map.put(<span class="hljs-string" style="color: rgb(0, 136, 0);">"pic0"</span>, R.drawable.ic_launcher);
            map.put(<span class="hljs-string" style="color: rgb(0, 136, 0);">"text0"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"fresh"</span>);
            dataList.add(map);
            sim_aAdapter.notifyDataSetChanged();<span class="hljs-comment" style="color: rgb(136, 0, 0);">// 通知UI进程刷新界面</span>
            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">break</span>;
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">case</span> SCROLL_STATE_IDLE:
            Log.i(<span class="hljs-string" style="color: rgb(0, 136, 0);">"ScrollState"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"视图已经停止滑动"</span>);
            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">break</span>;
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">case</span> SCROLL_STATE_TOUCH_SCROLL:
            Log.i(<span class="hljs-string" style="color: rgb(0, 136, 0);">"ScrollState"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"手指乜有离开屏幕,视图正在滑动"</span>);
            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">break</span>;
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">default</span>:
            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">break</span>;
        }
    }

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onItemClick</span>(AdapterView<?> parent, View view, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> position,
            <span class="hljs-keyword" style="color: rgb(0, 0, 136);">long</span> id) {
        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// TODO Auto-generated method stub</span>
        String text = listView.getItemAtPosition(position) + <span class="hljs-string" style="color: rgb(0, 136, 0);">""</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0);">// 指定位置的内容</span>
        Toast.makeText(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">"positon="</span> + position + <span class="hljs-string" style="color: rgb(0, 136, 0);">"text"</span> + text,
                Toast.LENGTH_LONG).show();
    }
}</code>

三、ListView中的BaseAdapter

这里着重来介绍一下BaseAdapter,各种方式的比较见代码注释。 
源码下载:https://github.com/herdyouth/ListView 
MainActivity

<code class=" hljs java" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">MainActivity</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">extends</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">AppCompatActivity</span> {</span>

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">protected</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<ItemBean> itemBeanList = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> ArrayList<>();
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102);">0</span>; i < <span class="hljs-number" style="color: rgb(0, 102, 102);">20</span>; i++){
            itemBeanList.add(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> ItemBean(R.mipmap.ic_launcher,
                    <span class="hljs-string" style="color: rgb(0, 136, 0);">"标题"</span> + i, <span class="hljs-string" style="color: rgb(0, 136, 0);">"内容"</span> + i));
        }

        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 数据源与适配器的绑定</span>
        ListView listView = (ListView) findViewById(R.id.lview);
        listView.setAdapter(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> MyBaseAdapter(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">this</span>, itemBeanList));
    }
}
</code>

BaseAdapter各种方式的对比,一步步优化的原因如代码注释

<code class=" hljs java" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-javadoc" style="color: rgb(136, 0, 0);">/**
 * 创建数据适配器
 * Created by herd_youth on 2016/4/15.
 */</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">MyBaseAdapter</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">extends</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">BaseAdapter</span>{</span>

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> List<ItemBean> mList;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> LayoutInflater mInflater;

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 通过构造器关联数据源与数据适配器</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-title">MyBaseAdapter</span>(Context context, List<ItemBean> list){
        mList = list;
        <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 使用当前要使用的界面对象context去初始化布局装载器对象mInflater</span>
        mInflater = LayoutInflater.from(context);
    }

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> <span class="hljs-title">getCount</span>() {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> mList.size();
    }

    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> Object <span class="hljs-title">getItem</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> position) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> mList.get(position);
    }

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 返回指定索引对应的数据项</span>
    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">long</span> <span class="hljs-title">getItemId</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> position) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> position;
    }


   <span class="hljs-comment" style="color: rgb(136, 0, 0);">/* */</span><span class="hljs-javadoc" style="color: rgb(136, 0, 0);">/**
     * 返回每一项对应的内容
     * 缺点:没有利用到ListView的缓存机制
     *      每次都会创建新的View,不管当前这个Item是否在屏幕上被调用过(即是否被缓存过)
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102);"> @param</span> position
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102);"> @param</span> convertView
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102);"> @param</span> parent
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102);"> @return</span>
     */</span><span class="hljs-comment" style="color: rgb(136, 0, 0);">/*
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 将布局文件转为View对象
        View view = mInflater.inflate(R.layout.item, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.iv_img);
        TextView title = (TextView) view.findViewById(R.id.tv_title);
        TextView content = (TextView) view.findViewById(R.id.tv_content);
        ItemBean bean = mList.get(position);
        imageView.setImageResource(bean.getItemImageResid());
        title.setText(bean.getItemContent());
        content.setText(bean.getItemContent());

        return view;
    }*/</span>

    <span class="hljs-javadoc" style="color: rgb(136, 0, 0);">/**
     * 改善处:使用系统的convertView来较好的利用ListView的缓存机制,避免重复大量的创建convertView
     * 缺点:findViewById依然会浪费大量的时间去调用视图树
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102);"> @param</span> position
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102);"> @param</span> convertView
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102);"> @param</span> parent
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102);"> @return</span>
     */</span><span class="hljs-comment" style="color: rgb(136, 0, 0);">/*
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){// View未被实例化,即缓冲池中无缓存才创建View
            convertView = mInflater.inflate(R.layout.item, null);
        }else{
            ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_img);
            TextView title = (TextView) convertView.findViewById(R.id.tv_title);
            TextView content = (TextView) convertView.findViewById(R.id.tv_content);
            ItemBean bean = mList.get(position);
            imageView.setImageResource(bean.getItemImageResid());
            title.setText(bean.getItemContent());
            content.setText(bean.getItemContent());
        }
        return convertView;
    }*/</span>

    <span class="hljs-javadoc" style="color: rgb(136, 0, 0);">/**
     * 既利用了ListView的缓存,
     * 更通过ViewHolder类来显示数据的视图的缓存,避免了多次通过findViewById寻找控件
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102);"> @param</span> position
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102);"> @param</span> convertView
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102);"> @param</span> parent
     *<span class="hljs-javadoctag" style="color: rgb(102, 0, 102);"> @return</span>
     */</span>
    <span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Override</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> View <span class="hljs-title">getView</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">if</span> (convertView == <span class="hljs-keyword" style="color: rgb(0, 0, 136);">null</span>){<span class="hljs-comment" style="color: rgb(136, 0, 0);">// View未被实例化,即缓冲池中无缓存才创建View</span>
            <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 将控件id保存在viewHolder中</span>
            viewHolder = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> ViewHolder();
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_img);
            viewHolder.title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.content = (TextView) convertView.findViewById(R.id.tv_content);
            <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 通过setTag将ViewHolder与convertView绑定</span>
            convertView.setTag(viewHolder);
        } <span class="hljs-keyword" style="color: rgb(0, 0, 136);">else</span>{
            <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 通过ViewHolder对象找到对应控件</span>
            viewHolder = (ViewHolder) convertView.getTag();
            ItemBean bean = mList.get(position);
            viewHolder.imageView.setImageResource(bean.getItemImageResid());
            viewHolder.title.setText(bean.getItemContent());
            viewHolder.content.setText(bean.getItemContent());
        }
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> convertView;
    }

    <span class="hljs-comment" style="color: rgb(136, 0, 0);">// 避免重复的findViewById的操作</span>
    class ViewHolder{
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> ImageView imageView;
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> TextView title;
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> TextView content;
    }
}</code>
<code class=" hljs java" style="display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="hljs-javadoc" style="color: rgb(136, 0, 0);">/**
 * 创建设置每个Item的类
 * Created by herd_youth on 2016/4/15.
 */</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">class</span> <span class="hljs-title" style="color: rgb(102, 0, 102);">ItemBean</span> {</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> ItemImageResid;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> String ItemTitle;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> String ItemContent;

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-title">ItemBean</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> itemImageResid, String itemTitle, String itemContent) {
        ItemImageResid = itemImageResid;
        ItemTitle = itemTitle;
        ItemContent = itemContent;
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> <span class="hljs-title">getItemImageResid</span>() {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> ItemImageResid;
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">setItemImageResid</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">int</span> itemImageResid) {
        ItemImageResid = itemImageResid;
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> String <span class="hljs-title">getItemTitle</span>() {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> ItemTitle;
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">setItemTitle</span>(String itemTitle) {
        ItemTitle = itemTitle;
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> String <span class="hljs-title">getItemContent</span>() {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136);">return</span> ItemContent;
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">setItemContent</span>(String itemContent) {
        ItemContent = itemContent;
    }
}</code>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值