云儿随风 發表於 2020-3-12 09:13:00

Android开发进阶 -- 通用适配器 CommonAdapter

<p>&nbsp; &nbsp; &nbsp; 在Android开发中,我们经常会用到ListView 这个组件,为了将ListView 的内容展示出来,我们会去实现一个Adapter来适配,将Layout中的布局以列表的形式展现到组件中。</p>
<p> &nbsp; &nbsp;比如,像&nbsp;GGTalk 安卓版的查找用户功能,会把符合条件的用户都列在下面:</p>
<p>&nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/20404/201912/20404-20191226162118696-1805853744.png" alt="" width="317" height="196"></p>
<p>&nbsp; &nbsp; &nbsp; 为了达到这个效果,我们需要实现一个自定义的Adapter,而其它地方的ListView也要实现一个Adapter,这些Adapter会有很多重复的代码,非常繁琐,现在我就将重复代码封装到了一个通用的适配器CommonAdapter中,这样,在使用时只要继承CommonAdapter就可以了,如此就避免了大段代码的重复,让代码更简洁易懂。我们先来看看CommonAdapter的定义。  </p>
<h2>一.CommonAdapter 实现</h2>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">abstract</span> <span style="color: rgba(0, 0, 255, 1)">class</span> <span style="color: rgba(0, 128, 128, 1)">CommonAdapter</span>&lt;T&gt; <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 128, 128, 1)"> BaseAdapter</span> {

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 128, 128, 1)">List</span>&lt;T&gt;<span style="color: rgba(0, 0, 0, 1)"> dataList;
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 128, 128, 1)"> Context</span> context;
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">int</span> item_layoutId=0<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 128, 128, 1)">HashMap</span>&lt;Integer,Integer&gt; layoutIdMap; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">多种itemView时用到。 第一个int对应type类型,第二个int对应 itemlayoutId</span>

    <span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 设置单个子模版VIew,
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> context
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> datas 绑定的对象集合
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> item_layoutId 被绑定的视图layoutID
    * </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> CommonAdapter(Context context, List&lt;T&gt; datas, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> item_layoutId)
    {
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.context=<span style="color: rgba(0, 0, 0, 1)">context;
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.dataList=<span style="color: rgba(0, 0, 0, 1)">datas;
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.item_layoutId=<span style="color: rgba(0, 0, 0, 1)">item_layoutId;
    }

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   *设置多个子模版VIew, 并配合重写 getItemViewType(int position)来确定是设置哪个模版
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> context
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> datas 绑定的对象集合
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> layoutIdMap 多种itemViewID Map 第一个int对应type类型,第二个int对应 itemlayoutId
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> CommonAdapter(Context context, List&lt;T&gt; datas, HashMap&lt;Integer,Integer&gt;<span style="color: rgba(0, 0, 0, 1)"> layoutIdMap)
    {
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.context=<span style="color: rgba(0, 0, 0, 1)">context;
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.dataList=<span style="color: rgba(0, 0, 0, 1)">datas;
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.layoutIdMap=<span style="color: rgba(0, 0, 0, 1)">layoutIdMap;
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getViewTypeCount() {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">this</span>.layoutIdMap==<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> 1<span style="color: rgba(0, 0, 0, 1)">;
      }
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.layoutIdMap.size();
    }


    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getCount() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.dataList.size();
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> Object getItem(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> position) {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.dataList.get(position);
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">long</span> getItemId(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> position) {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> position;
    }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> View getView(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> position, View convertView, ViewGroup parent) {
      </span><span style="color: rgba(0, 0, 255, 1)">int</span> type =<span style="color: rgba(0, 0, 0, 1)"> getItemViewType(position);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> getConvertView(position,convertView,type);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> View getConvertView(<span style="color: rgba(0, 0, 255, 1)">int</span> position,View convertView,<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> itemViewType)
    {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(convertView==<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> layoutId =0<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">this</span>.item_layoutId!=0<span style="color: rgba(0, 0, 0, 1)">)
            {
                layoutId</span>=<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.item_layoutId;
            }
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.layoutIdMap != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                layoutId </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.layoutIdMap.get(itemViewType);
            }
            convertView </span>= LayoutInflater.from(context).inflate(layoutId, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">);
      }
      T t </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.dataList.get(position);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setConvertView(convertView,t,itemViewType);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> convertView;
    }

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 局部更新数据,调用一次getView()方法;Google推荐的做法
   *
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> parent要更新的listview
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> position 要更新的位置
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> onOneItemChanged(ListView parent, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> position) {
      </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">第一个可见的位置*</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
      <span style="color: rgba(0, 0, 255, 1)">int</span> firstVisiblePosition =<span style="color: rgba(0, 0, 0, 1)"> parent.getFirstVisiblePosition();
      </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">最后一个可见的位置*</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
      <span style="color: rgba(0, 0, 255, 1)">int</span> lastVisiblePosition =<span style="color: rgba(0, 0, 0, 1)"> parent.getLastVisiblePosition();

      </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">在看见范围内才更新,不可见的滑动后自动会调用getView方法更新*</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span> ((position &gt;= firstVisiblePosition &amp;&amp; position &lt;=<span style="color: rgba(0, 0, 0, 1)"> lastVisiblePosition)) {

            </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">获取指定位置view对象*</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
            View view </span>= parent.getChildAt(position -<span style="color: rgba(0, 0, 0, 1)"> firstVisiblePosition);
            getView(position, view, parent);
      }
    }

    </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
   * 需要去实现的对item中的view的设置操作
   *
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> convertView 转换后的试图
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> t Model对象
   * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> itemViewType 试图类型。对应layoutIdMap中的key
   </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">abstract</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setConvertView(View convertView, T t,<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> itemViewType);
}</span></pre>
</div>
<p>&nbsp;说明如下:</p>
<p>(1)泛型参数&lt;T&gt;就是我们要绑定数据Model对象的class类型。</p>
<p>(2)getViewTypeCount()方法会根据HashMap是否为空来识别是传入的单一模版还是多模版.</p>
<p>(3)CommonAdapter还重写了getCount(),getItem(int position),getView,getConvertView等BaseAdapter 的方法,这些方法的代码都基本每个Adapter都是一样的,我们只需关心&nbsp;<span style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(255, 0, 0, 1)">setConvertView</span> <span style="color: rgba(0, 0, 0, 1)">这个抽象方法,在子类中重写它,将具体的逻辑和数据的绑定在此即可。</span></span></p>
<h2><span style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 0, 1)">二.使用CommonAdapter</span></span></h2>
<ol>
<li>先建一个类继承CommonAdapter,在构造函数中调用父类的构造方法以初始化数据。实现 setConvertView 方法,将对象的数据绑定到模版上 即可
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> SearchFriendAdapter extends CommonAdapter&lt;OrayUser&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 设置单个子模版VIew,
   * @param context
   * @param datas 绑定的对象集合
   * @param item_layoutId 被绑定的视图layoutID
    * </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
   <span style="color: rgba(0, 0, 255, 1)">public</span> SearchFriendAdapter(Context context,List&lt;OrayUser&gt; datas,<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> item_layoutId)
   {
       super(context,datas,item_layoutId);
   }
  @Override
  </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setConvertView(View view, OrayUser orayUser,<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> itemViewType)
  {
    ...具体将对象的数据绑定到模版上
  }
}</span>&nbsp;</pre>
</div>
</li>
<li>Activity中使用时new 一个Adapter,然后将adapter绑定到 ListView
<div class="cnblogs_code">
<pre>adapter = <span style="color: rgba(0, 0, 255, 1)">new</span> SearchFriendAdapter(<span style="color: rgba(0, 0, 255, 1)">this</span>,<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.userList,R.layout.friend_child);
listView.setAdapter(adapter);</span></pre>
</div>
</li>
<li>更新数据源中的数据同步到View
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">有2种方式将数据源同步到View</span>
adapter.notifyDataSetChanged();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">全部刷新</span>
adapter.onOneItemChanged(ListView parent, <span style="color: rgba(0, 0, 255, 1)">int</span> position);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">刷新指定位置的数据</span>&nbsp;</pre>
</div>
</li>
</ol>
<h2>三. 让CommonAdapter支持多模板</h2>
<p>&nbsp; &nbsp; &nbsp; 当然CommonAdapter还支持多模版的应用,我们只需将第一步中的Adapter实现稍稍做改动即可</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> ListViewAdapter extends CommonAdapter&lt;ListViewItemModel&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   *设置多个子模版VIew, 并配合重写 getItemViewType(int position)来确定是设置哪个模版
   * @param context
   * @param datas 绑定的对象集合
   * @param layoutIdMap 多种itemViewID Map 第一个int对应type类型,第二个int对应 itemlayoutId
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> ListViewAdapter(Context context, List&lt;ListViewItemModel&gt; datas, HashMap&lt;Integer,Integer&gt;<span style="color: rgba(0, 0, 0, 1)"> layoutIdMap)
    {
      super(context,datas,layoutIdMap);
    }
    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> getItemViewType(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> position)
    {
      ListViewItemModel model</span>=<span style="color: rgba(0, 0, 0, 1)">(ListViewItemModel)super.getItem(position);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(model.floatRight)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;
      }
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
    }
    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setConvertView(View view, ListViewItemModel listViewItemModel,<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> itemViewType) {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">根据itemViewType 的值来识别是哪个模版,以对应给模版绑定数据</span><span style="color: rgba(0, 0, 0, 1)">     
    }
}</span></pre>
</div>
<p>如上图我们将构造函数的最后一个参数从单一的layoutID 换成了 模版集合的HashMap,再就是多加了&nbsp;getItemViewType(int position) 方法来返回 具体Model对应的是哪一个模版类型(即hashMap 中的key值),使用时更新数据源中的数据同步到View和上面的单一模版一样使用。(同上2种更新方法)</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">      HashMap</span>&lt;Integer,Integer&gt; layoutMap=<span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 128, 128, 1)">HashMap</span>&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
      layoutMap.put(</span>0,R.layout.listview_item);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">key值最好从0开始向上递增,否则可能会找不到key的BUG</span>
      layoutMap.put(1<span style="color: rgba(0, 0, 0, 1)">,R.layout.listview_right_item);
      adapter</span>=<span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 128, 128, 1)">ListViewAdapter</span>(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">,models,layoutMap);
      listView.setAdapter(adapter);</span></pre>
