私は線形レイアウトのスクロールビューを持っています。線形レイアウトの中に5つのアイテム(いくつかのビューを含む線形レイアウト)を追加します。膨張したときに、スクリーンよりも大きくなります。ScrollViewの先頭に移動
画面がレンダリングされるときは、項目5(最後の項目)が画面上部に表示されます。どうやってするの?
私が試してみた:
new Handler().post(new Runnable() {
@Override
public void run() {
scrollView.smoothScrollTo(0, item.getBottom());
}
});
}
をしかし、それは動作しません。私は、私の見解でgetScrollYを()、geTop()、getBottomを()に検査しましたが、すべてが0
を返さこれは私の親のレイアウトです:
<ScrollView
android:id="@+id/suggested_menu_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/suggested_menu_meals_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
この私の項目のレイアウト:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linear_suggested_meal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="@dimen/cardview_default_radius"
android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp">
<ProgressBar
android:id="@+id/progress_suggested_meal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
style="@android:style/Widget.ProgressBar"
android:visibility="gone"
android:layout_gravity="center_vertical|center_horizontal" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/cards_padding_space">
<TextView
android:id="@+id/suggested_meal_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/black_soft"
android:text="Sugestão de café da manhã"
android:textStyle="bold" />
<TextView
android:id="@+id/suggested_meal_total_points"
style="@style/DSTextLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/suggested_meal_name"
android:layout_alignStart="@id/suggested_meal_name"
android:layout_alignLeft="@id/suggested_meal_name"
android:layout_marginTop="4dp"
android:layout_marginBottom="8dp"
android:text="@string/zero"
android:textSize="14sp"
android:textColor="@color/green_x"
android:textStyle="bold" />
<TextView
android:id="@+id/suggested_meal_suggested"
style="@style/DSTextSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/suggested_meal_total_points"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/suggested_meal_total_points"
android:layout_toEndOf="@id/suggested_meal_total_points"
android:textSize="14sp"
android:text="sugerido de "
android:textColor="@color/gray_opaque" />
<View
android:id="@+id/separator_suggested_meal"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_below="@id/suggested_meal_total_points"
android:background="@color/dividers" />
<LinearLayout
android:id="@+id/suggested_meal_food_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/separator_suggested_meal"
android:orientation="vertical"
android:layout_marginBottom="2dp"
/>
<LinearLayout
android:layout_marginTop="2dp"
android:paddingTop="3dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/suggested_meal_food_container"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:id="@+id/button_suggested_meal_next_suggestion"
style="@style/DSLinkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/button_suggested_meal_add"
style="@style/DSLinkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/horizontal_space_large"
android:layout_marginStart="20dp"
android:background="?attr/selectableItemBackground"
android:text="@string/button_meal_suggestion_label_add_suggest" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
は.scrollTo()の代わりに、.smoothScrollTo() – LoveForDroid