手记

BaseAdapter封装优化-Android进阶

参照ArrayAdapter的源码,对BasAdapter进行封装。添加addAll(),remove(),clear(),sort()等操作数据源的方法。

代码如下
public abstract class BaseAdapterWraper<D> extends BaseAdapter {

  private List<D> mInfos = new ArrayList<>();
  private final Object mLock = new Object();

  protected LayoutInflater mInflater;
  protected Context context;
  private boolean mNotifyOnChange = true;

  public BaseAdapterWraper(Context context) {
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.context = context;
  }
  @Override
  public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
    mNotifyOnChange = true;
  }
  public void setNotifyOnChange(boolean notifyOnChange) {
    mNotifyOnChange = notifyOnChange;
  }

  public Context getContext() {
    return context;
  }

  public int getCount() {
    return isEmpty() ? 0 : mInfos.size();
  }

  public D getItem(int position) {
    return isEmpty() ? null : mInfos.get(position);
  }

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

  public abstract View getView(int position, View convertView, ViewGroup parent);

  public void setDatas(@Nullable List<D> infos) {
    mInfos = infos;
    notifyDataSetChanged();
  }

  public List<D> getDatas() {
    return mInfos;
  }

  public boolean isEmpty() {
    return mInfos == null;
  }

  public void add(D t) {
    synchronized (mLock) {
      if (isEmpty()) {
        return;
      }
      mInfos.add(t);
    }
    if (mNotifyOnChange) notifyDataSetChanged();
  }

  public void addAll(List<D> list) {
    synchronized (mLock) {
      if (isEmpty()) {
        return;
      }
      if (list != null) {
        mInfos.addAll(list);
      }
    }
    if (mNotifyOnChange) notifyDataSetChanged();
  }

  public void clear() {
    if (isEmpty()) {
      return;
    }
    synchronized (mLock) {
      mInfos.clear();
    }
    if (mNotifyOnChange) notifyDataSetChanged();
  }
  public void remove(D t){
    synchronized (mLock){
      if(isEmpty()){
        return;
      }
      if(mInfos != null){
        mInfos.remove(t);
      }
    }
    if (mNotifyOnChange) notifyDataSetChanged();
  }
  public void insert(D t, int index){
    synchronized (mLock){
      if(isEmpty()){
        return;
      }
      if(mInfos != null){
        mInfos.add(index,t);
      }
    }
    if (mNotifyOnChange) notifyDataSetChanged();
  }

  public void sort(Comparator<? super D> comparator){
    synchronized (mLock){
      if(isEmpty()){
        return;
      }
      if(mInfos != null){
        Collections.sort(mInfos, comparator);
      }
    }
    if (mNotifyOnChange) notifyDataSetChanged();
  }
  public void hideLastPositionView(int position, View v) {
    v.setVisibility(position == getCount() - 1 ? View.GONE : View.VISIBLE);
  }

  public void lastPositionDividerFull(int position, View v, int margin) {
    LayoutParams params =
        new LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 0.6f));
    if (position == getCount() - 1) {
      params.setMargins(0, 0, 0, 0);
    } else {
      params.setMargins(margin, 0, 0, 0);
    }
    v.setLayoutParams(params);
  }

  @SuppressWarnings("unchecked")
  protected static <T extends View> T findViewById(View view, int id) {
    return (T) view.findViewById(id);
  }
}
ArrayAdapter源码如下
public abstract class BaseAdapterWraper<D> extends BaseAdapter {

  private List<D> mInfos = new ArrayList<>();
  private final Object mLock = new Object();

  protected LayoutInflater mInflater;
  protected Context context;
  private boolean mNotifyOnChange = true;

  public BaseAdapterWraper(Context context) {
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.context = context;
  }
  @Override
  public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
    mNotifyOnChange = true;
  }
  public void setNotifyOnChange(boolean notifyOnChange) {
    mNotifyOnChange = notifyOnChange;
  }

  public Context getContext() {
    return context;
  }

  public int getCount() {
    return isEmpty() ? 0 : mInfos.size();
  }

  public D getItem(int position) {
    return isEmpty() ? null : mInfos.get(position);
  }

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

  public abstract View getView(int position, View convertView, ViewGroup parent);

  public void setDatas(@Nullable List<D> infos) {
    mInfos = infos;
    notifyDataSetChanged();
  }

  public List<D> getDatas() {
    return mInfos;
  }

  public boolean isEmpty() {
    return mInfos == null;
  }

  public void add(D t) {
    synchronized (mLock) {
      if (isEmpty()) {
        return;
      }
      mInfos.add(t);
    }
    if (mNotifyOnChange) notifyDataSetChanged();
  }

  public void addAll(List<D> list) {
    synchronized (mLock) {
      if (isEmpty()) {
        return;
      }
      if (list != null) {
        mInfos.addAll(list);
      }
    }
    if (mNotifyOnChange) notifyDataSetChanged();
  }

  public void clear() {
    if (isEmpty()) {
      return;
    }
    synchronized (mLock) {
      mInfos.clear();
    }
    if (mNotifyOnChange) notifyDataSetChanged();
  }
  public void remove(D t){
    synchronized (mLock){
      if(isEmpty()){
        return;
      }
      if(mInfos != null){
        mInfos.remove(t);
      }
    }
    if (mNotifyOnChange) notifyDataSetChanged();
  }
  public void insert(D t, int index){
    synchronized (mLock){
      if(isEmpty()){
        return;
      }
      if(mInfos != null){
        mInfos.add(index,t);
      }
    }
    if (mNotifyOnChange) notifyDataSetChanged();
  }

  public void sort(Comparator<? super D> comparator){
    synchronized (mLock){
      if(isEmpty()){
        return;
      }
      if(mInfos != null){
        Collections.sort(mInfos, comparator);
      }
    }
    if (mNotifyOnChange) notifyDataSetChanged();
  }
  public void hideLastPositionView(int position, View v) {
    v.setVisibility(position == getCount() - 1 ? View.GONE : View.VISIBLE);
  }

  public void lastPositionDividerFull(int position, View v, int margin) {
    LayoutParams params =
        new LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 0.6f));
    if (position == getCount() - 1) {
      params.setMargins(0, 0, 0, 0);
    } else {
      params.setMargins(margin, 0, 0, 0);
    }
    v.setLayoutParams(params);
  }

  @SuppressWarnings("unchecked")
  protected static <T extends View> T findViewById(View view, int id) {
    return (T) view.findViewById(id);
  }
}
6人推荐
随时随地看视频
慕课网APP

热门评论

参考ArrayAdapter,还可以继续封装优化

查看全部评论