【问题标题】:RecyclerView scroll listener's method onscolled calls automatically without scrollingRecyclerView 滚动侦听器的方法 onscolled 自动调用而不滚动
【发布时间】:2016-09-15 10:05:13
【问题描述】:

在实现分页时。我遇到了这个问题。无需滚动 onscrolled 方法连续调用。 请检查我下面的代码。

作为参考,我使用的是Endless Scrolling with AdapterViews and RecyclerView

还有一个演示示例this

我正在从 API 请求数据。所以我实施了凌空抽射。设置完成后,奇怪的事情发生了。 OnScrolled 方法连续调用而不滚动。 下面是我的代码。

    HomeFragment.java

    view = inflater.inflate(R.layout.fragment_home, container, false);

    tvNoDataFound = (TextView) view.findViewById(R.id.tv_no_data_found);
    recyclerView = (RecyclerView)view.findViewById(R.id.listView);

    linearLayoutManager = new LinearLayoutManager(getActivity());

    recyclerView.setNestedScrollingEnabled(false);

    recyclerView.setLayoutManager(linearLayoutManager);


    context = view.getContext();

    // Lookup the swipe container view
    swipeProjects = (SwipeRefreshLayout)view.findViewById(R.id.swipeProjects);

    // Setup refresh listener which triggers new data loading
    swipeProjects.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            // Your code to refresh the list here.
            // Make sure you call swipeContainer.setRefreshing(false)
            // once the network request has completed successfully.
            getProjects(0,0);
        }
    });
    // Configure the refreshing colors
    swipeProjects.setColorSchemeResources(android.R.color.holo_blue_bright,
            android.R.color.holo_green_light,
            android.R.color.holo_orange_light,
            android.R.color.holo_red_light);

    List<LatestProject> getLatest = getProjects(0,0);

    recyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener(linearLayoutManager) {

        @Override
        public void onLoadMore(int page, int totalItemsCount) {

            Log.e("here scrolling","scrolling---"+page+"---"+totalItemsCount);

            // Triggered only when new data needs to be appended to the list
            // Add whatever code is needed to append new items to the bottom of the list
            List<LatestProject> getLatest = getProjects(totalItemsCount,page);
        }
    });
return view;

得到服务器的响应后,我使用下面的函数来设置适配器。

public List<LatestProject> returnProjects(List<LatestProject> list, int offset, int curSizes){

    if(offset!=0){
        int curSize = adapter.getItemCount();
        latestProjectsList.addAll(list);
        adapter.notifyItemRangeInserted(curSize, list.size() - 1);
    }else{
        latestProjectsList = list;
        adapter = new ProjectListAdapter(list,getActivity(),HomeFragment.this);
        recyclerView.setAdapter(adapter);
    }
    return list;
}

在上面的代码中,我的第一个 API 调用的偏移量为 0。我不知道是什么问题。是不是因为volley异步请求?

【问题讨论】:

    标签: android android-recyclerview android-volley endlessscroll


    【解决方案1】:

    是的,它会被调用。为了克服这个问题,请使用一种方法

    private boolean isLastItemDisplaying(RecyclerView recyclerView) {
        if (recyclerView.getAdapter().getItemCount() != 0) {
            int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
            if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
                return true;
        }
        return false;
    }
    

    现在在onScolled 方法中检查该方法。

     boolean a = isLastItemDisplaying(recycler_product);
     if(a)
         {
            List<LatestProject> getLatest = getProjects(totalItemsCount,page);
         }
    

    【讨论】:

    • 感谢您的帮助。但是这个逻辑已经存在于 EndlessRecyclerViewScrollListener.java 中。我只犯了一个错误。在 xml 中,recyclerView 在 NestedScrollView 内。所以一旦我删除了 NestedScrollView,它就开始工作了。所以这就是问题所在。
    • 我们不能在不删除 NestedScrollView 的情况下这样做吗?就我而言,我应该保留 NestedScrollView
    • recyclerview 不需要滚动视图,它本身有滚动视图
    • 仍然两次调用最后一项。如何避免这种多次调用?
    • 感谢您的帮助。真的很好用!
    【解决方案2】:
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                }
    
                @Override
                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
    
                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    
                    if (!isLoading) {
                        if (linearLayoutManager != null && linearLayoutManager.findLastCompletelyVisibleItemPosition() == rowsArrayList.size() - 1) {
                            //bottom of list!
                            loadMore();
                            isLoading = true;
                        }
                    }
                }
            });
    

    来源:JournalDev

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多