From ae6393a45a27071316876c6b12a70331b7a7ea2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=AF=E5=BD=AC?= Date: Fri, 7 Jun 2024 10:55:18 +0800 Subject: [PATCH] =?UTF-8?q?add=20BaseListAdapter=20=E5=A2=9E=E5=8A=A0=20se?= =?UTF-8?q?tNewData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adapter/BaseByRecyclerViewAdapter.java | 2 +- .../library/adapter/BaseListAdapter.java | 35 +++++++++++++++---- .../fragment/BindingListFragment.java | 3 +- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/ByRecyclerview/src/main/java/me/jingbin/library/adapter/BaseByRecyclerViewAdapter.java b/ByRecyclerview/src/main/java/me/jingbin/library/adapter/BaseByRecyclerViewAdapter.java index a8643f3..1158ab4 100644 --- a/ByRecyclerview/src/main/java/me/jingbin/library/adapter/BaseByRecyclerViewAdapter.java +++ b/ByRecyclerview/src/main/java/me/jingbin/library/adapter/BaseByRecyclerViewAdapter.java @@ -238,7 +238,7 @@ public void setPageEmptyView(int emptyLayoutId) { } /** - * 设置数据和处理空视图。空视图是在 setEmptyView() 设置的。 + * 设置数据和处理空视图。空视图是在 setPageEmptyView() 设置的。 * 如果想列表上方显示状态视图(StateView),不能使用这个方法。 * * @param isFirstPage 是否是第一页 diff --git a/ByRecyclerview/src/main/java/me/jingbin/library/adapter/BaseListAdapter.java b/ByRecyclerview/src/main/java/me/jingbin/library/adapter/BaseListAdapter.java index 0dc86d7..5c238d1 100644 --- a/ByRecyclerview/src/main/java/me/jingbin/library/adapter/BaseListAdapter.java +++ b/ByRecyclerview/src/main/java/me/jingbin/library/adapter/BaseListAdapter.java @@ -37,17 +37,21 @@ protected BaseListAdapter() { } protected BaseListAdapter(List data) { - this.mData = data == null ? new ArrayList() : data; + mData = data == null ? new ArrayList() : 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 @@ -81,24 +85,41 @@ public List getData() { } public void setData(List data) { - this.mData = data; + mData = data; } public void addAll(List data) { - this.mData.addAll(data); + checkNoNull(); + mData.addAll(data); } public void removeAll(List 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 data) { + this.mData = data == null ? new ArrayList() : data; + notifyDataSetChanged(); + } } diff --git a/app/src/main/java/me/jingbin/byrecyclerview/fragment/BindingListFragment.java b/app/src/main/java/me/jingbin/byrecyclerview/fragment/BindingListFragment.java index 937e657..9d5b7c6 100644 --- a/app/src/main/java/me/jingbin/byrecyclerview/fragment/BindingListFragment.java +++ b/app/src/main/java/me/jingbin/byrecyclerview/fragment/BindingListFragment.java @@ -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);