简体   繁体   English

使用不同的 ScrollY 将 TextView 动态添加到 FlipperView

[英]Dynamically add TextViews to FlipperView with varying ScrollY

I am developing an app and have begun learning Java a few weeks ago.我正在开发一个应用程序,几周前开始学习 Java。 At this point, there's something that I'm trying to get to work, but I can't figure out what's going wrong.在这一点上,我正试图开始工作,但我不知道出了什么问题。

So the problem is as follows.所以问题如下。 I have a FlipperView in which I want to dynamically add TextViews between which I should be able to navigate.我有一个 FlipperView,我想在其中动态添加我应该能够在它们之间导航的 TextView。 These TextViews have the same content and layout, the only difference between them is their " ScrollY " attribute.这些 TextView 具有相同的内容和布局,它们之间的唯一区别是它们的“ ScrollY ”属性。 I want the first TextView to have a ScrollY of 0 so that the text can be seen from its beginning.我希望第一个 TextView 的 ScrollY 为 0,以便可以从头看到文本。 Then, I want the next TextView to have a ScrollY of 0 + x, where x is the height of the TextView (which is originally defined as match_parent).然后,我希望下一个 TextView 的 ScrollY 为 0 + x,其中 x 是 TextView(最初定义为 match_parent)的高度。 The next one should have a ScrollY of 2x, etc.下一个应该有 2x 的 ScrollY,等等。

I've tried many things to get this work.我已经尝试了很多事情来完成这项工作。 First I managed to dynamically get the height of the TextView using a.post.首先,我设法使用 a.post 动态获取 TextView 的高度。 For now, I can change the ScrollY of the first TextView. For additional views, I'm using a for a loop.现在,我可以更改第一个 TextView 的 ScrollY。对于其他视图,我正在使用 for 循环。 At this point, TextViews are properly created and I can navigate between them with the FlipperView.此时,已正确创建 TextView,我可以使用 FlipperView 在它们之间导航。 However, the SetScrollY function doesn't work inside the for loop .但是, SetScrollY function 在 for loop 中不起作用 I've tried to feed it with an arbitrary integer instead of the dynamic height of the TextView without success, so I guess the function is not working at all in this case.我试图用任意 integer 而不是 TextView 的动态高度来喂它但没有成功,所以我猜 function 在这种情况下根本不起作用。 Here's my code (the number of loops is fixed for now):这是我的代码(循环数现在是固定的):

public int formatText(Context context, ViewFlipper textFlipper) {
    int nbOfViews = 1;
    final int[] viewMaxHeight = {0};
    final int[] lineSpacing = {0};

    textContent = context.getResources().getString(R.string.dummyText);

    final TextView flipperView = new TextView(context, null, R.style.flipperText);

    flipperView.setText(textContent);

    textFlipper.addView(flipperView);

    flipperView.post(new Runnable() {
        @Override
        public void run() {
            viewMaxHeight[0] = flipperView.getHeight();
            lineSpacing[0] = Math.round(flipperView.getLineSpacingExtra());
        }
    });

    for(int i = 0; i < 2; i++) {
        TextView flipperViewExtra = new TextView(context, null, R.style.flipperText);
        flipperViewExtra.setText(textContent);
        flipperViewExtra.setScrollY(viewMaxHeight[0] * nbOfViews + lineSpacing[0]);

        textFlipper.addView(flipperViewExtra);

        nbOfViews++;
    }

    return nbOfViews;
}

Any help is appreciated, thanks!任何帮助表示赞赏,谢谢!

You are working with THREAD and thread tends to optimize the performance by caching the using variables.您正在使用 THREAD 并且线程倾向于通过缓存使用变量来优化性能。 Hence any chance OUTSIDE the thread it won't "see" the change in memory. To coerce the thread to look at the change in memory you have to declare the variables to volatile.因此,在线程之外的任何机会它都不会“看到”memory 中的变化。要强制线程查看 memory 中的变化,您必须将变量声明为 volatile。 Example例子

final volatile int[] viewMaxHeight = {0};
final volatile int[] lineSpacing = {0};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM