我如何在 RecyclerView 中显示/隐藏 imageView,因为 TTS(文本到语音)逐一播放所有可用的列表项!
Activity 方法 -这个方法被称为 r with Loop(不工作,没有错误,但根本没有给出我预期的输出
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 播放每一行(列表项)。我现在面临的问题是 imageView 没有显示,因为 TTS 正在读取每个 recyclerView 项目。
预期输出是每当 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++;
}
}
}
我的适配器.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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55516858/