当TTS(文本到语音)播放所有可用列表项时,我如何在recyclerVIEw中显示/隐藏imageVIEw,一一播放!
活动方法
-此方法称为带有循环的r(不起作用,没有错误,但根本没有给出我的预期输出
int position=0; public voID convertTextToSpeech() { Multiples multiples1=items.get(position); for (Multiples item : items) { text = item.first + " " + item.getSecond() + " Za " + item.getResult() + "."; tts.speak(text,TextToSpeech.QUEUE_ADD,null); boolean speakingEnd = tts.isspeaking(); if (speakingEnd) { Toast.makeText(getApplicationContext(),"Speaking...."+position,Toast.LENGTH_SHORT).show(); multiples1.setimage_show(true); mAdapter.notifyItemChanged(position); } else { Toast.makeText(getApplicationContext(),"Done...."+position,Toast.LENGTH_SHORT).show(); } position++; } }
下面是完整的代码,以供您进一步了解,
首先,我在recyclerVIEw中显示了所有项目.之后,我在具有for循环到TTS的方法中调用显示的项目来播放每行(列表项目).我现在面临的问题是,由于TTS正在读取每个recyclerVIEw项目,因此无法显示imageVIEw.
预期的输出是每行项目的TTS播放textVIEw时,应同时显示imageVIEw(#image1)
尝试码更新
displayActivityResultAdapter.java
...... ....... int position = 0; public voID convertTextToSpeech() { Multiples multiples1=items.get(position); for (Multiples item : items) { text = item.first + " " + item.getSecond() + " Za " + item.getResult() + "."; tts.speak(text,null); boolean speakingEnd = tts.isspeaking(); // Toast.makeText(getApplicationContext(),Toast.LENGTH_SHORT).show(); // multiples1.setimage_show(true); // mAdapter.notifyItemChanged(position); // Log.i("values","------------------------------------------Row value-"+item.getFirst()+" X "+item.getSecond()+",position="+position); //------------------------ MyAdapter m= (MyAdapter) mAdapter; m.updateItem(item,position); position++; } }}
MyAdapter.java
..... ..... // Replace the contents of a vIEw (invoked by the layout manager) @OverrIDe public voID onBindVIEwHolder(MyVIEwHolder holder,int position) { // - get element from your dataset at this position // - replace the contents of the vIEw with that element if (holder instanceof MyAdapter.MyVIEwHolder) { final MyAdapter.MyVIEwHolder vIEw = (MyAdapter.MyVIEwHolder) holder; Multiples p = items.get(position); vIEw.name.setText(p.first + " X " + p.getSecond() + "= "+p.getResult()); if(position>0) { if (p.image_show) { vIEw.image1.setVisibility(VIEw.VISIBLE); vIEw.image.setVisibility(VIEw.INVISIBLE); } else { vIEw.image1.setVisibility(VIEw.INVISIBLE); vIEw.image.setVisibility(VIEw.VISIBLE); } } } } public voID updateItem(Multiples newItem,int pos) { Log.i("values","-----In updateItem Log----Row value-"+newItem.getFirst()+" X "+newItem.getSecond()+",position="+pos); items.set(pos,newItem); //update passed value in your adapter's data structure Log.e("msg","-----items.get(pos)------------->"+items.get(pos).getFirst()+" X " +items.get(pos).getSecond()); notifyItemChanged(pos,newItem); } // Return the size of your dataset (invoked by the layout manager) @OverrIDe public int getItemCount() { return items.size(); }}
最佳答案您的主要失败是缺少TTS侦听器.没有它,您将不知道何时应该更新RecyclerVIEw.侦听器可以如下所示:
class MyListener extends UtteranceProgressListener { @OverrIDe public voID onStart(String utteranceID) { int currentIndex = Integer.parseInt(utteranceID); mMainAdapter.setCurrentposition(currentIndex); handler.post(new Runnable() { @OverrIDe public voID run() { mMainAdapter.notifyDataSetChanged(); } }); } @OverrIDe public voID onDone(String utteranceID) { int currentIndex = Integer.parseInt(utteranceID); mMainAdapter.setCurrentposition(-1); handler.post(new Runnable() { @OverrIDe public voID run() { mMainAdapter.notifyDataSetChanged(); } }); if (currentIndex < data.size() - 1) { playSound(currentIndex + 1); } } @OverrIDe public voID onError(String utteranceID) { } }
我创建了一个测试项目,以演示如何实现. Here可以看到它是如何工作的. Here是我的github存储库. 总结
以上是内存溢出为你收集整理的java-无法与TTS同步在recyclerVIew中显示imageView 全部内容,希望文章能够帮你解决java-无法与TTS同步在recyclerVIew中显示imageView 所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)