适配器BaseAdapter的封装,提高开发效率

BaseAdapter是我们学习中入门就需要了解的适配器,主要用于给ListView,GridView等其他列表内容展示。
今天我主要是提供一个自己封装的BaseAdapter,并且感觉还挺实用的类給大家,减少一些重复的工作量,直接贴代码:

package com.tangyx.home.factory;

import android.content.Context;
import android.os.Build;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import org.json.JSONArray;
import org.json.JSONException;

import java.lang.reflect.Field;
import java.util.List;


/**
 * Created by tangyx on 15/9/21.
 *
 */
public abstract class BaseHomeAdapter extends BaseAdapter {
    public OnChildClickListener mChildClickListener;
    public JSONArray mArrays;
    public List mList;
    public Context mContext;
    private ViewHolder viewHolder;

    public BaseHomeAdapter(Context context) {
        this.mContext = context;
    }

    public BaseHomeAdapter(Context context, JSONArray arrays) {
        this(context);
        this.mArrays = arrays;
    }

    public BaseHomeAdapter(Context context, List list) {
        this(context);
        this.mList = list;
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        if (mArrays != null) {
            return mArrays.length();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        try {
            if (mList != null) {
                return mList.get(position);
            }
            return mArrays.get(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            if (convertView == null) {
                LayoutInflater inflater = LayoutInflater.from(mContext);
                convertView = onBindView(inflater);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            onBindData(position, convertView, parent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    /**
     * 保存对象
     */
    private class ViewHolder {
        View view;
        SparseArray holderArray;

        ViewHolder(View view) {
            this.view = view;
            holderArray = new SparseArray<>();
        }
    }

    /**
     * 绑定item布局
     * @param inflater
     * @return
     * @throws Exception
     */
    public abstract View onBindView(LayoutInflater inflater) throws Exception;

    /**
     * 获取item布局中的控件
     * @param position
     * @param convertView
     * @param parent
     * @throws Exception
     */
    public abstract void onBindData(int position, View convertView, ViewGroup parent) throws Exception;

    /**
     * 获取一个缓存的view
     *
     * @param id
     * @param 
     * @return
     */
    public  T obtainView(int id) {
        return obtainView(viewHolder.view,id,0);
    }
    public  T obtainView(View layout,int id,int position) {
        View view = viewHolder.holderArray.get(layout.getId()+id+position);
        if (null != view) {
            return (T) view;
        }
        view =layout.findViewById(id);
        if (null == view) {
            return null;
        }
        viewHolder.holderArray.put(layout.getId()+id+position, view);
        return (T) view;
    }
    /**
     * 获取一个参数值
     */
    public  T get(int index) {
        Object val;
        if (mArrays != null) {
            try {
                val = mArrays.get(index);
                return (T) val;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        if (mList != null) {
            val = mList.get(index);
            return (T) val;
        }
        return null;
    }

    /**
     * 移除元素
     */
    public Object remove(int index) {
        if (mList != null) {
            return mList.remove(index);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            return mArrays.remove(index);
        }
        Field valuesField;
        try {
            valuesField = JSONArray.class.getDeclaredField("values");
            valuesField.setAccessible(true);
            List values = (List) valuesField.get(mArrays);
            if (index < values.size()) {
                return values.remove(index);
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 移除所有元素
     */
    public void removeAll() {
        if (mArrays != null) {
            mArrays = new JSONArray();
        }
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    /**
     * 添加一个item
     * @param object
     */
    public void addItem(Object object) {
        if (mArrays != null) {
            mArrays.put(object);
        }
        if (mList != null) {
            mList.add(object);
        }
    }

    /**
     * 替换所有的元素
     * @param jsonArray
     */
    public void replaceAll(JSONArray jsonArray) {
        this.mArrays = jsonArray;
        this.mList = null;
        notifyDataSetChanged();
    }

    /**
     * 替换所有的元素
     * @param list
     */
    public void replaceAll(List list) {
        this.mList = list;
        this.mArrays = null;
        notifyDataSetChanged();
    }

    /**
     * item其他元素的点击事件
     * @param mChildClickListener
     */
    public void setChildClickListener(OnChildClickListener mChildClickListener) {
        this.mChildClickListener = mChildClickListener;
    }

    public interface OnChildClickListener {
        void onChildClick(View... view);
    }

}
 
 

代码也还算简介,支持List集合和JsonArray两种类型,如果还有其他集合,可根据需求自己更改,下面再介绍一下使用:

package com.tangyx.home.factory;

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

import com.tangyx.home.demo.R;

import java.util.List;

/**
* Created by tangyx on 2016/12/16.
*
*/

public class SimpleAdapter extends BaseHomeAdapter {

   public SimpleAdapter(Context context, List list) {
       super(context, list);
   }

   @Override
   public View onBindView(LayoutInflater inflater) throws Exception {
       //替换成你自己的item布局
       return inflater.inflate(R.layout.adapter_item,null);
   }

   @Override
   public void onBindData(int position, View convertView, ViewGroup parent) throws Exception {
       //获取item布局的控件
       TextView mName = obtainView(R.id.name);
       //获取一个list中一个对象
       Simple mSimple = get(position);
   }
}

很简单,直接继承BaseHomeAdapter即可。
总结:很多时候写的代码多了,你就会去总结怎么让代码更佳完美,开发效率更加高效,我就喜欢分享一些简单又实用的东西。

你可能感兴趣的:(适配器BaseAdapter的封装,提高开发效率)