nested scrollview inside recyclerview not triggering onscrolllistener when scrolling down

北城余情 提交于 2020-01-23 06:22:27

问题


Edit:

I have loading more than 200 datas from webservice.When I am scrolling down the recyclerview, it is not triggering the scrollLisener.

Because, if I'm not used dy>0 condition, it is loading all next 20 datas, 20 datas and so on, initially when coming to this activity.

Below I have posted the code relevant to that.

Logcat:

E/dy: 0

Activity code:

recyclerView = (RecyclerView) findViewById(R.id.rv_list_tab_home_recycler);

recyclerView.setHasFixedSize(true);

mLayoutManager = new LinearLayoutManager(this);

recyclerView.setLayoutManager(mLayoutManager);

//homePostitemsAdapter = new UserPostAdapter(TabHomeActivity.this, homePostItemsList);
homePostitemsAdapter = new TabHomeAdapter(homePostItemsList, recyclerView);

//  recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(homePostitemsAdapter);
recyclerView.setNestedScrollingEnabled(false);

Adapter Code:

private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;

public TabHomeAdapter(List<HomePostItems> objects, RecyclerView recycle) {

    homePostArrListItems = objects;

    if (recycle.getLayoutManager() instanceof LinearLayoutManager) {

        final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recycle
                .getLayoutManager();

        Log.e("LinearLayoutManager", "Test");

        recycle.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView,
                                           int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                totalItemCount = linearLayoutManager.getItemCount();
                lastVisibleItem = linearLayoutManager
                                .findLastVisibleItemPosition();
                Log.e("dy", ""+dy);

                if (!loading
                                && totalItemCount <= (lastVisibleItem + visibleThreshold ) && dy > 0) {
                    // End has been reached
                    // Do something

                    Log.e("totalItemCount", ""+totalItemCount);
                    Log.e("lastVisibleItem", ""+lastVisibleItem);
                    Log.e("visibleThreshold", ""+visibleThreshold);
                    Log.e("loading", ""+loading);
                    Log.e("onLoadMoreListener", ""+onLoadMoreListener);

                    if (onLoadMoreListener != null) {;
                        onLoadMoreListener.onLoadMore();
                    }

                    loading = true;
                }

            }
        });
    } 

    public void setLoaded() {
        loading = false;
    }

    @Override
    public int getItemCount() {    
        return homePostArrListItems.size(); 
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    public static class ProgressViewHolder extends RecyclerView.ViewHolder {
        public ProgressBar progressBar;
        public ProgressViewHolder(View v) {
            super(v);
            progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
        }
    }
}

When I initially came to this activity, I am getting dy as 0. When I'm scrolling the RecyclerView , dy is not triggering in LogCat.

I think because of the NestedScrollView it is not working.But I need nested scroll view because I need to scroll down some views before RecyclerView.


回答1:


If it is lazy loading you want then have a look at my RecyclerAdapter

@Override
public void onBindViewHolder(VH viewHolder, final int position) {

    // Set Data to Views

    if(position == count) {
        // When last item is reached.

        if (onLoadMoreListener != null) {;
            onLoadMoreListener.onLoadMore();
        }
    }
}

I think this is easier and fairly inexpensive way to achieve lazy loading.




回答2:


Try This its works for me....

nestedScrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
        if(v.getChildAt(v.getChildCount() - 1) != null) {
            if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
                    scrollY > oldScrollY) {
                    //code to fetch more data for endless scrolling
            }
        }
    });

Reference : NestedScrollView




回答3:


Try this it's work for me #NestedScrollView in side RecycleView Pagination

 public abstract class NestedScroll implements NestedScrollView.OnScrollChangeListener {

    public void onScrollChange(NestedScrollView view, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

        if (view.getChildAt(view.getChildCount() - 1) != null) {
            if ((scrollY >= (view.getChildAt(view.getChildCount() - 1).getMeasuredHeight() - view.getMeasuredHeight())) &&
                    scrollY > oldScrollY) {
                onScroll();
            }

        }
    }

    public abstract void onScroll();
}

and call ->

//NestedScrollView nested
nested.setOnScrollChangeListener(new NestedScroll() {
        @Override
        public void onScroll() {
            //do here 
        }
    });



回答4:


1. Change RecycleView view like this

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);

2. Set scroll listener to your NestedScroll like this

  NestedScrollView nestedScrollView = NestedScrollView)findViewById(R.id.nestedscroll);
         nestesdScroll.getViewTreeObserver().addOnScrollChangedListener(new 
         ViewTreeObserver.OnScrollChangedListener() {
                    @Override
                    public void onScrollChanged() {
                        View view = (View) nestesdScroll.getChildAt(nestesdScroll.getChildCount() - 1);
                        int diff = (view.getBottom() - (nestesdScroll.getHeight() + nestesdScroll.getScrollY()));
                        if (diff == 0) {  
                           //code to fetch more data for endless scrolling                
                           load_more_data();
                        }
                    }
                });


来源:https://stackoverflow.com/questions/40103183/nested-scrollview-inside-recyclerview-not-triggering-onscrolllistener-when-scrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!