我在我的android應用程序中使用smoothScrollTo時遇到了問題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>
什麼是isShowMore變量?它有什麼價值? – Giacomoni
我編輯了我的問題... – Twilie2012