加载更多数据时停止Recyclerview的动画

时间:2018-10-29 16:19:48

标签: java android animation android-recyclerview

我希望每次用户到达Recyclerview底部时通过获取下一页的数据来加载更多数据,使用

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

一切正常,但动画存在问题,每次加载更多数据Recyclerview都会消失并显示。

这是我剩下的代码:

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

    }

希望你们能帮助我停止播放此动画,我只想加载更多没有任何动画的数据

1 个答案:

答案 0 :(得分:1)

我认为您的问题出在setAdapterForRecyclerView的调用中,因为您可能会在其中创建新的适配器实例,并将此新适配器再次设置为recyclerview,从而导致此行为。 我建议将新的壁纸对象列表添加到recyclerview列表,然后调用notifydatasetchanged