java - 用动态创造突破性的新标题

标签 java android android-recyclerview autoscroll

我正在创建一个突发新闻应用程序,我想无限滚动 recyclerview,但它在列表大小完成后结束,所以如何自动滚动 recyclerview。

By this method the autoscroll is done but it not scroll infinite

 public void autoScrollAnother() {
    scrollCount = 0;
    final int speedScroll = 1200;
    final Handler handler = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if(scrollCount == breakingNewAdapter.getItemCount()){

                breakingNewAdapter.notifyDataSetChanged();
            }
            brakingNews.smoothScrollToPosition((scrollCount++));
            handler.postDelayed(this, speedScroll);
        }
    };
    handler.postDelayed(runnable, speedScroll);
}

And set in recyclerview like this

  layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
    brakingNews.setLayoutManager(layoutManager);
    brakingNews.setHasFixedSize(true);
    brakingNews.setItemViewCacheSize(10);
    brakingNews.setDrawingCacheEnabled(true);
    brakingNews.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
    brakingNews.setAdapter(breakingNewAdapter);

Any help it will more help full

最佳答案

这是一个没有任何监听器或编程滚动的解决方案。

当用户到达列表末尾时,增加 getItemCount 返回值。您的实际列表大小并不重要,只是您会将对 onBindViewHolder 的每次调用定向到特定项目。

试试这段代码:

public class BreakingNewsAdapter {

    private List<BreakingNews> news;
    private int adapterSize = 0;

    public void setNews(List<BreakingNews> news) {
        this.news = news;
        this.adapterSize = news.size(); // some initialization
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (this.news != null)
            return this.adapterSize;
        return 0;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BreakingNews item = news.get(position % news.size()); // the '%' will ensure you'll always have data to bind to the view holder
        
        if (position == this.adapterSize - 1) {
            this.adapterSize += news.size(); // do whatever you want here, just make sure to increment this.adapterSize's value 
            new Handler().postDelayed(() -> notifyItemInserted(position + 1), 100); // Can't notify directly from this method, so we use a Handler. May change the delay value for what works best for you.
            // notifyItemInserted will animate insertion of one item to your list. You may call to notifyDataSetChanged or notifyItemRangeInserted if you don't want the animation

            //new Handler().postDelayed(() -> notifyItemRangeInserted(position + 1, this.adapterSize - news.size()), 100);
            //new Handler().postDelayed(this::notifyDataSetChanged, 100);
        }
    }
}

您可能会注意到,当您到达列表末尾时,滚动将停止,您需要再次滚动。

如果你根本不想让滚动停止,你可以用一个非常大的数字初始化adapterSize,缺点是滚动条不会移动。

另一种选择是检查 this.adapterSize - 1 之前的值。
this.adapterSize - (news.size()/2)例子。这样,滚动会停止片刻,然后自动继续。

这里有很多选项,所以你可以玩弄数字(也许将 adapterSize 初始化为输入大小的两倍等),但主要规则是检测 中的特定事件code>onBindViewHilder 并增加适配器的大小。

另见 h22's answer .

找到一些 article (在 kotlin 中)实现了不同的方法,如果您有兴趣的话。

关于java - 用动态创造突破性的新标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56967546/

相关文章:

android - Jetpack compose 中的自动镜像

android - 使用 volley json 数据的 fragment 中的 Recyclerview?

java - 404 Java 项目中的 Apache tomcat

java.lang.OutOfMemory错误: Java heap space Exception while Parsing of Large file using ANTLR

java - Http 发布请求

android - 使用图标而不是文本滑动 View

Android:获取 HTTP 页面并抓取它

android - 如何从RecyclerView读取数据并在BottomSheet中发送

android - 如何从对话框中删除保存在 sqlite 数据库中的 recyclerview 项目?

java - 如何获取jar类来自