为了账号安全,请及时绑定邮箱和手机立即绑定

BaseAdapter封装优化-Android进阶

标签:
Java 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人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
移动开发工程师
手记
粉丝
424
获赞与收藏
5663

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消