我正在尝试实现这个源代码来制作无尽的滚动回收视图。但在这种情况下不起作用。我正在尝试 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);
}