我正在尝试实现此源代码以制作无限滚动循环视图。但在这种情况下不起作用。我正在尝试recyclerView.addOnScrollListener()和recyclerView.setOnScrollListener()但不能正常工作
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_sanction_info, container, false);
recyclerView = view.findViewById(R.id.recyclerView);
linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
sanctionInfoAdapter = new SanctionInfoAdapter(getActivity(), adapterData);
recyclerView.setAdapter(sanctionInfoAdapter);
recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int current_page) {
if (adapterData.length() > 0) {
loadMore(current_page);
Log.e(TAG, "Scroll if");
} else {
loadMore(0);
Log.e(TAG, "Scroll else");
}
}
});
return view;
此源代码从api
获取数据 private void loadMore(int pageIndex) {
final ProgressDialog pDialog = new ProgressDialog(getActivity());
pDialog.setMessage(getString(R.string.please_wait));
pDialog.setCanceledOnTouchOutside(false);
pDialog.show();
Map<String, String> params = new HashMap<>();
params.put("username", SharedPreferenceClass.getUserName(getActivity()));
params.put("api_key", ConstantClass.API_KEY);
params.put("imei", SharedPreferenceClass.getImeiNumber(getActivity()));
params.put("hits", String.valueOf(pageIndex));
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, ConstantClass.SANCTION_INFO,
new JSONObject(params), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
pDialog.dismiss();
try {
adapterData = response.getJSONArray("result");
Log.e(TAG, adapterData.toString());
sanctionInfoAdapter.notifyDataSetChanged();
} catch (Exception ex) {
Log.e(TAG, ex.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pDialog.dismiss();
Log.e(TAG, error.toString());
}
});
AppClass.getInstance().addToRequestQueue(jsonObjectRequest, TAG);
}
这是我的EndlessScrollListener源代码
public abstract class EndlessRecyclerOnScrollListener extends
RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerOnScrollListener.class
.getSimpleName();
public int previousTotal = 0;
private boolean loading = true;
int firstVisibleItem, visibleItemCount, totalItemCount;
private int current_page = 0;
private LinearLayoutManager mLinearLayoutManager;
public EndlessRecyclerOnScrollListener(
LinearLayoutManager linearLayoutManager) {
this.mLinearLayoutManager = linearLayoutManager;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
int visibleThreshold = 8;
if (!loading
&& (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
current_page++;
onLoadMore(current_page);
loading = true;
}
}
public abstract void onLoadMore(int current_page);
}
答案 0 :(得分:1)
几天来,我也一直面临着类似的问题。但是在我的情况下,正是NestedScrollView与recyclerView的scrollListner混淆了,所以我所做的就是将setOnScrollChangeListener设置为嵌套的ScrollView而不是recyclerView,现在我可以检测列表何时到达末尾并获取更多数据>
mNestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if(v.getChildAt(v.getChildCount() - 1) != null) {
if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
scrollY > oldScrollY) {
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (isLoadData()) {
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
fetchMoreData();
}
}
}
}
}
});
答案 1 :(得分:0)
使用nestedScrollView.setOnScrollChangeListener()代替使用recyclerview.addOnScrollListener()。
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX,
int oldScrollY) {
LinearLayoutManager layoutManager = (LinearLayoutManager) questionRecyclerView.getLayoutManager();
if (layoutManager != null) {
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (!questionAdapter.isLoading()) {
if (visibleItemCount +firstVisibleItemPosition >= totalItemCount
&& firstVisibleItemPosition >= 0
&& totalItemCount >= PAGE_SIZE) {
// loadMore();
}
}
}
}
}
});