smoothScrollTo无法正常工作

时间:2014-02-28 17:10:17

标签: android animation scroll

我的android应用程序中有smoothScrollTo smoothScrollTo的问题。当我第一次单击该按钮时,文本显示但滚动不起作用,但是当我第二次单击该按钮时,一切正常。这是我的代码。有任何想法吗?谢谢

showMoreBt = (Button) findViewById(R.id.buttonMore);
    showMoreBt.setVisibility(View.VISIBLE);
    LinearLayout layout = (LinearLayout) findViewById(R.id.animatedLL);
    layout.setVisibility(View.VISIBLE);

    scrollView = (ScrollView)findViewById(R.id.scrollView1);
    showMoreBt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (!isShowMore) {
                isShowMore = true;
                showMoreBt.setText("Show more");
                Animation slideDown = AnimationUtils.loadAnimation(getBaseContext(), R.anim.slide_down);    
                webviewMore.setVisibility(View.VISIBLE);
                webviewMore.startAnimation(slideDown);
                focusOnView();

            } else {
                isShowMore = false;
                showMoreBt.setText("Show less"); 
                webviewMore.setVisibility(View.GONE);
            }
        }
    });

这里是focusOnView方法

private final void focusOnView(){
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            int position = webviewTitle.getHeight() + webview.getHeight();
            scrollView.smoothScrollTo(0, position);
        }
    });
}

这里是xml的一部分     

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/buttonMore"
            android:layout_width="fill_parent"
            android:layout_margin="10dip"
            android:layout_height="50dip"
            android:layout_gravity="center"
            android:textStyle="bold"
            android:background="@color/light_green"
            android:drawableLeft="@drawable/arrow_down"
            android:drawableRight="@drawable/arrow_down"
            android:visibility="gone"
            android:text="Show more" />


        <LinearLayout
            android:id="@+id/animatedLL"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <WebView
                android:id="@+id/webviewMore"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
             />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

3 个答案:

答案 0 :(得分:1)

如果你初始化你的布尔

isShowMore = false; 

然后它应该按预期工作。

答案 1 :(得分:1)

我通过使用代码替换focusOnView方法中的代码来解决我的问题:

ViewTreeObserver vto = scrollView.getViewTreeObserver();
                 vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                      public void onGlobalLayout() {
                          scrollView.scrollTo(0, View.FOCUS_DOWN);
                      }
                 });

答案 2 :(得分:0)

您应该在UI线程中执行操作。因此,您可以在UI线程中创建一个Handler而不是另一个线程。在Handler的handleMessage()方法中执行smoothScrollTo()方法。 试试这样:

private static final int SHOW_MORE = 0X10;
private static final int SHOW_LESS = 0X20;

Handler mHandler = new Handler(){

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case SHOW_LESS:
            showMoreBt.setText("Show more");
            Animation slideDown = AnimationUtils.loadAnimation(getBaseContext(), R.anim.slide_down);    
            webviewMore.setVisibility(View.VISIBLE);
            webviewMore.startAnimation(slideDown);
            int position = webviewTitle.getHeight() + webview.getHeight();
            scrollView.smoothScrollTo(0, position);
            break;

        case SHOW_MORE:
            showMoreBt.setText("Show less"); 
            webviewMore.setVisibility(View.GONE);
            break;

        default:
            break;
        }
    }

};

点击按钮:

showMoreBt = (Button) findViewById(R.id.buttonMore);
showMoreBt.setVisibility(View.VISIBLE);
LinearLayout layout = (LinearLayout) findViewById(R.id.animatedLL);
layout.setVisibility(View.VISIBLE);

scrollView = (ScrollView)findViewById(R.id.scrollView1);
showMoreBt.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        if (!isShowMore) {
            isShowMore = true;
            mHandler.sendMessage(mHandler.obtainMessage(SHOW_LESS));
        } else {
            isShowMore = false;
            mHandler.sendMessage(mHandler.obtainMessage(SHOW_MORE));
        }
    }
});