java-无法与TTS同步在recyclerVIew中显示imageView
作者:互联网
当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, TextToSpeech.QUEUE_ADD, null);
boolean speakingEnd = tts.isSpeaking();
// Toast.makeText(getApplicationContext(), "Speaking...."+position, 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 one rror(String utteranceId) {
}
}
我创建了一个测试项目,以演示如何实现. Here可以看到它是如何工作的. Here是我的github存储库.
标签:android-recyclerview,text-to-speech,java,android 来源: https://codeday.me/bug/20191210/2105298.html