RecyclerView Endless Scroll

时间:2017-02-13 11:06:41

标签: java android android-recyclerview onscrolllistener

我知道有很多关于同一问题的问题,但我无法找到我的问题。当我在我的HttpRequest类中打印URL时,它显示正确的URL但仍然加载相同的数据。

   recyclerView.addOnScrollListener(new asd(manager    ) {
                boolean loading = false;
                int scrollIndex = 1;
                @Override
                public void onScrolledToEnd() {

                    if(!loading){
                        loading = true;
                        try {
                            scrollIndex = scrollIndex+10;
                            String url = "https // xyz  com?q=" + query + "&scrollIndex="+ scrollIndex + "&key=" + getResources().getString(R.string.api_key);
                            new HttpRequest(url,SearchActivity.this).execute();
                            adapter.notifyItemRangeChanged(list.size(),10);
                            adapter.notifyDataSetChanged();
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        }

                    }
                    loading = false;
                }
            });

这是OnScrollListener

public abstract class asd extends RecyclerView.OnScrollListener {


    public static String TAG = asd.class.getSimpleName();

    // use your LayoutManager instead
    private LinearLayoutManager llm;

    public asd(LinearLayoutManager sglm) {
        this.llm = sglm;
    }

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

        if (!recyclerView.canScrollVertically(1)) {
            onScrolledToEnd();
        }
    }

    public abstract void onScrolledToEnd();
}

2 个答案:

答案 0 :(得分:0)

我使用它并且它正在工作,也许你必须玩一点价值以适应你的具体情况。但是我希望它有所帮助:

SELECT `SHIFT`, SUM(`DECIMAL`)
FROM
(
    -- your query here
)
GROUP BY `SHIFT`

这里mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); LinearLayoutManager linearLayoutManager = ((LinearLayoutManager)mRecyclerView.getLayoutManager()); int visibleItemCount = linearLayoutManager.getChildCount(); int totalItemCount = linearLayoutManager.getItemCount(); int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition(); //Log.d(TAG,"Visible="+visibleItemCount+", total="+totalItemCount+", first="+firstVisibleItemPosition); if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && firstVisibleItemPosition >= 0 && totalItemCount >= pageSize) { //Log.d(TAG,"Entrato, lancio"); loadMoreItems(); } } }); 是我每次加载的元素数量; pageSize方法负责从互联网上获取数据并通知适配器。

答案 1 :(得分:0)

使用它。

LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager,maxCount) {
            @Override
            public void onLoadMore(int current_page) {
                /// onLoadMore(current_page);
                loadmoreContacts(current_page);
                Log.e("onLoadMore ", "" + current_page);
            }
        });

创建新班级

public abstract class EndlessRecyclerOnScrollListener extends
    RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerOnScrollListener.class
        .getSimpleName();
int maxCount;
private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 5;
int firstVisibleItem, visibleItemCount, totalItemCount;

private int current_page = 1;

private LinearLayoutManager mLinearLayoutManager;

public EndlessRecyclerOnScrollListener(
        LinearLayoutManager linearLayoutManager,int maxCount) {
    this.mLinearLayoutManager = linearLayoutManager;
    this.maxCount = maxCount;
}

@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;
        }
    }

    if(totalItemCount>=maxCount){
        loading=false;
        return;
    }
    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);}