简体   繁体   English

recyclerview addOnScrollListener不起作用

[英]recyclerview addOnScrollListener doesnt work

I have a recyclerview which is filled in another thread, but the code in addOnScrollListener doesnt work when scrolling. 我在另一个线程中填充了一个recyclerview,但是滚动时addOnScrollListener中的代码不起作用。

   private class LoadTask extends AsyncTask< NodeList, Void, Void> {

    @Override
    protected Void doInBackground(NodeList... params) {
      recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    adapter = new RecyclerViewAdapter(JoinSearchApps.this);
    recyclerView.setAdapter(adapter);
    GridLayoutManager lLayout = new GridLayoutManager(JoinSearchApps.this, 1);

    recyclerView.setLayoutManager(lLayout);

    recyclerView.setItemAnimator(new DefaultItemAnimator());
     recyclerView.addOnScrollListener(new PaginationScrollListener(lLayout) {
        @Override
        protected void loadMoreItems() {
             Log.d("inaddOnScrollListener","addOnScrollListener");
          }
       }

        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
          LoadTask  loader=new LoadTask();
          loader.execute();
    }
}

you have to setLayoutManager before setting adapter to your recyclerview 您必须先设置setLayoutManager才能将adapter设置为您的recyclerview

change your code like below code 像下面的代码一样更改您的代码

recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
GridLayoutManager lLayout = new GridLayoutManager(JoinSearchApps.this, 1); 
recyclerView.setLayoutManager(lLayout);
adapter = new RecyclerViewAdapter(JoinSearchApps.this);
recyclerView.setAdapter(adapter);

For better ease, you can add another class EndlessRecyclerOnScrollListener 为了更轻松,您可以添加另一个类EndlessRecyclerOnScrollListener

Step 1: Create this class 第1步:创建此类

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
    public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();

    private int previousTotal = 0; // The total number of items in the dataset after the last load
    private boolean loading = true; // True if we are still waiting for the last set of data to load.
    private int visibleThreshold = 0; // The minimum amount of items to have below your current scroll position before loading more.
    int firstVisibleItem, visibleItemCount, totalItemCount;

    private int current_page = 0;

    private LinearLayoutManager mLinearLayoutManager;

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

    @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;
            }
        }
        //Log.d(TAG, "loading---"+loading);
        //Log.d(TAG, (totalItemCount - visibleItemCount) + "-checking-" + (firstVisibleItem + visibleThreshold));
        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);
}

Step 2: call method like this: 步骤2:像这样调用方法:

mRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) {
            @Override
            public void onLoadMore(int current_page) {
                int listSize = notificationList.size();
                if (listSize < notificationCount) {
                    offset = offset + 10;
                    limit = limit + 10;
                    getNotificationsScroll(offset, 10);
                }
            }
        });

I had the similar issue and i solved it using this code ... First create this class 我遇到了类似的问题,并使用此代码解决了该问题……首先创建此类

public abstract class EndlessOnScrollListener extends OnScrollListener {

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

// use your LayoutManager instead
private LinearLayoutManager llm;

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

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

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

Second .. in you activity use this 其次..在您的活动中使用此

 RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
 GridLayoutManager lLayout = new GridLayoutManager(JoinSearchApps.this, 1);
recyclerView.setLayoutManager(lLayout);
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new RecyclerViewAdapter(JoinSearchApps.this);
recyclerView.setAdapter(adapter);

recyclerView.addOnScrollListener(new EndlessOnScrollListener() {
@Override
public void onScrolledToEnd() {
    if (!loading) {
        loading = true;
 Log.d("inaddOnScrollListener","addOnScrollListener");
        //Do your operation here and notify the adapter by youradapter.notifyDataSetChanged()
    }
    loading = false;
}
});

I hope it will work for you to0. 希望对您有帮助。

You can't call the method inside the PaginationScrollListener anything you want. 您无法在PaginationScrollListener内部调用任何方法。 You called it loadMoreItems but that function won't get called. 您将其称为loadMoreItems但不会调用该函数。 You probably want to rename it to something like onScrolled or one of the other methods of PaginationScrollListener 您可能想将其重命名为类似onScrolled或PaginationScrollListener的其他方法之一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM