1

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

2 回答 2

2

几天来,我也面临着类似的问题。但在我的情况下,它是 NestedScrollView 弄乱了我的回收器视图的滚动列表,所以我所做的只是将 OnScrollChangeListener 设置为嵌套的滚动视图而不是回收器视图,现在我可以检测到列表何时到达末尾并获取更多数据

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();

                }
            }
        }
    }
  }
});
于 2018-11-22T12:15:31.573 回答
0

不要使用recyclerview.addOnScrollListener(),而是使用nestedScrollView.setOnScrollChangeListener()。

 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();  
            }            
        }
      }
    }
  }
});
于 2019-11-15T11:45:27.200 回答