[英]Update item count of Recyclerview
我正在嘗試在我的應用程序中實現搜索功能,但是我遇到了問題。
例如,當我搜索黃色花朵時,我發現200個結果,我將200個存儲在NumberWalls變量中,以使適配器在RecyclerView
僅顯示200個項目,並且一切正常,我的問題是當我搜索例如Google Picture時找到4個結果,當我再次搜索黃色花朵時,我在RecyclerView
上只看到4個項目,而不是200個。
private void setAdapterForRecyclerView(List<Wallpaper> wlls, int NumberWalls) {
if (myAdapter == null) {
errortv.setVisibility(View.GONE);
myAdapter = new MyAdapterSearch(wlls, getApplicationContext(), new RecyclerViewClickListener() {
@Override
public void onClick(View view, Wallpaper wallpaper) {
Intent intent = new Intent(Search.this, FullScreen.class);
intent.putExtra("img", wallpaper.getThumbnails());
intent.putExtra("tags", wallpaper.getTitle());
startActivity(intent);
}
@Override
public void onClick(View view, Category categories) {
}
}, (NumberWalls));
recyclerView.setAdapter(myAdapter);
} else {
int position = myAdapter.getItemCount();
myAdapter.getItems().addAll(wlls);
myAdapter.notifyItemRangeInserted(position, NumberWalls);
}
progressbar.setVisibility(View.GONE);
}
在適配器中:
public MyAdapterSearch(List<Wallpaper> wallpapers, Context context, RecyclerViewClickListener clickListener, int numberItem) {
this.wallpapers = wallpapers;
this.context = context;
this.mClickListener = clickListener;
this.numberItem = numberItem;
}
.
.
.
@Override
public int getItemCount() {
return Math.min(wallpapers.size(), numberItem);
}
這是我獲取NumberWalls的AsyncTask
類:
public class fetchDataSearch extends AsyncTask<Integer, List<Wallpaper>, List<Wallpaper>> {
Document doc = null;
ArrayList<Wallpaper> urlsSearch = new ArrayList<>();
String numberOnly;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected List<Wallpaper> doInBackground(Integer... integers) {
try {
doc = Jsoup.connect(getResources().getString(R.string.SearchRequest) + mSearchText.replace(" ", "+") + "&page=" + integers[0]).get();
} catch (IOException e) {
e.printStackTrace();
}
if (doc != null) {
Element noResultfound = doc.getElementsByClass("notice notice-error").first();
if (noResultfound != null) {
noResultsFoundCatched = true;
} else {
Element element = doc.select("div.center > h1").first();
numberOnly = element.text().replaceFirst(".*?(\\d+).*", "$1");
Elements newsHeadlines = doc.getElementsByClass("img-responsive");
for (Element headline : newsHeadlines) {
String thumb = headline.select("img").attr("src");
String title = headline.select("img").attr("title");
Wallpaper wallpaperInfo = new Wallpaper();
wallpaperInfo.setThumbnails(thumb);
wallpaperInfo.setTitle(title);
if (BooleanChecker.checkTagsContains(title)) {
urlsSearch.add(wallpaperInfo);
} else {
unWantedTagsFound++;
}
}
noResultsFoundCatched = false;
}
} else {
noResultsFoundCatched = true;
}
return urlsSearch;
}
@Override
protected void onPostExecute(List<Wallpaper> wallpapersSearch) {
super.onPostExecute(wallpapersSearch);
if (numberOnly != null) {
numberWallpapersPage = Integer.parseInt(numberOnly);
setAdapterForRecyclerView(wallpapersSearch, numberWallpapers);
}
}
}
我認為您可以刪除numberItem
東西,而讓getItemCount()
僅僅返回wallpapers.size()
。 因為,您要為適配器提供搜索返回的壁紙列表,所以我猜您想在列表中顯示所有壁紙。
另外,在更新適配器時,無需將搜索結果添加到項目列表中,只需將結果設置為項目即可。
myAdapter.setWallpapers(wlls);
並將方法setWallpapers()
添加到適配器:
public void setWallpapers(List<Wallpaper> wallpapers) {
this.wallpapers = wallpapers;
notifyDatasetChanged();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.