用动态创造突破性的新标题
Creating breaking new title with dynamic
我正在创建一个突发新闻应用程序,我想无限滚动 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
初始化为输入大小的两倍等),但主要规则是检测 [= 中的特定事件17=] 并增加适配器的大小。
另见 h22's answer。
找到了一些 article(在 kotlin 中)实现了不同的方法,如果您感兴趣的话。
我正在创建一个突发新闻应用程序,我想无限滚动 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
初始化为输入大小的两倍等),但主要规则是检测 [= 中的特定事件17=] 并增加适配器的大小。
另见 h22's answer。
找到了一些 article(在 kotlin 中)实现了不同的方法,如果您感兴趣的话。