</div>
<h2>&nbsp;四.综合实例</h2>
<p>&nbsp; &nbsp; &nbsp; 最后我们还是以本文开头的查找用户的例子,来更全面地说明CommonAdapter的使用。页面截图如下:</p>
<p>&nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/20404/201912/20404-20191226162118696-1805853744.png" alt="" width="317" height="196"></p>
<h3><strong>&nbsp;1.</strong>模版布局</h3>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_item"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="4dp"&gt;

    &lt;ImageView
      android:id="@+id/ct_photo"
      android:layout_width="@dimen/headImageSize"
      android:layout_height="@dimen/headImageSize"
      android:layout_margin="5dp"
      android:src="@drawable/plus"
      /&gt;

    &lt;ImageView
      android:id="@+id/ct_status"
      android:layout_width="11dip"
      android:layout_height="11dip"
      android:layout_marginLeft="@dimen/headImageSize"
      android:layout_marginTop="@dimen/headImageSize"
      android:src='@drawable/status_2' /&gt;

    &lt;LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center_vertical"
      android:layout_alignTop="@id/ct_photo"
      android:layout_toRightOf="@id/ct_photo"
      &gt;

      &lt;TextView
            android:id="@+id/ct_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:paddingTop="0dp"
            android:text="name"
            android:textColor="@color/dimgrey"
            android:textSize="16sp"
            android:textStyle="bold" /&gt;

      &lt;TextView
            android:id="@+id/ct_describe"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/ct_name"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:paddingTop="0dp"
            android:text="describe"
            android:textColor="@color/dimgrey"
            android:textSize="14sp"
            android:visibility="gone" /&gt;
    &lt;/LinearLayout&gt;
    &lt;TextView
      android:id="@+id/ct_sign"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@id/ct_photo"
      android:layout_toRightOf="@id/ct_photo"
      android:paddingLeft="5dp"
      android:text="sign"
      android:singleLine="true"
      android:ellipsize="start"
      android:textColor="@color/dimgrey"
      android:textSize="12sp" /&gt;

