关于tts android的滚动视图

时间:2016-06-06 22:08:06

标签: java android arraylist text-to-speech onutterancecompleted

如何滚动与我的tts相关的视图(recyclerview), 香港专业教育学院看过onUtterance,但似乎只有一个开始和停止听众,所以我需要在盒子外思考,我给我的tts一个字符串来自像这样的Arraylist

 ArrayList<String> list = new ArrayList<>();

    for (int i = 0; i < SpeakRecyclerGrid.recyclerView.getChildCount(); i++) 
      {
       list.add(((EditText) SpeakRecyclerGrid.recyclerView.getChildAt(i)).getText().toString());
      }

      speakWords(words);

我正在考虑将字符串切割成多个部分并一次将其分配给TTS一个字符串并随时移动视图。我已经给了我的gridlayout管理器一个int的列数(称为列)。 数组列表在每个单词后添加一个逗号,所以我想的是

  • 找到第n /(列)逗号
  • 拆分字符串
  • 检查tts是否正在讲话并听取onUtterance onDone传递新字符串
  • 读取字符串
  • 移动视图

并继续这样做,直到没有留下任何文字并编写剩余部分%im我不知道如何做所有这些所以如果有人想要帮助感觉自由,(我想我正在看StringUtils并创建每个较小的字符串for循环并将它们传递给onUtteranceListener onDone中的tts,但我仍然有点新的android),但主要是有人有更好的方法

1 个答案:

答案 0 :(得分:0)

好吧这就是我到目前为止所做的但它可能会使用一些工作我仍然是业余爱好者,我根据屏幕的大小将字符串拆分成块,并且滚动屏幕的处理程序依赖于每个分解的字符串中的字母数量,也就是smoothscrolltoposition是滚动自定义布局管理器,滚动很慢,我有一个问题虽然在某些设备上数字列表计数单词只会达到20不确定为什么但是生病但有希望当我修复它时更新这个,所以这是我的说话和移动方法

public void speakAndMove(){

    final ArrayList<String> list = new ArrayList<>();

    SpeakGrid.recyclerView.getLayoutManager().scrollToPosition(0);

    for (int i = 0; i < SpeakRecyclerGrid.recyclerView.getChildCount(); i++) {
        list.add(((EditText) SpeakRecyclerGrid.recyclerView.getChildAt(i)).getText().toString());
    }

    Integer numOfWords = list.size();
    words = list.toString();
    Integer count = 0;
    Integer startPoint = 0;
    scrollPos = 0;
    final Integer speed = words.length() * 15;
    Integer leftOver = 0;
    final int columns = getResources().getInteger(R.integer.grid_columns);
    System.out.println(numOfWords);
    ArrayList<String> arrayList = new ArrayList<>();

    if (list.size() <= columns) {

        if (words.contains("[]")) {
            speakWords("");
        } else if (words.contains(", 's")) {
            formatString = words.replaceFirst(", 's", "'s");
            speakWords(formatString);
        } else if (words.contains(", ing")) {
            formatString = words.replaceFirst(", ing", "ing");
            speakWords(formatString);
        } else {
            speakWords(words);
        }
    }
    if (list.size()>=columns) {

        for (int i = 0; i < words.length(); i++) {

            if (words.charAt(i) == ',') {
                count++;
                if (count == columns) {

                    String ab = words.substring(startPoint, i + 1);
                    //speakWords(ab);
                    if (ab.contains(", 's")) {
                        formatString = ab.replaceFirst(", 's", "'s");
                        speakWords(formatString);
                    } else if (ab.contains(", ing")) {
                        formatString = ab.replaceFirst(", ing", "ing");
                        speakWords(formatString);
                    } else {
                        speakWords(ab);
                    }
                    startPoint = i + 1;
                    count = 0;
                    leftOver = words.length() - startPoint;
                }


                //SpeakGrid.recyclerView.getLayoutManager().scrollToPosition(scrollPos);
                System.out.println("main string"+" scroll " + scrollPos + " startpoint " + startPoint +" speed " + speed);
            }
        }
        if (leftOver > 0) {
            String ab2 = words.substring(startPoint, words.length());
            //speakWords(ab2);
            if (ab2.contains(", 's")) {
                formatString = ab2.replaceFirst(", 's", "'s");
                speakWords(formatString);
            } else if (ab2.contains(", ing")) {
                formatString = ab2.replaceFirst(", ing", "ing");
                speakWords(formatString);
            } else {
                speakWords(ab2);
            }
            //SpeakGrid.recyclerView.getLayoutManager().scrollToPosition(scrollPos);
            System.out.println("leftovers "+ leftOver + " scroll " + scrollPos + " startpoint " + startPoint +" count " + scrollPos);
            count = 0;
            //scrollPos = 0;
        }

    }

    final Handler h = new Handler();
    h.postDelayed(new Runnable() {
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity

            scrollPos = scrollPos + columns;
            SpeakGrid.recyclerView.getLayoutManager().smoothScrollToPosition(SpeakGrid.recyclerView, null ,scrollPos);
            System.out.println("position "+ scrollPos + " speed " + speed + " list size " + list.size());
            if (scrollPos < list.size())
                h.postDelayed(this,speed);
            // close this activity
        }
    }, speed);


}