android baseadapter优化,BaseAdapter封装优化-Android进阶

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

代码如下

public abstract class BaseAdapterWraper extends BaseAdapter {

private List 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 infos) {

mInfos = infos;

notifyDataSetChanged();

}

public List 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 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 findViewById(View view, int id) {

return (T) view.findViewById(id);

}

}

ArrayAdapter源码如下

public abstract class BaseAdapterWraper extends BaseAdapter {

private List 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 infos) {

mInfos = infos;

notifyDataSetChanged();

}

public List 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 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 findViewById(View view, int id) {

return (T) view.findViewById(id);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值