Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SmoothScroll到isn';t平滑_Android_Scrollview_Smooth Scrolling - Fatal编程技术网

Android SmoothScroll到isn';t平滑

Android SmoothScroll到isn';t平滑,android,scrollview,smooth-scrolling,Android,Scrollview,Smooth Scrolling,我正在尝试滚动到ScrollView中的给定布局。我的XML基本上由一个实现各种RelativeLayout的ScrollView组成,我想通过编程滚动到一个给定的ScrollView 我的活动的onCreate方法中有以下代码: // Defining my view sv = (ScrollView)findViewById(R.id.reward_scroll_view); // Get Layout's id from intent Bundle extras = getIntent(

我正在尝试滚动到ScrollView中的给定布局。我的XML基本上由一个实现各种RelativeLayout的ScrollView组成,我想通过编程滚动到一个给定的ScrollView

我的活动的onCreate方法中有以下代码:

// Defining my view
sv = (ScrollView)findViewById(R.id.reward_scroll_view);

// Get Layout's id from intent
Bundle extras = getIntent().getExtras();
if (extras != null) {
    idToScroll = extras.getInt("uiToScroll");
    sv.post(new Runnable() {
        public void run() {

            // Scroll to the passed element
            RelativeLayout layout = (RelativeLayout) findViewById(idToScroll);
            sv.smoothScrollTo(0, layout.getTop());
        }
    });
}

“自动滚动”到一个给定的锚正在工作,但没有“平滑”效果,只是一个原始的滚动到布局。我缺少什么?

您正在覆盖视图的post方法

滚动视图在UI线程或Post中无法正常工作


多亏了Aamir的评论,我解决了问题,下面是我的最终代码:

    Bundle extras = getIntent().getExtras();
    if (extras != null) {

        // Get Layout's id from intent
        idToScroll = extras.getInt("uiToScroll");
        layout = (RelativeLayout) findViewById(idToScroll);

        // Start 1 second timer
        new CountDownTimer(1000, 20) {      
             public void onTick(long millisUntilFinished) {
                 // Nothing...
             }

             // When over, start smoothScroll
             public void onFinish() {  
                 sv.smoothScrollTo( 0, layout.getTop() );  
             }      
        }.start(); 
    }

我的视图可能需要一些延迟才能正确显示滚动效果。

谢谢,但我该怎么办?如果我删除runnable,卷轴甚至不适用。再次感谢链接,但我认为这与我的情况无关,因为我没有动态加载我的内容。我的ScrollView的所有组件都是硬编码的,我只想要一个平滑的锚定滚动效果到一个特定的组件。然后使用倒计时。它将显示你的滚动运动平稳我很好奇你在哪里做这件事?onResume()?