Twilie2012
Twilie2012

Reputation: 79

smoothScrollTo does not work correctly

I have a problem with smoothScrollTosmoothScrollTo in my android application. When I click on the button for the first time, the text shows but the scrolling does not work, but when I click the button for the second time, everything is OK. here is my code. Any ideas? Thank you

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);
            }
        }
    });

here is the focusOnView method

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

and here is part of 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>

Upvotes: 0

Views: 3667

Answers (3)

Sysphus
Sysphus

Reputation: 204

You should do operations in UI thread. So you can create a Handler in your UI thread instead of another thread. Do the smoothScrollTo() method in the handleMessage() method from Handler. Try like this:

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;
        }
    }

};

Click the button:

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));
        }
    }
});

Upvotes: 0

Twilie2012
Twilie2012

Reputation: 79

i solved my problem by replacing code in focusOnView method with code:

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

Upvotes: 1

Decoy
Decoy

Reputation: 1597

If you initialize your boolean

isShowMore = false; 

then it should work as expected.

Upvotes: 1

Related Questions