簡體   English   中英

recyclerview addOnScrollListener不起作用

[英]recyclerview addOnScrollListener doesnt work

我在另一個線程中填充了一個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();
    }
}

您必須先設置setLayoutManager才能將adapter設置為您的recyclerview

像下面的代碼一樣更改您的代碼

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

為了更輕松,您可以添加另一個類EndlessRecyclerOnScrollListener

第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);
}

步驟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);
                }
            }
        });

我遇到了類似的問題,並使用此代碼解決了該問題……首先創建此類

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();
}

其次..在您的活動中使用此

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

希望對您有幫助。

您無法在PaginationScrollListener內部調用任何方法。 您將其稱為loadMoreItems但不會調用該函數。 您可能想將其重命名為類似onScrolled或PaginationScrollListener的其他方法之一

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM