简体   繁体   中英

Stop animation of Recyclerview when load more data

i'm tying to load more data each time user reached at the bottom of Recyclerview by get data of next page , using this :

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            if (!recyclerView.canScrollVertically(1) && isEnd) {
                page++;
                isEnd = false;
                new fetchData().execute(page);
            }
        }
    });

And everything works fine , but i have a problem with animation , each time i load more data Recyclerview disappears and shows.

here's my rest of code :

   public class fetchData extends AsyncTask<Integer, List<Wallpaper>, List<Wallpaper>> {
        Document doc = null;
        ArrayList<Wallpaper> urls = new ArrayList<>();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected List<Wallpaper> doInBackground(Integer... integers) {

            try {
                doc = Jsoup.connect(URLSource+"/page/" + integers[0]).get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Elements newsHeadlines = doc.select(".thumbnail");
            for (Element headline : newsHeadlines) {
                String thumb = headline.select("img").attr("src");
                String title = headline.select("img").attr("alt");
                Wallpaper wallpaperInfo = new Wallpaper();
                URL = URL.replace("/wallpapers/", "/walls/");
                wallpaperInfo.setThumbnails(URL);
                wallpaperInfo.setTitle(title);
                urls.add(wallpaperInfo);
            }
            return urls;
        }

        @Override
        protected void onPostExecute(List<Wallpaper> wallpapers) {
            super.onPostExecute(wallpapers);
            setAdapterForRecyclerView(wallpapers);
            isEnd = true;
        }

    }

private void setAdapterForRecyclerView(List<Wallpaper> wallpapers) {

        if (myAdapter == null) {
            myAdapter = new MyAdapter(wallpapers, getActivity(), new RecyclerViewClickListener() {
                @Override
                public void onClick(View view, Wallpaper wallpaper) {
                    Intent intent = new Intent(getActivity(), FullScreen.class);
                    intent.putExtra("img", wallpaper.getThumbnails());
                    if (wallpaper.getTitle().isEmpty()) {
                        intent.putExtra("title", "Unknown");
                    } else {
                        intent.putExtra("title", wallpaper.getTitle());
                    }
                    startActivity(intent);
                    /* TODO: FULLSCREEN ACTIVITY HERE*/
                }

                @Override
                public void onClick(View view, Category categories) {

                }
            });
            recyclerView.setAdapter(myAdapter);

        } else {
            myAdapter.getItems().addAll(wallpapers);
            myAdapter.notifyDataSetChanged();

        }

        progressbar.setVisibility(View.GONE);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (!recyclerView.canScrollVertically(1) && isEnd) {
                    page++;
                    isEnd = false;
                    new fetchData().execute(page);
                }
            }
        });

    }

hope you guys help me to stop this animation , i just want to load more data without any animation

i think that your problem is in the calling of setAdapterForRecyclerView as there you may create new adapter instance and set this new adapter to the recyclerview again which lead to this behavior. i recommend to append the new list of wallpapers objects to the recyclerview list then call 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