&lt;/RelativeLayout&gt;
</pre>
</div>
<h3>2.具体继承CommonAdapter实现子类</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> SearchFriendAdapter <span style="color: rgba(0, 0, 255, 1)">extends</span> CommonAdapter&lt;OrayUser&gt;<span style="color: rgba(0, 0, 0, 1)"> {
   </span><span style="color: rgba(0, 0, 255, 1)">public</span> SearchFriendAdapter(Context context,List&lt;OrayUser&gt; datas,<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> item_layoutId)
   {
       </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(context,datas,item_layoutId);
   }

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setConvertView(View view, OrayUser orayUser,<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> itemViewType) {
      </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
            SearchFriendAdapter.ViewHolder holder;
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(view.getTag()==<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
            {
                holder </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SearchFriendAdapter.ViewHolder(view);
                view.setTag(holder);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                holder </span>=<span style="color: rgba(0, 0, 0, 1)"> (SearchFriendAdapter.ViewHolder) view.getTag();
            }
            HeadImgHelper.setUserHeadImg(holder.headImg,orayUser);
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(orayUser.getName().equals(orayUser.getCommentName()))
            {
                holder.userName.setText(orayUser.getName());
            }
            </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                holder.userName.setText(orayUser.getCommentName()</span>+"("+ orayUser.getName()+")"<span style="color: rgba(0, 0, 0, 1)">);
            }
            String catalogName</span>=<span style="color: rgba(0, 0, 0, 1)"> ClientGlobalCache.getInstance().getCurrentUser().getCatalogNameByFriend(orayUser.getUserID());
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 0, 1)">StringHelper.isNullOrEmpty(catalogName))
            {
                holder.describe.setText(</span>"[ "+ catalogName+" ]"<span style="color: rgba(0, 0, 0, 1)">);
                holder.describe.setVisibility(View.VISIBLE);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
            {
                holder.describe.setText(</span>""<span style="color: rgba(0, 0, 0, 1)">);
                holder.describe.setVisibility(View.GONE);
            }
            holder.orgName.setText(orayUser.getUserID());
      }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception ex){ex.printStackTrace();}
    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ViewHolder
    {
      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> ImageView headImg;
      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> TextView userName;
      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> TextView describe;
      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> TextView orgName;

      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> ViewHolder(View view)
      {
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.headImg =<span style="color: rgba(0, 0, 0, 1)"> (ImageView) view.findViewById(R.id.ct_photo);
            ImageView statusImg</span>=<span style="color: rgba(0, 0, 0, 1)">(ImageView) view.findViewById(R.id.ct_status);
            statusImg.setVisibility(View.GONE);
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.userName=<span style="color: rgba(0, 0, 0, 1)">(TextView) view.findViewById(R.id.ct_name);
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.orgName=<span style="color: rgba(0, 0, 0, 1)">(TextView) view.findViewById(R.id.ct_sign);
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.describe=<span style="color: rgba(0, 0, 0, 1)">(TextView) view.findViewById(R.id.ct_describe);
      }
    }
}</span></pre>
</div>
<h3>3. Activity类 核心代码</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 128, 128, 1)"> SearchFriendActivity</span> extends Activity implements TextView.<span style="color: rgba(0, 128, 128, 1)">OnEditorActionListener</span>{
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 128, 128, 1)"> DrawableEditText</span> input;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 128, 128, 1)"> ListView</span> listView;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 128, 128, 1)"> TextView</span> search_resultStr;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 128, 128, 1)">List</span>&lt;OrayUser&gt; userList=<span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 128, 128, 1)">ArrayList</span>&lt;OrayUser&gt;<span style="color: rgba(0, 0, 0, 1)">() ;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 128, 128, 1)"> SearchFriendAdapter</span> adapter;

    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(<span style="color: rgba(0, 128, 128, 1)">Bundle</span> savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_search_friend);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.initView();
    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> initView()
    {
      TextView pageTitle</span>=<span style="color: rgba(0, 0, 0, 1)">(TextView)findViewById(R.id.headerText);
      pageTitle.setText(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">查找用户</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
      search_resultStr </span>=<span style="color: rgba(0, 0, 0, 1)">(TextView) findViewById(R.id.search_resultStr);
<span style="color: rgba(255, 102, 0, 1)">      listView</span></span><span style="color: rgba(255, 102, 0, 1)">=(ListView)findViewById(R.id.listview_user);
      listView.setOnItemClickListener(this);
      adapter = new SearchFriendAdapter(this,this</span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(255, 102, 0, 1)">.userList,R.layout.friend_child);
      listView.setAdapter(adapter);</span>       </span><span style="color: rgba(0, 0, 0, 1)">

    }   </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
   * 执行点击搜索指令
   * </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> action_search(String idOrName)
    {
      List</span>&lt;OrayUser&gt; temp_users=<span style="color: rgba(0, 0, 0, 1)">Manager.getInstance().doSearchUser(idOrName);
      search_resultStr.setText(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">没有找到符合条件的用户或群组</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setSearchResult(temp_users);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setSearchResult(<span style="color: rgba(0, 128, 128, 1)">List</span>&lt;OrayUser&gt;<span style="color: rgba(0, 0, 0, 1)"> temp_users)
    {
      </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.userList.clear();
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(temp_users==<span style="color: rgba(0, 0, 255, 1)">null</span>||temp_users.size()==<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">)
      {
            search_resultStr.setVisibility(View.VISIBLE);
      }
      </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
      {
            </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (OrayUser orayUser :temp_users) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(orayUser.getUserID().equals(ClientGlobalCache.getInstance().getCurrentUser().getUserID()))
                {
                  temp_users.remove(orayUser);
                  </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
                }
            }
            </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.userList.addAll(temp_users) ;
            search_resultStr.setVisibility(View.GONE);
      }
      </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.<span style="color: rgba(255, 102, 0, 1)">adapter.notifyDataSetChanged()</span>;
    }</span><span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/justnow/p/12096752.html
頁: [1]
查看完整版本: Android开发进阶 -- 通用适配器 CommonAdapter