Android BaseAdapter

yangxi_001發表於2017-08-02
public abstract class BaseAdapter<T> extends android.widget.BaseAdapter {

    private List<T> mDataList;

    public BaseAdapter(List<T> list) {
        mDataList = list;
    }

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

    @Override
    public T getItem(int position) {
        return position < mDataList.size() ? mDataList.get(position) : null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = ViewHolder.getInstance(convertView, parent, getItemLayoutId(position));
        T item = getItem(position);
        convert(viewHolder, item, position, parent);
        return viewHolder.getConvertView();
    }

    public List<T> getDataList() {
        return mDataList;
    }

    public abstract @LayoutRes int getItemLayoutId(int position);

    public abstract void convert(ViewHolder holder, T item, int position, ViewGroup parent);

    /**
     * 重置資料
     * @param data
     */
    public void resetData(List<T> data) {
        if (data == null) {
            return;
        }
        mDataList = data;
        super.notifyDataSetChanged();
    }

}

public class ViewHolder {

    private View mConvertView;
    private SparseArray<View> mViews;

    private ViewHolder(View convertView) {
        mViews = new SparseArray<>();
        this.mConvertView = convertView;
        convertView.setTag(this);
    }

    /**
     * 獲取ViewHolder 例項物件
     *
     * @param convertView
     * @param parent
     * @param layoutId
     * @return
     */
    public static ViewHolder getInstance(View convertView, ViewGroup parent, int layoutId) {
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
            return new ViewHolder(convertView);
        }
        return (ViewHolder) convertView.getTag();
    }

    public View getConvertView() {
        return mConvertView;
    }

    public <T extends View> T getView(int viewId) {
        return retrieveView(viewId);
    }

    /**
     * 設定控制元件是否可見
     *
     * @param viewId
     * @param isVisible true 或者 false, 如設為false 則控制元件狀態為 GONE
     * @return
     */
    public ViewHolder setVisible(int viewId, boolean isVisible) {
        return setVisibility(viewId, isVisible ? View.VISIBLE : View.GONE);
    }

    public ViewHolder setVisibility(int viewId, int visibility) {
        View view = getView(viewId);
        view.setVisibility(visibility);
        return this;
    }

    public ViewHolder setBackgroundColor(int viewId, int color) {
        View view = getView(viewId);
        view.setBackgroundColor(color);
        return this;
    }

    public ViewHolder setBackgroundRes(int viewId, int resid) {
        View view = getView(viewId);
        view.setBackgroundResource(resid);
        return this;
    }

    /**
     * 設定透明度
     *
     * @param viewId
     * @param alpha  0 ~ 10 完全透明
     * @return
     */
    public ViewHolder setAlpha(int viewId, int alpha) {
        View view = getView(viewId);
        view.setAlpha(alpha);
        return this;
    }

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

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

    public ViewHolder setText(int viewId, CharSequence text, int color, float size) {
        TextView tv = getView(viewId);
        tv.setText(text);
        tv.setTextColor(color);
        tv.setTextSize(size);
        return this;
    }

    @SuppressWarnings("deprecation")
    public ViewHolder setTextColorRes(int viewId, int resId) {
        return setTextColor(viewId, mConvertView.getContext().getResources().getColor(resId));
    }

    public ViewHolder setTextColor(int viewId, int color) {
        TextView tv = getView(viewId);
        tv.setTextColor(color);
        return this;
    }

    public ViewHolder setTextSize(int viewId, float size) {
        TextView tv = getView(viewId);
        tv.setTextSize(size);
        return this;
    }

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

    public ViewHolder setImage(int viewId, Drawable drawable) {
        ImageView imageView = getView(viewId);
        imageView.setImageDrawable(drawable);
        return this;
    }

    public ViewHolder setImage(int viewId, Bitmap bmp) {
        ImageView imageView = getView(viewId);
        imageView.setImageBitmap(bmp);
        return this;
    }

    public ViewHolder setTypeface(int viewId, Typeface typeface) {
        TextView view = retrieveView(viewId);
        view.setTypeface(typeface);
        view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        return this;
    }

    public ViewHolder setTag(int viewId, Object tag) {
        View view = retrieveView(viewId);
        view.setTag(tag);
        return this;
    }

    public ViewHolder setTag(int viewId, int key, Object tag) {
        View view = retrieveView(viewId);
        view.setTag(key, tag);
        return this;
    }

    public ViewHolder setChecked(int viewId, boolean checked) {
        Checkable view = (Checkable) retrieveView(viewId);
        view.setChecked(checked);
        return this;
    }

    public ViewHolder setSelected(int viewId, boolean selected) {
        View view = retrieveView(viewId);
        view.setSelected(selected);
        return this;
    }

    @SuppressWarnings("unchecked")
    private <T extends View> T retrieveView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }
}

