on scrolled to add items in recyclerview always co

2019-08-24 13:09发布

I am implementing endless recycler view . Data is getting fine from server aslo scroll down getting down and show it. But on scroll it automatically comes to top again and again. I implemented number of solutions but doesn't work. here is bit of code.

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

What I want is when I scroll down app increase num of items and it should scroll fine.

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

1条回答
做个烂人
2楼-- · 2019-08-24 13:43

Try this way.Hope it will work.

 if(adapter ==null){
    adapter = new RecyclerAdapter_apps(jsonData, getContext());
    recyclerView.setAdapter(adapter);
 }else{
    homeAdapter.notifyDataSetChanged();
 }
查看更多
登录 后发表回答