android - 更新 Recyclerview 的项目计数

标签 android android-recyclerview android-arrayadapter

我正在尝试在我的应用程序中实现搜索功能,但遇到问题。

例如,当我搜索黄色花朵时,我找到了 200 个结果,我将 200 个存储在 NumberWalls 变量中,以使适配器仅显示 RecyclerView 中的 200 个项目。一切正常,我的问题是,当我搜索Google图片时,我找到了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);
    }

这是 AsyncTask 类,我在其中得到NumberWalls:

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

关于android - 更新 Recyclerview 的项目计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53140294/

相关文章:

安卓菜单不显示

android - 应用程序安装期间出现 INSTALL_FAILED_SHARED_USER_INCOMPATIBLE 错误

javascript - 为什么我的 three.js 场景中的聚光灯仍然以相机视角居中,但仅在 Chrome for Android 上?

android - 使用折叠工具栏滚动到回收 View 的最后一项

android - 当recyclerview中没有任何内容时显示ItemDecoration

java - ListView 已停止

android - ImageView,更改DemoArrayAdapter以显示图像

java - 如何使 JSONObject 的 toString() 将 UTF-8 字符编码为 un​​icode,就像 PHP 的 json_encode 中一样?

android - Jetpack Compose LazyColumn 的 ItemDecoration 是什么?

android - 如何为 Recycler View 的选定项目设置颜色?