RecycleView版本 RecycleViewHolder

public class RecycleViewHolder extends RecyclerView.ViewHolder {

    private View mRootView;
    private SparseArray<View> mViews;

    public RecycleViewHolder(View view) {
        super(view);
        mViews = new SparseArray<>();
        this.mRootView = view;
    }


    public View getRootView(){
        return  mRootView;
    }

    public <T extends View> T getView(int viewId) {
        return retrieveView(viewId);
    }

    public ImageView getImageView(int viewId){
        return getView(viewId);
    }

    /**
     * 設定控制元件是否可見
     *
     * @param viewId
     * @param isVisible true 或者 false, 如設為false 則控制元件狀態為 GONE
     * @return
     */
    public RecycleViewHolder setVisible(int viewId, boolean isVisible) {
        return setVisibility(viewId, isVisible ? View.VISIBLE : View.GONE);
    }

    public RecycleViewHolder setVisibility(int viewId, int visibility) {
        View view = getView(viewId);
        view.setVisibility(visibility);
        return this;
    }

    public RecycleViewHolder setBackgroundColor(int viewId, int color) {
        View view = getView(viewId);
        view.setBackgroundColor(color);
        return this;
    }

    public RecycleViewHolder setBackgroundRes(int viewId, int resid) {
        View view = getView(viewId);
        view.setBackgroundResource(resid);
        return this;
    }

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

    public RecycleViewHolder setText(int viewId, CharSequence text, int color) {
        TextView tv = getView(viewId);
        tv.setText(text);
        tv.setTextColor(color);
        return this;
    }

    public RecycleViewHolder setText(int viewId, CharSequence text, int color, float size) {
        TextView tv = getView(viewId);
        tv.setText(text);
        tv.setTextColor(color);
        tv.setTextSize(size);
        return this;
    }


    public RecycleViewHolder setTextColor(int viewId, int color) {
        TextView tv = getView(viewId);
        tv.setTextColor(color);
        return this;
    }

    public RecycleViewHolder setTextSize(int viewId, float size) {
        TextView tv = getView(viewId);
        tv.setTextSize(size);
        return this;
    }

    public RecycleViewHolder setImage(int viewId, int resId) {
        ImageView imageView = getView(viewId);
        imageView.setImageResource(resId);
        return this;
    }

    public RecycleViewHolder setImage(int viewId, Drawable drawable) {
        ImageView imageView = getView(viewId);
        imageView.setImageDrawable(drawable);
        return this;
    }

    public RecycleViewHolder setImage(int viewId, Bitmap bmp) {
        ImageView imageView = getView(viewId);
        imageView.setImageBitmap(bmp);
        return this;
    }

    public RecycleViewHolder setTypeface(int viewId, Typeface typeface) {
        TextView view = retrieveView(viewId);
        view.setTypeface(typeface);
        view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        return this;
    }

    public RecycleViewHolder setTag(int viewId, Object tag) {
        View view = retrieveView(viewId);
        view.setTag(tag);
        return this;
    }

    public RecycleViewHolder setTag(int viewId, int key, Object tag) {
        View view = retrieveView(viewId);
        view.setTag(key, tag);
        return this;
    }

    public RecycleViewHolder setChecked(int viewId, boolean checked) {
        Checkable view = (Checkable) retrieveView(viewId);
        view.setChecked(checked);
        return this;
    }

    public RecycleViewHolder setSelected(int viewId, boolean selected) {
        View view = retrieveView(viewId);
        view.setSelected(selected);
        return this;
    }

    private <T extends View> T retrieveView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mRootView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }
}

相關文章