1. 程式人生 > >android學習筆記 通用介面卡工具類和ViewHolder工具類

android學習筆記 通用介面卡工具類和ViewHolder工具類

<pre class="java" name="code">/**
 * 通用介面卡工具類
 * Created by bobok on 2016/9/5.
 */
public abstract class CommonAdapter<T> extends BaseAdapter{

    //protected讓子類可以訪問
    protected LayoutInflater mInflater;
    protected Context mContext;
    protected List<T> mDatas;
    private int layoutId;

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

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

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

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        //例項化一個ViewHolder物件
        ViewHolder viewHolder = ViewHolder.get(mContext,view,viewGroup,layoutId,i);
        //把viewHolder和當前Item對於的Bean物件給傳出去
        convert(viewHolder,getItem(i));
        return viewHolder.getConvertView();
    }

    /**
     * 對外公佈了一個convert方法,把viewHolder和當前Item對於的Bean物件給傳出去
     * @param viewHolder
     * @param t
     */
    public abstract void convert(ViewHolder viewHolder,T t);
}

/**
 * Created by xhb on 2016/2/20.
 * 通用的ViewHolder工具類
 */
public class ViewHolder {
    //使用SparseArray是因為當鍵值對對映是    Integer--Object 時 SparseArray的效率要比Map高
    private SparseArray<View> mViews;
    private View mConvertView;
    private int mPosition;
    private Context mContext;
    private ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
        this.mPosition = position;
        mContext=context;
        this.mViews = new SparseArray<>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
        //設定tag
        mConvertView.setTag(this);
    }

    /**
     * 拿到一個ViewHolder物件
     *
     * @param context     上下文
     * @param convertView 快取佈局
     * @param parent      父控制元件
     * @param layoutId    佈局id
     * @param position    位置
     * @return
     */
    public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
        if (convertView == null) {
            return new ViewHolder(context, parent, layoutId, position);
        }else {
            ViewHolder holder = (ViewHolder) convertView.getTag();
            //更新position
            holder.mPosition=position;
            return holder;
        }
    }

    /**
     * 通過控制元件的viewId獲取對應的控制元件,如果沒有則加入views
     *
     * @param viewId
     * @param <T>
     * @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;
    }

    /**
     * 給TextView設定值
     * @param viewId 控制元件id
     * @param text   值
     * @return
     */
    public ViewHolder setText(int viewId,String text){
        TextView tv =getView(viewId);
        tv.setText(text);
        return this;
    }

    /**
     * 給TextView設定字型顏色
     * @param viewId
     * @param textColor
     * @return
     */
    public ViewHolder setTextColor(int viewId, int textColor)
    {
        TextView view = getView(viewId);
        view.setTextColor(textColor);
        return this;
    }

    /**
     * 給TextView設定顏色
     * @param viewId
     * @param textColorRes  顏色id
     * @return
     */
    public ViewHolder setTextColorRes(int viewId, int textColorRes)
    {
        TextView view = getView(viewId);
        view.setTextColor(mContext.getResources().getColor(textColorRes));
        return this;
    }

    /**
     * 給TextView設定連結
     * @param viewId
     * @return
     */
    public ViewHolder linkify(int viewId)
    {
        TextView view = getView(viewId);
        Linkify.addLinks(view, Linkify.ALL);
        return this;
    }

    /**
     * 給ImageView設定ImageResource
     * @param viewId  控制元件id
     * @param resId   資源id
     * @return
     */
    public ViewHolder setImageResource(int viewId,int resId){
        ImageView iv= getView(viewId);
        iv.setImageResource(resId);
        return this;
    }
    /**
     * 給ImageView設定ImageResource使用Frsesco
     * @param uri   資源uri
     * @return
     */
    public ViewHolder setImageResourcewithFresco(int viewId,Uri uri){
        SimpleDraweeView draweeView=getView(viewId);
        draweeView.setImageURI(uri);
        return this;
    }

    /**
     * 給ImageView設定bitmap
     * @param viewId
     * @param bitmap
     * @return
     */
    public ViewHolder setImageBitmap(int viewId,Bitmap bitmap){
        ImageView iv= getView(viewId);
        iv.setImageBitmap(bitmap);
        return this;
    }

    /**
     * 載入網路圖片使用xutils
     * @param viewId
     * @param uri
     * @return
     */
    public ViewHolder setImageURIWithXutils(int viewId,String uri){
        ImageView iv= getView(viewId);
        x.image().bind(iv,uri);
        return this;
    }

    /**
     * 給ImageView設定drawable
     * @param viewId
     * @param drawable
     * @return
     */
    public ViewHolder setImageDrawable(int viewId, Drawable drawable)
    {
        ImageView view = getView(viewId);
        view.setImageDrawable(drawable);
        return this;
    }

    /**
     * 給控制元件設定背景顏色
     * @param viewId
     * @param color
     * @return
     */
    public ViewHolder setBackgroundColor(int viewId, int color)
    {
        View view = getView(viewId);
        view.setBackgroundColor(color);
        return this;
    }

    /**
     * 給控制元件設定背景圖片
     * @param viewId
     * @param backgroundRes
     * @return
     */
    public ViewHolder setBackgroundRes(int viewId, int backgroundRes)
    {
        View view = getView(viewId);
        view.setBackgroundResource(backgroundRes);
        return this;
    }

    /**
     * 設定透明度
     * @param viewId
     * @param value
     * @return
     */
    @SuppressLint("NewApi")
    public ViewHolder setAlpha(int viewId, float value)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        {
            getView(viewId).setAlpha(value);
        } else
        {
            // Pre-honeycomb hack to set Alpha value
            AlphaAnimation alpha = new AlphaAnimation(value, value);
            alpha.setDuration(0);
            alpha.setFillAfter(true);
            getView(viewId).startAnimation(alpha);
        }
        return this;
    }

    /**
     * 設定控制元件是否可見
     * @param viewId
     * @param visible
     * @return
     */
    public ViewHolder setVisible(int viewId, boolean visible)
    {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }


    /**
     *設定字型型別
     * @param typeface
     * @param viewIds
     * @return
     */
    public ViewHolder setTypeface(Typeface typeface, int... viewIds)
    {
        for (int viewId : viewIds)
        {
            TextView view = getView(viewId);
            view.setTypeface(typeface);
            view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        }
        return this;
    }

    /**
     *點選事件
     */
    public ViewHolder setOnClickListener(int viewId,
                                         View.OnClickListener listener)
    {
        View view = getView(viewId);
        view.setOnClickListener(listener);
        return this;
    }

    /**
     * 設定進度條
     * @param viewId
     * @param progress
     * @return
     */
    public ViewHolder setProgress(int viewId, int progress)
    {
        ProgressBar view = getView(viewId);
        view.setProgress(progress);
        return this;
    }

    /**
     * 設定進度條
     * @param viewId
     * @param progress
     * @param max
     * @return
     */
    public ViewHolder setProgress(int viewId, int progress, int max)
    {
        ProgressBar view = getView(viewId);
        view.setMax(max);
        view.setProgress(progress);
        return this;
    }

    public ViewHolder setMax(int viewId, int max)
    {
        ProgressBar view = getView(viewId);
        view.setMax(max);
        return this;
    }

    public ViewHolder setRating(int viewId, float rating)
    {
        RatingBar view = getView(viewId);
        view.setRating(rating);
        return this;
    }

    public ViewHolder setRating(int viewId, float rating, int max)
    {
        RatingBar view = getView(viewId);
        view.setMax(max);
        view.setRating(rating);
        return this;
    }

    /**
     * 設定標籤
     * @param viewId
     * @param tag
     * @return
     */
    public ViewHolder setTag(int viewId, Object tag)
    {
        View view = getView(viewId);
        view.setTag(tag);
        return this;
    }

    /**
     * 設定標籤
     * @param viewId
     * @param key
     * @param tag
     * @return
     */
    public ViewHolder setTag(int viewId, int key, Object tag)
    {
        View view = getView(viewId);
        view.setTag(key, tag);
        return this;
    }


    /**
     * 設定是否選中
     * @param viewId
     * @param checked
     * @return
     */
    public ViewHolder setChecked(int viewId, boolean checked)
    {
        Checkable view = (Checkable) getView(viewId);
        view.setChecked(checked);
        return this;
    }



    /**
     * 觸控事件
     * @param viewId
     * @param listener
     * @return
     */
    public ViewHolder setOnTouchListener(int viewId,
                                         View.OnTouchListener listener)
    {
        View view = getView(viewId);
        view.setOnTouchListener(listener);
        return this;
    }

    /**
     * 長按事件
     * @param viewId
     * @param listener
     * @return
     */
    public ViewHolder setOnLongClickListener(int viewId,
                                             View.OnLongClickListener listener)
    {
        View view = getView(viewId);
        view.setOnLongClickListener(listener);
        return this;
    }

}