完善搜索功能模块
This commit is contained in:
parent
f0c4dd1146
commit
6ce970df07
@ -0,0 +1,8 @@
|
||||
package com.yunbao.common.views.weight;
|
||||
|
||||
/**
|
||||
* 滚动到底部的监听
|
||||
*/
|
||||
public interface OnBottomListener {
|
||||
void onBottom();
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -100,4 +100,12 @@ public class SearchResultsItemAdapter extends RecyclerView.Adapter {
|
||||
}
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载更多资源
|
||||
*/
|
||||
public void onLoadMoreData(List<List<SearchModel>> mLists) {
|
||||
lists.addAll(mLists);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
@ -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<List<List<SearchModel>>>() {
|
||||
@Override
|
||||
public void onSuccess(List<List<SearchModel>> 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<List<SearchModel>>() {
|
||||
@Override
|
||||
public void onSuccess(List<SearchModel> data) {
|
||||
List<List<SearchModel>> listList = new ArrayList<>();
|
||||
listList.add(data);
|
||||
itemAdapter.showData(listList, type);
|
||||
if (data.size() == 0) {
|
||||
ToastUtil.show(R.string.refresh_footer_nothing);
|
||||
} else {
|
||||
List<List<SearchModel>> listList = new ArrayList<>();
|
||||
listList.add(data);
|
||||
if (page > 1) {
|
||||
itemAdapter.onLoadMoreData(listList);
|
||||
} else {
|
||||
itemAdapter.showData(listList, type);
|
||||
}
|
||||
}
|
||||
|
||||
fragment.dismiss();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user