Android RecyclerView-在for循环中逐个更新项目imageView可见性
我正在从for循环调用Android RecyclerView-在for循环中逐个更新项目imageView可见性,android,android-recyclerview,Android,Android Recyclerview,我正在从for循环调用notifyItemChanged(position)以设置ImageViewVisibility=inVisible,但在for循环结束后,所有图像视图都会立即显示。而我希望一次显示/隐藏一个imageView。 我需要做什么来解决这个问题 当前输出: 当for循环结束时,所有图像视图同时可见。(我是说 OnBindviewHolder方法仅在for循环结束后调用` 预期输出: 对于为每个索引执行的循环,我想逐个显示/隐藏每一行的imageView(我的意思是OnBind
notifyItemChanged(position)
以设置ImageViewVisibility=inVisible
,但在for
循环结束后,所有图像视图都会立即显示。而我希望一次显示/隐藏一个imageView。
我需要做什么来解决这个问题
当前输出:
当for循环结束时,所有图像视图同时可见。(我是说
OnBindviewHolder方法仅在for
循环结束后调用`
预期输出:
对于为每个索引执行的循环,我想逐个显示/隐藏每一行的imageView(我的意思是OnBindviewHolder
方法应该在调用每个for
循环时调用)
我尝试过的:
我尝试了notifyItemChanged(pos);
,notifyDataSetChanged();
,notifyItemInserted(pos);
,但都没有帮助我获得预期的输出。我也尝试了,但仍然是相同的输出
活动
private List<Multiples> items = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_result);
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use a linear layout manager
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
// specify an adapter (see also next example)
items.addAll(DataGenerator.getPeopleData(this, of, value));
mAdapter = new MyAdapter(items);
recyclerView.setAdapter(mAdapter);
//----------now give few seconds untill all default data is loaded in the recyclerView----------------
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//--------------Now call for loop on every 2 seconds so that we can hide or show ImageViews in each 2 seconds
MyAdapter m= (MyAdapter) mAdapter;
for (Multiples item : items) {
Multiples multiples1=items.get(items.indexOf(item));
multiples1.setImageShow(true); // set true to hide loop
Log.i("ms","----------in activity--------"+items.indexOf(item));
m.updateItem(item,items.indexOf(item)); // forward each row to adapter to take effect
try { // sleep for 2 seconds so that we can see the effect of above code(hide/show imageView for this row index items.indexOf(item)
Log.i("s","##################### going to sleep #######################"+items.indexOf(item) );
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, 2500);
}
您的问题是,您在尝试更改所有可见性时阻塞了主线程。因此,尽管您尝试睡眠以在操作之间设置延迟,但前面的操作无法执行,因为线程被阻塞。换句话说,您正在排队等待所有操作,但在释放主线程之前,这些操作无法生效线程和系统可以处理它们,然后同时发生
public void run() {
// THIS IS HAPPENING ON THE MAIN THREAD - ANDROID CAN'T CONTINUE DRAWING
// UNTIL THIS METHOD FINISHES
//--------------Now call for loop on each 2 seconds so that we can hide or show ImageViews in each 2 seconds
MyAdapter m= (MyAdapter) mAdapter;
for (Multiples item : items) {
Multiples multiples1=items.get(items.indexOf(item));
multiples1.setImageShow(true); // set true to hide loop
Log.i("ms","----------in activity--------"+items.indexOf(item));
m.updateItem(item,items.indexOf(item)); // forward each row to adapter to take effect
try { // sleep for 2 seconds so that we can see the effect of above code(hide/show imageView for this row index items.indexOf(item)
Log.i("s","##################### going to sleep #######################"+items.indexOf(item) );
// THIS SLEEP IS BLOCKING THE MAIN THREAD - ANDROID CAN'T DRAW
// UNTIL THE OUTER FUNCTION IS DONE
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} // THE END OF THE RUN BLOCK - NOW ANDROID CAN RUN THE INSTRUCTIONS THAT
// HAVE BEEN QUEUED UP, WHICH WILL APPEAR INSTANT
您可以做的一件事是在for循环中启动runnables,就像您已经在使用main函数一样
public void run() {
//--------------Now call for loop on each 2 seconds so that we can hide or show ImageViews in each 2 seconds
MyAdapter m= (MyAdapter) mAdapter;
int index = 0;
for (Multiples item : items) {
// DO NOT SLEEP TO NOT BLOCK THE MAIN THREAD - SCHEDULE WORK FOR LATER INSTEAD
handler.postDelayed(new Runnabled() {
Multiples multiples1=items.get(items.indexOf(item));
multiples1.setImageShow(true); // set true to hide loop
// OTHER LOGIC FOR THIS ITEM HERE
}, 2000 * index++); // SCHEDULE EACH ITEM TO BE 2 SECONDS LATER THAN THE PREVIOUS
}
} // THE LOOP FINISHES INSTANTLY AND DOES NOT BLOCK THE MAIN THREAD
通过这种方式,您可以计划所有更新在您想要的时间发生,并立即释放主线程。稍后,在计划的时间,主线程可以自由处理指令并根据需要制作动画
希望这有帮助!why-ve vote,请写一篇评论谢谢,它根据问题进行。现在,我要写的是,如果我想让TTS阅读每行项目标题,那么现在我想播放基于发言(文本到演讲)的每段文字,而不是时间延迟完成,&一旦TTS播放一行,我就想显示/隐藏图像视图。所以一旦TTS移动到下一行,我就想显示/隐藏下一行的图像视图等。示例:请查看此屏幕截图:我建议创建一个全新的问题(您可以链接到此问题作为参考)而不是试图继续解决这些评论中的不同问题。谢谢!请查看此链接。您可以在这个全新的问题上帮助我:
public void run() {
//--------------Now call for loop on each 2 seconds so that we can hide or show ImageViews in each 2 seconds
MyAdapter m= (MyAdapter) mAdapter;
int index = 0;
for (Multiples item : items) {
// DO NOT SLEEP TO NOT BLOCK THE MAIN THREAD - SCHEDULE WORK FOR LATER INSTEAD
handler.postDelayed(new Runnabled() {
Multiples multiples1=items.get(items.indexOf(item));
multiples1.setImageShow(true); // set true to hide loop
// OTHER LOGIC FOR THIS ITEM HERE
}, 2000 * index++); // SCHEDULE EACH ITEM TO BE 2 SECONDS LATER THAN THE PREVIOUS
}
} // THE LOOP FINISHES INSTANTLY AND DOES NOT BLOCK THE MAIN THREAD