滚动时的Recycler View Top应该加载数据

时间:2018-01-24 14:38:10

标签: android arraylist scroll android-asynctask android-recyclerview

我正在尝试创建一个聊天应用程序,其中服务器现在只提取15条消息,因为用户滚动到顶部我想要加载更多数据并将其附加到arraylist我尝试了addonScroll侦听器但它只会被触发时如果用户滚动到顶部,用户滚动到底部而不是顶部如何获得相同的功能

  public UserAdapter(RecyclerView recyclerView, Activity activity, ArrayList<User> arrayList) {
    this.activity = activity;
    this.arrayList = arrayList;
    final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

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

            super.onScrolled(recyclerView, dx, dy);
            totalItemCount = linearLayoutManager.getItemCount();
            lastItemCount = linearLayoutManager.findLastVisibleItemPosition();
            Log.d(TAG, "onScrolled: " + lastItemCount + " " + totalItemCount);
            if (!isLoading && totalItemCount <= (lastItemCount + visibleThreshold)) {
                if (iLoadMore != null) {
                    iLoadMore.onLoadMore();
                    isLoading = true;
                }
            }
        }
    });
}

我的主要活动

    adapter.setUnLoaded();
    adapter.setiLoadMore(new ILoadMore() {
        @Override
        public void onLoadMore() {
            arrayList.add(null);//for the progressbar
            adapter.notifyItemInserted(arrayList.size() - 1);
            Log.d("LOADMORE", "onLoadMore: scrolling working");
            BackgroundTask backgroundTask1 = new BackgroundTask(getApplicationContext());
            backgroundTask1.delegate = (BackgroundTask.AsynResponse) context;
            backgroundTask1.execute("getuser");
        }
    });

1 个答案:

答案 0 :(得分:1)

recyclerViewParticulars.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0 ) {
                    // Scrolling up
                    Log.d("recyclerviewscroll", "up");



                } else if(dy <0 ){
                    // Scrolling down
                    Log.d("recyclerviewscroll", "down");

                }
            }
}