android - 如果向下滚动时如何回收视图,如何运行任务

时间:2015-09-05 06:13:13

标签: android android-recyclerview

我有一个循环视图,我想在用户到达recycleview结束时运行asynTask。这是我的代码:

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

            visibleItemCount = recycle.getChildCount();
            totalItemCount = mLayoutManager.getItemCount();
            firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();

            if (loadmore) {
                if (totalItemCount > previousTotal) {
                    loadmore = false;
                    previousTotal = totalItemCount;
                }
            }
            if (!loadmore && (totalItemCount - visibleItemCount)
                    <= (firstVisibleItem + visibleThreshold)) {
                if(taskrunning!=null && taskrunning==false){
                    loadmore = false;
                    page = page + 1;
                    new AsyncHttpTask().execute(url);
                }

            }
        }
    });

当我到达recycleView的末尾时它会找到,但是当我滚动时,它会再次调用asynctask。

当我滚动时我不想调用asyncTask,我只想在向下滚动时调用它。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

在我指向下方的位置将loadmore设为true。

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

        visibleItemCount = recycle.getChildCount();
        totalItemCount = mLayoutManager.getItemCount();
        firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();

        if (loadmore) {
            if (totalItemCount > previousTotal) {
                loadmore = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loadmore && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
            if(taskrunning!=null && taskrunning==false){
                loadmore = true; // SET TO TRUE HERE!
                page = page + 1;
                new AsyncHttpTask().execute(url);
            }
        }
    }
});

并在loadmore = false的{​​{1}}部分设置onPostExecute