【问题标题】:How to do pagination with NestedScrollView?如何使用 NestedScrollView 进行分页?
【发布时间】:2018-01-18 11:44:51
【问题描述】:

当我使用嵌套滚动视图进行分页时,它需要太多时间,有时我的应用程序会挂起?请告诉我使用嵌套滚动视图实现分页的正确方法

【问题讨论】:

  • 你的意思是RecyclerView中的分页?
  • 我的 Recyclerview 在 NestedScrollView 里面所以我从 Nestedscrollview 得到动作响应
  • 对不起,我不知道你想要达到什么目的
  • 我使用nestedscrollview 操作处理了所有分页。因为nestedscrollview 是我的父视图。
  • 您的 Recyclerview 花费了这么多时间,因为它在 NestedScrollView 内并且它正在获取完整长度。所以它会自动调用下一页直到结束(您可以查看日志)。

标签: android android-recyclerview pagination android-nestedscrollview recyclerview-layout


【解决方案1】:

在你的包中添加这个类

    import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.View;

public class TouchDetectableScrollView extends NestedScrollView {

    OnMyScrollChangeListener myScrollChangeListener;

    public TouchDetectableScrollView(Context context) {
        super(context);
    }

    public TouchDetectableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TouchDetectableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);

        if (myScrollChangeListener!=null)
        {
            if (t>oldt)
            {
                myScrollChangeListener.onScrollDown();
            }
            else if (t<oldt){
                myScrollChangeListener.onScrollUp();
            }
            View view = (View) getChildAt(getChildCount()-1);
            int diff = (view.getBottom()-(getHeight()+getScrollY()));
            if (diff == 0 ) {
                myScrollChangeListener.onBottomReached();
            }
        }
    }

    public OnMyScrollChangeListener getMyScrollChangeListener() {
        return myScrollChangeListener;
    }

    public void setMyScrollChangeListener(OnMyScrollChangeListener myScrollChangeListener) {
        this.myScrollChangeListener = myScrollChangeListener;
    }

    public interface OnMyScrollChangeListener
    {
            public void onScrollUp();
            public void onScrollDown();
            public void onBottomReached();
    }
}

现在在您的 xml 和 java 代码中使用 TouchDetectableScrollView 而不是 NestedScrollView。 并像这样设置监听器:

TouchDetectableScrollView nestedScrollView=findViewById(R.id.nestedScrollView);
        nestedScrollView.setMyScrollChangeListener(new TouchDetectableScrollView.OnMyScrollChangeListener() {
            @Override
            public void onScrollUp() {

            }

            @Override
            public void onScrollDown() {

            }

            @Override
            public void onBottomReached() {
                // api call for pagination
            }
        });

onBottomReached 方法中执行分页任务

【讨论】:

  • 是的,它可以工作,谢谢.. 但它需要太多时间来加载。
  • 可能是网络问题,您使用哪个库进行 API 调用?
  • 不,这不是互联网问题。我正在使用 Retrofil 库进行连接
  • 您的自定义 nestedScrollview 工作正常,但在 ViewPager 选项卡中不起作用?
【解决方案2】:

1.设置嵌套滚动启用为 false 的回收站视图。

recyclerView.setNestedScrollingEnabled(false);

2. 将滚动监听器添加到嵌套滚动视图中。

nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (isFeedsFetchInProgress)
                    return;
                if(!mOnLastPage) {
                    int visibleItemCount = linearLayoutManager.getChildCount();
                    int totalItemCount = linearLayoutManager.getItemCount();
                    int pastVisibleItems = linearLayoutManager.findFirstVisibleItemPosition();
                    if (pastVisibleItems + visibleItemCount >= totalItemCount) {
                        //End of list
                        mPageToRequest++;
                        loader = LOADER.BOTTOM;
                        hitApiRequest(ApiConstants.REQUEST_TYPE.GET_REFERRAL_LIST);
                    }
                }
//                int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
                swipeRefreshLayout.setEnabled(scrollY <= 0);

            }
        });

【讨论】:

    猜你喜欢
    • 2019-05-22
    • 2019-10-02
    • 2019-02-03
    • 2012-11-10
    • 2011-06-17
    • 2018-03-04
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多