如何在向上滚动时清除 RecyclerView 中的列表

Posted

技术标签:

【中文标题】如何在向上滚动时清除 RecyclerView 中的列表【英文标题】:How to clear list in RecyclerView on ScrollUp 【发布时间】:2021-06-28 15:36:41 【问题描述】:

我想用 Rest Api 和 Retrofit 制作应用程序。当用户拉下 RecyclerView 时,我想加载随机帖子,但当我加载随机帖子时,我做到了但一件很烦人的事情。它显示在前 10 个帖子之后,我的意思是在主页的 10 个帖子之后,但我想清除旧帖子并显示第 1 行的随机帖子。

我用于向上滚动的代码

 else if (!recyclerView.canScrollVertically(-1) && dy < 0)
            


                        yourURL1 = baseURL + baseModel + "&orderby=rand&page=" + pageNo++;
                        getRetrofit1();



            

改造

try 
        List<Model> list = new ArrayList<>();
        Call<List<WPPost>>  call = service.getPostInfo( yourURL);
        call.enqueue(new Callback<List<WPPost>>() 
            @Override
            public void onResponse(Call<List<WPPost>> call, Response<List<WPPost>> response) 
                Log.d("==>>", " response "+ response.body());
                mListPost = response.body();
                progressBar.setVisibility(View.GONE);
                if (response.body() != null) 
                    for (int i = 0; i < response.body().size(); i++ ) 
                        Log.d("==>>", " title " + response.body().get(i).getTitle().getRendered() + " " +
                                response.body().get(i).getId());
                        String tempdetails = String.valueOf((html.fromHtml(response.body().get(i).getExcerpt().getRendered().toString())));
                        tempdetails = tempdetails.replace("<p>", "");
                        tempdetails = tempdetails.replace("</p>", "");
                        tempdetails = tempdetails.replace("[&hellip;]", "");
                        list.add(new Model(String.valueOf((Html.fromHtml(response.body().get(i).getTitle().getRendered()))),
                                tempdetails, response.body().get(i).getPostViews().toString(),
                                response.body().get(i).getImages().getMedium(),response.body().get(i).getContent().getRendered()));

                    
                    if (loading)
                        loading = false;
                        adapter.showHideProgress(false);
                    
                    adapter.addItemsToList(list);
                    progressBar.setVisibility(View.GONE);
                 else 
                    progressBar.setVisibility(View.GONE);
                
            
            @Override
            public void onFailure(Call<List<WPPost>> call, Throwable t) 
            
        );

    catch (Exception exception)
        Log.d("tisha==>>"," "+exception.getLocalizedMessage());
    

在适配器中

 void addItemsToList(List<Model> newItems)
    if (dataset.isEmpty())
        dataset.addAll(newItems);
        notifyDataSetChanged();
        Log.d("tisha==>>","First time List size = "+dataset.size());
    else 
        int lastItemPosition = dataset.size() -1;
        Log.d("tisha==>>","Old list size = "+dataset.size()+ "Last Item position= "+lastItemPosition);
        dataset.addAll(newItems);
        Log.d("tisha==>>","Update List size = "+dataset.size());
        notifyItemRangeInserted(lastItemPosition,newItems.size());
    

如何清除列表?

【问题讨论】:

【参考方案1】:
once this list is set you are adding items to list , you have to remove elements first notify item ranged removed then add new and update on notify item inserted   
 
void addItemsToList(List<Model> newItems)
 if (dataset.isEmpty())
 dataset.addAll(newItems); 
notifyDataSetChanged(); 
Log.d("tisha==>>","First time List size = "+dataset.size());
 
else 
 
int lastItemPosition = dataset.size() -1; 
Log.d("tisha==>>","Old list size = "+dataset.size()+ "Last Item position= "+lastItemPosition); dataset.addAll(newItems);
 Log.d("tisha==>>","Update List size = "+dataset.size()); notifyItemRangeInserted(lastItemPosition,newItems.size());  

【讨论】:

如何删除元素?你能帮忙吗? 只需使用 list.clear,然后通知适配器然后添加您的元素 addItemsToList 内 不适合我,你能提供代码吗?

以上是关于如何在向上滚动时清除 RecyclerView 中的列表的主要内容,如果未能解决你的问题,请参考以下文章

插入数据时Recyclerview自动向上滚动

向上滚动recyclerview android时项目正在更新?

滚动时如何为 recyclerview 项目设置动画?

键盘打开时相应向上滚动 RecyclerView

在RecyclerView列表滚动的时候显示或者隐藏Toolbar

在 RecyclerView 中滚动时不可见的项目变得可见