Reputation: 13555
I want to display recyclerview items after every 10 seconds. For instance, I have 8 items in my arraylist. Initially I want to display 3 items, then after waiting for 10 seconds first three visible items will disappear and next 3 items will show. how to achieve it ?
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);
}
Upvotes: 1
Views: 817
Reputation: 1379
You should absolutely avoid using Thread.sleep()
on the UI thread to keep it unblocked.
Blocking the UI thread will stop event dispatch (input, drawing events) and to a user the application appears to be frozen. If you end up blocking the UI thread for >5 seconds then the user will see an Application Not Responding
dialog which makes for a very poor UX.
For your use-case you could use a Handler
to dispatch events to the UI thread in a delayed manner
Here's a rough skeleton that might be resourceful:
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
}
}
Upvotes: 1
Reputation: 1105
Interesting scenario. I think instead of adding time delays in adapter you should do that stuff in your class where you are passing data to adapter. Try to load first 3 items which you want to show then use handler to make delay of 10 seconds.
Like this :
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);
After this you need to clear your list and add next 3 items in list and notify your adapter that data has been updated. With this approach you can achieve your use case.
Upvotes: 2