简体   繁体   中英

on scrolled to add items in recyclerview always comes to top automatically when i scroll down

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

Try this way.Hope it will work.

 if(adapter ==null){
    adapter = new RecyclerAdapter_apps(jsonData, getContext());
    recyclerView.setAdapter(adapter);
 }else{
    homeAdapter.notifyDataSetChanged();
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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