Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android超級common的Adapter,也就是傳說的CommonAdapter

Android超級common的Adapter,也就是傳說的CommonAdapter

編輯:關於Android編程

有了這個CommonAdapter以後的Adapter就so easy 了。

1. 定義CommonAdapter

 

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 
 */
public abstract class CommonAdapter extends BaseAdapter {

    protected Context mContext;
    protected List mDatas;
    protected LayoutInflater mInflater;
    protected int layoutId;

    public CommonAdapter(Context context, List 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 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 mViews;
    private int mPostion;
    private View mConvertView;

    public ViewHolder(Context context, ViewGroup parent, int layoutId, int postion) {
        this.mPostion = postion;
        this.mViews = new SparseArray();
        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 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 {

    public ShowFishingAdapter(Context context, List 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);
    }

}

當然沒有最好,只有更好,還有很多可以改進的地方喲!

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved