From 6ce970df07d0ff6555bd0adc38569cfcc3552314 Mon Sep 17 00:00:00 2001 From: 18401019693 Date: Mon, 8 Aug 2022 13:08:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=90=9C=E7=B4=A2=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/views/weight/OnBottomListener.java | 8 ++ .../weight/OnRecyclerViewScrollListener.java | 92 +++++++++++++++++++ .../adapter/SearchResultsItemAdapter.java | 8 ++ .../fragment/SearchResultsItemFragment.java | 31 ++++++- 4 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 common/src/main/java/com/yunbao/common/views/weight/OnBottomListener.java create mode 100644 common/src/main/java/com/yunbao/common/views/weight/OnRecyclerViewScrollListener.java diff --git a/common/src/main/java/com/yunbao/common/views/weight/OnBottomListener.java b/common/src/main/java/com/yunbao/common/views/weight/OnBottomListener.java new file mode 100644 index 000000000..7c22e6a3e --- /dev/null +++ b/common/src/main/java/com/yunbao/common/views/weight/OnBottomListener.java @@ -0,0 +1,8 @@ +package com.yunbao.common.views.weight; + +/** + * 滚动到底部的监听 + */ +public interface OnBottomListener { + void onBottom(); +} diff --git a/common/src/main/java/com/yunbao/common/views/weight/OnRecyclerViewScrollListener.java b/common/src/main/java/com/yunbao/common/views/weight/OnRecyclerViewScrollListener.java new file mode 100644 index 000000000..ab41d7243 --- /dev/null +++ b/common/src/main/java/com/yunbao/common/views/weight/OnRecyclerViewScrollListener.java @@ -0,0 +1,92 @@ +package com.yunbao.common.views.weight; + +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; +import androidx.recyclerview.widget.StaggeredGridLayoutManager; + +public class OnRecyclerViewScrollListener extends RecyclerView.OnScrollListener implements OnBottomListener { + + public enum LAYOUT_MANAGER_TYPE { + LINEAR, + GRID, + STAGGERED_GRID + } + /* LayoutManager的类型(枚举)*/ + protected LAYOUT_MANAGER_TYPE mLayoutManagerType; + /*存储瀑布流每一列最下面的那个item的位置*/ + private int[] mLastPositions; + /*最后一个可见的item的位置*/ + private int mLastVisibleItemPosition; + /*记录当前滑动的状态*/ + private int mCurrentScrollState = 0; + /*用于判断滑动方向,dy等于0,表示RecyclerView没有滑动,比如条目没充满RecyclerView的话,手势不管哪个方向滑都是0;dy大于0,RecyclerView向上滑动;dy小于0 ,RecyclerView向下滑动*/ + private int mDy; + + @Override + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + this.mDy = dy; + RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); + if (mLayoutManagerType == null) { + if (layoutManager instanceof LinearLayoutManager) { + mLayoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR; + } else if (layoutManager instanceof GridLayoutManager) { + mLayoutManagerType = LAYOUT_MANAGER_TYPE.GRID; + } else if (layoutManager instanceof StaggeredGridLayoutManager) { + mLayoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID; + } else { + throw new RuntimeException("Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager"); + } + } + + switch (mLayoutManagerType) { + case LINEAR: + mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition(); + break; + case GRID: + mLastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition(); + break; + case STAGGERED_GRID: + StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; + if (mLastPositions == null) { + mLastPositions = new int[staggeredGridLayoutManager.getSpanCount()]; + } + staggeredGridLayoutManager.findLastVisibleItemPositions(mLastPositions); + mLastVisibleItemPosition = findMax(mLastPositions); + break; + } + + } + + + @Override + public void onScrollStateChanged(RecyclerView recyclerView, int newState) { + super.onScrollStateChanged(recyclerView, newState); + mCurrentScrollState = newState; + RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); + int visibleItemCount = layoutManager.getChildCount(); + int totalItemCount = layoutManager.getItemCount(); + if (visibleItemCount > 0 + && mCurrentScrollState == RecyclerView.SCROLL_STATE_IDLE + && mLastVisibleItemPosition >= totalItemCount - 1 && mDy > 0) { + onBottom(); + } + } + + + @Override + public void onBottom() { + + } + + private int findMax(int[] lastPositions) { + int max = lastPositions[0]; + for (int value : lastPositions) { + if (value > max) { + max = value; + } + } + return max; + } +} diff --git a/main/src/main/java/com/yunbao/main/adapter/SearchResultsItemAdapter.java b/main/src/main/java/com/yunbao/main/adapter/SearchResultsItemAdapter.java index 4da41c1eb..39f1910f9 100644 --- a/main/src/main/java/com/yunbao/main/adapter/SearchResultsItemAdapter.java +++ b/main/src/main/java/com/yunbao/main/adapter/SearchResultsItemAdapter.java @@ -100,4 +100,12 @@ public class SearchResultsItemAdapter extends RecyclerView.Adapter { } notifyDataSetChanged(); } + + /** + * 加载更多资源 + */ + public void onLoadMoreData(List> mLists) { + lists.addAll(mLists); + notifyDataSetChanged(); + } } diff --git a/main/src/main/java/com/yunbao/main/fragment/SearchResultsItemFragment.java b/main/src/main/java/com/yunbao/main/fragment/SearchResultsItemFragment.java index 82bac2af4..2079a6bbe 100644 --- a/main/src/main/java/com/yunbao/main/fragment/SearchResultsItemFragment.java +++ b/main/src/main/java/com/yunbao/main/fragment/SearchResultsItemFragment.java @@ -14,6 +14,7 @@ import com.yunbao.common.fragment.BaseFragment; import com.yunbao.common.http.base.HttpCallback; import com.yunbao.common.http.main.MainNetManager; import com.yunbao.common.utils.ToastUtil; +import com.yunbao.common.views.weight.OnRecyclerViewScrollListener; import com.yunbao.main.R; import com.yunbao.main.adapter.SearchResultsItemAdapter; import com.yunbao.main.utils.WordsTypeUtil; @@ -27,6 +28,7 @@ import java.util.List; public class SearchResultsItemFragment extends BaseFragment { private String searchKey = ""; private int page = 1; + private int type = 1; private RecyclerView searchList; private SearchResultsItemAdapter itemAdapter; @@ -46,6 +48,13 @@ public class SearchResultsItemFragment extends BaseFragment { itemAdapter = new SearchResultsItemAdapter(); searchList.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false)); searchList.setAdapter(itemAdapter); + searchList.addOnScrollListener(new OnRecyclerViewScrollListener() { + @Override + public void onBottom() { + page = page + 1; + setSearchKey(searchKey, type); + } + }); } @Override @@ -64,6 +73,7 @@ public class SearchResultsItemFragment extends BaseFragment { LoadingDialog fragment = new LoadingDialog(); fragment.show(getChildFragmentManager(), "LoadingDialog"); this.searchKey = searchKey; + this.type = type; if (type == 1) { MainNetManager.get(getActivity()) .search(WordsTypeUtil.changeSimplified(searchKey), @@ -71,7 +81,11 @@ public class SearchResultsItemFragment extends BaseFragment { type, page, new HttpCallback>>() { @Override public void onSuccess(List> data) { - itemAdapter.showData(data, type); + if (page > 1) { + itemAdapter.onLoadMoreData(data); + } else { + itemAdapter.showData(data, type); + } fragment.dismiss(); } @@ -88,9 +102,18 @@ public class SearchResultsItemFragment extends BaseFragment { type, page, new HttpCallback>() { @Override public void onSuccess(List data) { - List> listList = new ArrayList<>(); - listList.add(data); - itemAdapter.showData(listList, type); + if (data.size() == 0) { + ToastUtil.show(R.string.refresh_footer_nothing); + } else { + List> listList = new ArrayList<>(); + listList.add(data); + if (page > 1) { + itemAdapter.onLoadMoreData(listList); + } else { + itemAdapter.showData(listList, type); + } + } + fragment.dismiss(); }