关闭

万能适配器 CommonAdapter

298人阅读 评论(0) 收藏 举报
分类:
package com.stephen.ifishing.adapter;

import java.util.List;

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

/**
 * 
 * @author Stephen Huang
 *
 * @param <T>
 */
public abstract class CommonAdapter<T> extends BaseAdapter {

    protected Context mContext;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;
    protected int layoutId;

    public CommonAdapter(Context context, List<T> data, int layoutId) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        this.mDatas = data;
        this.layoutId = layoutId;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

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

    public void onDataChange(List<T> data) {
        this.mDatas = data;
        this.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = ViewHolder.get(mContext, convertView, parent, layoutId, position);
        convert(holder, mDatas.get(position));
        return holder.getConvertView();
    }

    public abstract void convert(ViewHolder holder, T t);

}
 2. 辅助类ViewHolder
package com.stephen.ifishing.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * @author Stephen Huang
 *
 */
public class ViewHolder {

    private SparseArray<View> mViews;
    private int mPostion;
    private View mConvertView;

    public ViewHolder(Context context, ViewGroup parent, int layoutId, int postion) {
        this.mPostion = postion;
        this.mViews = new SparseArray<View>();
        this.mConvertView = LayoutInflater.from(context).inflate(layoutId, null);
        mConvertView.setTag(this);
    }

    public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int postion) {
        if (convertView == null) {
            return new ViewHolder(context, parent, layoutId, postion);
        } else {
            ViewHolder holder = (ViewHolder)convertView.getTag();
            holder.mPostion = postion;
            return holder;
        }
    }

    /**
     * 通过viewId获取控件
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView() {
        return mConvertView;
    }

    public ViewHolder setText(int viewId, String text) {
        TextView tv = getView(viewId);
        tv.setText(text);
        return this;
    }

    public ViewHolder setImageResource(int viewId, int resId) {
        ImageView iv = getView(viewId);
        iv.setImageResource(resId);
        return this;
    }

    public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
        ImageView iv = getView(viewId);
        iv.setImageBitmap(bm);
        return this;
    }
}

3. 如何使用我们的CommonAdapter?
package com.stephen.ifishing.adapter;

import java.util.List;

import com.stephen.ifishing.R;
import com.stephen.ifishing.model.ShowFishingItem;

import android.content.Context;

/**
 * 
 * @author Stephen Huang
 *
 */
public class ShowFishingAdapter extends CommonAdapter<ShowFishingItem> {

    public ShowFishingAdapter(Context context, List<ShowFishingItem> itemList) {
        super(context, itemList, R.layout.show_fishing);
    }

    @Override
    public void convert(ViewHolder holder, ShowFishingItem bean) {
        holder.setText(R.id.tv_user_name, bean.userName)
            .setText(R.id.tv_say_something, bean.saySomething)
            .setImageResource(R.id.iv_portrait, bean.portraitImageResId);
    }

}
0
0
查看评论

万能适配器CommonAdapter和ViewHolder

对于我们Android程序猿来说,listview算是最常见的控件之一了,当然listview是要和adapter配套使用的,下面我们就来看看常规的adapter操作  @Override public View getView(int position, View convertVi...
  • wrm0013
  • wrm0013
  • 2015-08-13 17:18
  • 663

万能适配器(2) 抽取CommonAdapter

万能适配器(2) 抽取CommonAdapter
  • wuxintdrh
  • wuxintdrh
  • 2016-07-19 13:07
  • 188

ListView万能适配器CommonAdapter

CommonAdapter: /* * @Copyright (c) tianchenglin * @Author TCL * @Date 2016.7.5 */ package com.study.tcl.commonadapter; import android.content...
  • u012246348
  • u012246348
  • 2016-07-05 14:26
  • 606

安卓万能适配器-CommonAdapter

依赖:recyclerview+glide图片加载器 compile 'com.android.support:recyclerview-v7:25.3.1' compile 'com.github.bumptech.glide:glide:4.0.0' ...
  • qq_34536167
  • qq_34536167
  • 4天前 22:42
  • 22

Java泛型应用之打造Android中ListView和GridView万能适配器【CommonAdapter】--超简洁写法

在android中使用最多的就是ListView,GridView,用到这两个控件那么肯定要用到适配器,那就是定义一个类继承BaseAdapter,让后覆写它里面的getCount(),getView()等方法,下面来写一个demo。
  • u010785585
  • u010785585
  • 2016-10-16 11:25
  • 3244

Android万能适配器CommonAdapter的源码分析

1.ViewFinder的实现 package com.mycollege.util; import java.lang.ref.WeakReference; import android.content.Context; import android.util.Log; import and...
  • pcaxb
  • pcaxb
  • 2015-08-23 13:55
  • 3421

支持多布局的CommonAdapter

支持多布局的CommonAdapter以前总是打算要写一些博客,一方面是为了记录自己在Android开发中的心得,另一方面也跟大家分享一下,但总是觉得水平不够,总是拖着,心想在等等,后来看了看任玉刚大神的博客android学习路线:如何成长为高级工程师,觉得大神说的很有道理,“时不时把自己的知识汇总...
  • qq_25652907
  • qq_25652907
  • 2016-05-20 18:10
  • 796

Android超级common的Adapter,也就是传说的CommonAdapter

有了这个CommonAdapter以后的Adapter就so easy 了。 1. 定义CommonAdapter package com.stephen.ifishing.adapter; import java.util.List; import android.content.Cont...
  • u010571844
  • u010571844
  • 2015-07-30 22:28
  • 3498

CommonAdapter

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/44014941,本文出自:【张鸿洋的博客】 1. 功能介绍 1.1. base-adapter-helper base-adapter-helper 是对我们传...
  • lyndsc
  • lyndsc
  • 2016-02-17 12:15
  • 1802

PowerfulRecyclerViewAdapter:一种RecyclerView的万能适配器实现

PowerfulRecyclerAdapter支持各种ViewHolder类型的RecyclerView.Adapter的实现,是一个万能适配器 项目地址:https://github.com/simplify20/PowerfulRecyclerViewAdapter 特性: 使用DataBean...
  • u012825445
  • u012825445
  • 2016-04-10 15:09
  • 1082
    个人资料
    • 访问:152360次
    • 积分:2138
    • 等级:
    • 排名:千里之外
    • 原创:60篇
    • 转载:128篇
    • 译文:1篇
    • 评论:15条
    最新评论