0

我正在实施无尽的回收者视图。数据从服务器变得很好,向下滚动并显示它。但是在滚动时,它会一次又一次地自动回到顶部。我实施了许多解决方案,但不起作用。这是一些代码。

    private int postNo = 15;

public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    if (!lastTime) {
        if (dy > 0) { // check for scroll down
            visibleItemCount = layoutManager.getChildCount();
            totalItemCount = layoutManager.getItemCount();
            pastVisibleItems = ((GridLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
            if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                postNo = postNo + 15;
                getDataFromUrl(postNo);
            }
        }
    }
} 

我想要的是当我向下滚动应用程序时会增加项目数量并且它应该滚动正常。

         public void getDataFromUrl(int postNoLocal) {
    final int postLocal = postNoLocal;
    call = apiInterface.getGames("androidapp/get_apps?per_page="+postLocal+"&type=12");
    call.enqueue(new Callback<List<Example>>() {
        @Override
        public void onResponse(Call<List<Example>> call, Response<List<Example>> response) {
            if (response.isSuccessful()) {
                retrylayout.setVisibility(View.GONE);
                    jsonData = response.body();
                adapter = new RecyclerAdapter_apps(jsonData, getContext());
                    recyclerView.setAdapter(adapter);
                    refreshLayout.setRefreshing(false);
                    spinner.setVisibility(myFragmentView.INVISIBLE);
            }
        }

        @Override
        public void onFailure(Call<List<Example>> call, Throwable t) {
            runFun();
            retrylayout.setVisibility(View.VISIBLE);
            retry.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    spinner.setVisibility(View.VISIBLE);
                    retrylayout.setVisibility(View.GONE);
                    getDataFromUrl(postNo);
                }
            });
            if (jsonData!=null){
                jsonData.clear();
            }
            refreshLayout.setRefreshing(false);
            recyclerView.removeAllViewsInLayout();
            spinner.setVisibility(myFragmentView.INVISIBLE);
        }
    });
}
4

1 回答 1

0

试试这种方式。希望它会奏效。

 if(adapter ==null){
    adapter = new RecyclerAdapter_apps(jsonData, getContext());
    recyclerView.setAdapter(adapter);
 }else{
    homeAdapter.notifyDataSetChanged();
 }
于 2017-12-12T12:52:51.957 回答