Skip to content

Commit

Permalink
add BaseListAdapter 增加 setNewData
Browse files Browse the repository at this point in the history
  • Loading branch information
youlookwhat committed Jun 7, 2024
1 parent b11fe6e commit ae6393a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void setPageEmptyView(int emptyLayoutId) {
}

/**
* 设置数据和处理空视图。空视图是在 setEmptyView() 设置的。
* 设置数据和处理空视图。空视图是在 setPageEmptyView() 设置的。
* 如果想列表上方显示状态视图(StateView),不能使用这个方法。
*
* @param isFirstPage 是否是第一页
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ protected BaseListAdapter() {
}

protected BaseListAdapter(List<T> data) {
this.mData = data == null ? new ArrayList<T>() : data;
mData = data == null ? new ArrayList<T>() : data;
}

@Override
public int getCount() {
checkNoNull();
return mData.size();
}

@Override
public T getItem(int position) {
return mData.get(position);
if (mData != null && mData.size() > 0 && position >= 0 && position < mData.size()) {
return mData.get(position);
}
return null;
}

@Override
Expand Down Expand Up @@ -81,24 +85,41 @@ public List<T> getData() {
}

public void setData(List<T> data) {
this.mData = data;
mData = data;
}

public void addAll(List<T> data) {
this.mData.addAll(data);
checkNoNull();
mData.addAll(data);
}

public void removeAll(List<T> data) {
this.mData.removeAll(data);
if (mData == null) return;
mData.removeAll(data);
}

public void add(T t) {
this.mData.add(t);
checkNoNull();
mData.add(t);
}

public void clear() {
this.mData.clear();
if (mData == null) return;
mData.clear();
}

private void checkNoNull() {
if (mData == null) {
mData = new ArrayList<>();
}
}

/**
* 初始化数据,并刷新页面
*/
public void setNewData(List<T> data) {
this.mData = data == null ? new ArrayList<T>() : data;
notifyDataSetChanged();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ private void initAdapter() {
LayoutHeaderViewBinding headerBinding = DataBindingUtil.inflate(LayoutInflater.from(activity), R.layout.layout_header_view, binding.listView, false);
LayoutFooterViewBinding footerBinding = DataBindingUtil.inflate(LayoutInflater.from(activity), R.layout.layout_footer_view, binding.listView, false);

ListViewBindingAdapter mAdapter = new ListViewBindingAdapter(DataUtil.get(activity, 30));
ListViewBindingAdapter mAdapter = new ListViewBindingAdapter();
binding.listView.setAdapter(mAdapter);
binding.listView.addFooterView(footerBinding.getRoot());
binding.listView.addHeaderView(headerBinding.getRoot());
mAdapter.setNewData(DataUtil.get(activity, 30));

binding.listView.setHeaderDividersEnabled(false);
binding.listView.setFooterDividersEnabled(false);
Expand Down

0 comments on commit ae6393a

Please sign in to comment.