c19130cacb89e66cac09830f9106ca262fced896.svn-base 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package com.hjq.base;
  2. import android.content.Context;
  3. import android.content.res.Resources;
  4. import android.graphics.drawable.Drawable;
  5. import android.os.Build;
  6. import android.support.annotation.ColorRes;
  7. import android.support.annotation.DrawableRes;
  8. import android.support.annotation.IdRes;
  9. import android.support.annotation.NonNull;
  10. import android.support.annotation.Nullable;
  11. import android.support.annotation.StringRes;
  12. import android.util.SparseArray;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.BaseAdapter;
  17. import java.lang.ref.WeakReference;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. /**
  21. * desc : ListView 适配器基类
  22. */
  23. public abstract class BaseListViewAdapter
  24. <T, VH extends BaseListViewAdapter.ViewHolder>
  25. extends BaseAdapter {
  26. // 列表数据
  27. private List<T> mDataSet;
  28. // 上下文对象
  29. private Context mContext;
  30. public BaseListViewAdapter(Context context) {
  31. mContext = context;
  32. }
  33. @Override
  34. public int getCount() {
  35. return getItemCount();
  36. }
  37. public int getItemCount() {
  38. return mDataSet == null ? 0 : mDataSet.size();
  39. }
  40. @Override
  41. public long getItemId(int position) {
  42. return position;
  43. }
  44. @Override
  45. public View getView(int position, View convertView, ViewGroup parent) {
  46. final VH holder;
  47. if (convertView == null) {
  48. holder = onCreateViewHolder(parent, getItemViewType(position));
  49. holder.getItemView().setTag(holder);
  50. } else {
  51. holder = (VH) convertView.getTag();
  52. }
  53. onBindViewHolder(holder, position);
  54. return holder.getItemView();
  55. }
  56. @NonNull
  57. public abstract VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType);
  58. public abstract void onBindViewHolder(@NonNull VH holder, int position);
  59. /**
  60. * 设置新的数据
  61. */
  62. public void setData(List<T> data) {
  63. mDataSet = data;
  64. notifyDataSetChanged();
  65. }
  66. /**
  67. * 获取当前数据
  68. */
  69. @Nullable
  70. public List<T> getData() {
  71. return mDataSet;
  72. }
  73. /**
  74. * 追加一些数据
  75. */
  76. public void addData(List<T> data) {
  77. if (mDataSet != null) {
  78. mDataSet.addAll(data);
  79. } else {
  80. mDataSet = data;
  81. }
  82. notifyDataSetChanged();
  83. }
  84. /**
  85. * 清空当前数据
  86. */
  87. public void clearData() {
  88. //当前的数据不能为空
  89. if (mDataSet == null || mDataSet.size() == 0) return;
  90. mDataSet.clear();
  91. notifyDataSetChanged();
  92. }
  93. /**
  94. * 获取某个位置上的数据
  95. */
  96. @Override
  97. public T getItem(int position) {
  98. return mDataSet.get(position);
  99. }
  100. /**
  101. * 更新某个位置上的数据
  102. */
  103. public void setItem(int position, T item) {
  104. if (mDataSet == null) mDataSet = new ArrayList<>();
  105. mDataSet.set(position, item);
  106. notifyDataSetChanged();
  107. }
  108. /**
  109. * 添加单条数据
  110. */
  111. public void addItem(T item) {
  112. if (mDataSet == null) mDataSet = new ArrayList<>();
  113. addItem(mDataSet.size(), item);
  114. }
  115. /**
  116. * 添加单条数据
  117. */
  118. public void addItem(int position, T item) {
  119. if (mDataSet == null) mDataSet = new ArrayList<>();
  120. //如果是在for循环添加后要记得position++
  121. if (position < mDataSet.size()) {
  122. mDataSet.add(position, item);
  123. } else {
  124. mDataSet.add(item);
  125. }
  126. notifyDataSetChanged();
  127. }
  128. /**
  129. * 删除单条数据
  130. */
  131. public void removeItem(T item) {
  132. int index = mDataSet.indexOf(item);
  133. if (index != -1) {
  134. removeItem(index);
  135. }
  136. }
  137. public void removeItem(int position) {
  138. //如果是在for循环删除后要记得i--
  139. mDataSet.remove(position);
  140. notifyDataSetChanged();
  141. }
  142. /**
  143. * 获取上下文对象,注意不要在构造方法中调用
  144. */
  145. public Context getContext() {
  146. return mContext;
  147. }
  148. /**
  149. * 获取资源对象
  150. */
  151. public Resources getResources() {
  152. return mContext.getResources();
  153. }
  154. /**
  155. * 获取资源文本
  156. */
  157. public String getString(@StringRes int resId) {
  158. return mContext.getString(resId);
  159. }
  160. /**
  161. * 获取资源颜色
  162. */
  163. public int getColor(@ColorRes int id) {
  164. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  165. return mContext.getColor(id);
  166. }else {
  167. return mContext.getResources().getColor(id);
  168. }
  169. }
  170. /**
  171. * 获取资源图像
  172. */
  173. public Drawable getDrawable(@DrawableRes int id) {
  174. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  175. return mContext.getDrawable(id);
  176. }else {
  177. return mContext.getResources().getDrawable(id);
  178. }
  179. }
  180. public class ViewHolder {
  181. private final View itemView;
  182. // 内存优化和防止泄露
  183. private SparseArray<WeakReference<View>> mViews = new SparseArray<>();
  184. public ViewHolder(ViewGroup parent, int layoutId) {
  185. this(LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false));
  186. }
  187. public ViewHolder(View itemView) {
  188. this.itemView = itemView;
  189. }
  190. public final View getItemView() {
  191. return itemView;
  192. }
  193. @SuppressWarnings("unchecked")
  194. public final <V extends View> V findViewById(@IdRes int id) {
  195. WeakReference<View> reference = mViews.get(id);
  196. if (reference != null && reference.get() != null) {
  197. return (V) reference.get();
  198. }else {
  199. View view = itemView.findViewById(id);
  200. mViews.put(id, new WeakReference<>(view));
  201. return (V) view;
  202. }
  203. }
  204. }
  205. }