为什么smoothScroll在添加兄弟RelativeLayout后不能立即工作

Raf*_*Raf 1 android scroll relativelayout

我在ScrollView(SV)中有父RelativeLayout(RL ).在父RL中我有一个孩子也是RL.我想将另一个子RL添加到父RL并向下滚动屏幕高度

RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(this.width,
   this.height);
lparams.addRule(RelativeLayout.BELOW, first_child.getId());
new_layout.setLayoutParams(lparams);
rl.addView(new_layout);
int current_pos = sv.getScrollY();
sv.smoothScrollTo(0,current_pos + this.height);
Run Code Online (Sandbox Code Playgroud)

新的子RL已成功添加到父级,但smootScrollTo方法无法正常工作.就像addView()方法是异步工作一样,当方法smoothScrollTo调用时,父RL还没有包含新的子RL.

如何在父母中添加新的子RL并立即向下滚动屏幕?

use*_*165 6

试试这个:

RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(this.width,
       this.height);
    lparams.addRule(RelativeLayout.BELOW, first_child.getId());
    new_layout.setLayoutParams(lparams);
    rl.addView(new_layout);
    int height = this.height;
        sv.post(new Runnable() {
                @Override
                public void run() {
                    int current_pos = sv.getScrollY();
                    sv.smoothScrollTo(0,current_pos + height);
                }
            });
Run Code Online (Sandbox Code Playgroud)