gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:39:16 25 4
gpt4 key购买 nike

每次用户到达 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);
}
}
});

}

希望你们帮我停止这个动画,我只是想加载更多数据而不使用任何动画

最佳答案

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

关于java - 加载更多数据时停止 Recyclerview 的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53049749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com