BaseAdapter是我們學(xué)習(xí)中入門就需要了解的適配器剖效,主要用于給ListView灼擂,GridView等其他列表內(nèi)容展示壁查。
今天我主要是提供一個(gè)自己封裝的BaseAdapter,并且感覺還挺實(shí)用的類給大家剔应,減少一些重復(fù)的工作量睡腿,直接貼代碼:
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;
}
/**
* 保存對(duì)象
*/
private class ViewHolder {
View view;
SparseArray<View> 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;
/**
* 獲取一個(gè)緩存的view
*
* @param id
* @param <T>
* @return
*/
public <T extends View> T obtainView(int id) {
return obtainView(viewHolder.view,id,0);
}
public <T extends View> 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;
}
/**
* 獲取一個(gè)參數(shù)值
*/
public <T extends Object> 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<Object> values = (List<Object>) 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();
}
/**
* 添加一個(gè)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其他元素的點(diǎn)擊事件
* @param mChildClickListener
*/
public void setChildClickListener(OnChildClickListener mChildClickListener) {
this.mChildClickListener = mChildClickListener;
}
public interface OnChildClickListener {
void onChildClick(View... view);
}
}
代碼也還算簡(jiǎn)介,支持List集合和JsonArray兩種類型峻贮,如果還有其他集合席怪,可根據(jù)需求自己更改,下面再介紹一下使用:
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);
//獲取一個(gè)list中一個(gè)對(duì)象
Simple mSimple = get(position);
}
}
很簡(jiǎn)單纤控,直接繼承BaseHomeAdapter即可挂捻。
總結(jié):很多時(shí)候?qū)懙拇a多了,你就會(huì)去總結(jié)怎么讓代碼更佳完美船万,開發(fā)效率更加高效刻撒,我就喜歡分享一些簡(jiǎn)單又實(shí)用的東西。