如何按时间间隔显示recyclerview项目?
How to show recyclerview items in time interval?
我想每 10 秒显示一次 recyclerview 项目。例如,我的 arraylist 中有 8 个项目。最初我想显示 3 个项目,然后等待 10 秒后,前三个可见项目将消失,接下来的 3 个项目将显示。如何实现?
private void addList(){
ItemAdapter itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.cachua);
itemAdapter.setText("Tomato");
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.bo);
itemAdapter.setText("butter");
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.cam);
itemAdapter.setText("oranges");
mList.add(itemAdapter);
mAdapter = new ListAdapter(mList, this);
mRecycleview.setAdapter(mAdapter);
mRecycleview.setLayoutManager(new LinearLayoutManager(this));
mAdapter.notifyDataSetChanged();
Log.d("anhtt","mlist : " +mList.size());
mList.clear();
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.quaxoai);
itemAdapter.setText("mango");
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.dau);
itemAdapter.setText("strawberry");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mAdapter = new ListAdapter(mList, this);
mRecycleview.setAdapter(mAdapter);
mRecycleview.setLayoutManager(new LinearLayoutManager(this));
mAdapter.notifyDataSetChanged();
Log.d("anhtt","mlist : " +mList.size());
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.tao);
itemAdapter.setText("Apple");
mList.add(itemAdapter);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.oi);
itemAdapter.setText("guava fruit");
mList.add(itemAdapter);
}
有趣的场景。我认为与其在适配器中添加时间延迟,不如在将数据传递给适配器的 class 中执行这些操作。尝试加载要显示的前 3 个项目,然后使用处理程序延迟 10 秒。
像这样:
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do your code to clear list and adding next 3 items and notify your adapter
}
}, 10000);
在此之后,您需要清除您的列表并在列表中添加接下来的 3 个项目,并通知您的适配器数据已更新。通过这种方法,您可以实现您的用例。
您绝对应该避免在 UI 线程上使用 Thread.sleep()
以保持其畅通无阻。
阻塞 UI 线程将停止事件分派(输入、绘图事件),并且对用户而言应用程序似乎被冻结。如果您最终阻塞 UI 线程超过 5 秒,那么用户将看到一个 Application Not Responding
对话框,这会导致非常糟糕的用户体验。
对于您的用例,您可以使用 Handler
以延迟的方式将事件分派到 UI 线程
这是一个粗略的框架,可能会很有用:
public final class ScratchActivity extends AppCompatActivity {
private static final int INTERVAL_IN_MILLIS = 10000;
private static final int STEP = 3;
private Handler mHandler;
// Backing list of models to update the recycler view
private final List<Object> mItems = new ArrayList<>();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler(Looper.getMainLooper());
/*
Set content view for activity etc.
Initialize recycler view, layout manager and adapter
*/
updateItems(0);
}
// Loop periodically to derive a sublist of items to update the recycler view adapter
private void updateItems(int index) {
if (index >= mItems.size()) return;
notifyRecyclerView(mItems.subList(index, Math.min(index + STEP, mItems.size())));
mHandler.postDelayed(() -> updateItems(index + STEP), INTERVAL_IN_MILLIS);
}
private void notifyRecyclerView(List<Object> data) {
// Replace data within the recycler view
}
}
我想每 10 秒显示一次 recyclerview 项目。例如,我的 arraylist 中有 8 个项目。最初我想显示 3 个项目,然后等待 10 秒后,前三个可见项目将消失,接下来的 3 个项目将显示。如何实现?
private void addList(){
ItemAdapter itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.cachua);
itemAdapter.setText("Tomato");
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.bo);
itemAdapter.setText("butter");
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.cam);
itemAdapter.setText("oranges");
mList.add(itemAdapter);
mAdapter = new ListAdapter(mList, this);
mRecycleview.setAdapter(mAdapter);
mRecycleview.setLayoutManager(new LinearLayoutManager(this));
mAdapter.notifyDataSetChanged();
Log.d("anhtt","mlist : " +mList.size());
mList.clear();
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.quaxoai);
itemAdapter.setText("mango");
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.dau);
itemAdapter.setText("strawberry");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mAdapter = new ListAdapter(mList, this);
mRecycleview.setAdapter(mAdapter);
mRecycleview.setLayoutManager(new LinearLayoutManager(this));
mAdapter.notifyDataSetChanged();
Log.d("anhtt","mlist : " +mList.size());
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.tao);
itemAdapter.setText("Apple");
mList.add(itemAdapter);
mList.add(itemAdapter);
itemAdapter = new ItemAdapter();
itemAdapter.setImage(R.drawable.oi);
itemAdapter.setText("guava fruit");
mList.add(itemAdapter);
}
有趣的场景。我认为与其在适配器中添加时间延迟,不如在将数据传递给适配器的 class 中执行这些操作。尝试加载要显示的前 3 个项目,然后使用处理程序延迟 10 秒。
像这样:
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do your code to clear list and adding next 3 items and notify your adapter
}
}, 10000);
在此之后,您需要清除您的列表并在列表中添加接下来的 3 个项目,并通知您的适配器数据已更新。通过这种方法,您可以实现您的用例。
您绝对应该避免在 UI 线程上使用 Thread.sleep()
以保持其畅通无阻。
阻塞 UI 线程将停止事件分派(输入、绘图事件),并且对用户而言应用程序似乎被冻结。如果您最终阻塞 UI 线程超过 5 秒,那么用户将看到一个 Application Not Responding
对话框,这会导致非常糟糕的用户体验。
对于您的用例,您可以使用 Handler
以延迟的方式将事件分派到 UI 线程
这是一个粗略的框架,可能会很有用:
public final class ScratchActivity extends AppCompatActivity {
private static final int INTERVAL_IN_MILLIS = 10000;
private static final int STEP = 3;
private Handler mHandler;
// Backing list of models to update the recycler view
private final List<Object> mItems = new ArrayList<>();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler(Looper.getMainLooper());
/*
Set content view for activity etc.
Initialize recycler view, layout manager and adapter
*/
updateItems(0);
}
// Loop periodically to derive a sublist of items to update the recycler view adapter
private void updateItems(int index) {
if (index >= mItems.size()) return;
notifyRecyclerView(mItems.subList(index, Math.min(index + STEP, mItems.size())));
mHandler.postDelayed(() -> updateItems(index + STEP), INTERVAL_IN_MILLIS);
}
private void notifyRecyclerView(List<Object> data) {
// Replace data within the recycler view
}
}