我製作了一個Android應用程序,其中包含帶有AppBar和NestedScrollView的CoordinatorLayout。在我的NestedScrollView中,我有一個項目列表和一個滾動到我的NestedScrollView中的項目的按鈕。在CoordinatorLayout中滾動查看只顯示部分視圖
當單擊該按鈕,Android應用程序,向下滾動到該項目,但並沒有顯示整個項目中,只有部分:http://i.stack.imgur.com/8kuq0.png
我希望類似如下:http://i.stack.imgur.com/k0A5N.png
好像爲了展示整個視圖,應用程序需要滾動的大小與AppBar的大小大致相同。如果我在下面的佈局文件中刪除滾動標誌,我會得到預期的行爲。
我該怎麼做?
更新(12/01/2016):我更新了圖片,所以它們更大。此外,正如我在Herry的回覆中所寫的,我懷疑View.requestRectangleOnScreen()是否是正確的調用方法,或者是否應該使用不同的佈局,然後使用NestedScrollView。
我的活動編碼如下:
package com.example.sij.coordinatorlayoutbug;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView itemToNavigateTo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
itemToNavigateTo = (TextView)findViewById(R.id.itemToNavigateTo);
Button navigatorButton = (Button)findViewById(R.id.navigator_button);
navigatorButton.setOnClickListener(navigateToItemListener());
}
View.OnClickListener navigateToItemListener() {
return new View.OnClickListener() {
@Override
public void onClick(final View v) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
itemToNavigateTo.requestRectangleOnScreen(
new Rect(
0,
0,
itemToNavigateTo.getWidth(),
itemToNavigateTo.getHeight()));
}
});
}
};
}
}
而且佈局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/navigator_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/scroll_to_item" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/item1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lorem_ipsum"
android:paddingBottom="20dp"/>
<!-- Items 2 to 5 omitted for brevity -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/item6"/>
<TextView
android:id="@+id/itemToNavigateTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lorem_ipsum"
android:paddingBottom="20dp"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
我只是想添加fillViewport屬性,但我得到了同樣的結果。應該指出,我不確定View.requestRectangleOnScreen是否是正確的調用方法,或者我需要使用另一個佈局而不是NestedScrollView。 –