【发布时间】:2019-05-13 14:40:52
【问题描述】:
我正在尝试通过垂直 LinearLayout 滚动到 CardView 的顶部,该 CardView 是 ScrollView 的子级。
这是布局:
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/scroll_view" android:fillViewport="true">
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background_color">
<!-- Quality of Life -->
<android.support.v7.widget.CardView android:id="@+id/cardViewQOL" android:layout_margin="8sp" android:layout_marginBottom="8sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" cardview:cardElevation="4sp"
cardview:cardCornerRadius="5sp" cardview:contentPaddingTop="10sp" cardview:contentPaddingBottom="10sp" cardview:contentPaddingRight="10sp" cardview:contentPaddingLeft="10sp">
<include layout="@layout/qualityoflife" android:layout_width="fill_parent" android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
<!-- Goals -->
<android.support.v7.widget.CardView android:id="@+id/cardViewGoals" android:layout_margin="8sp" android:layout_marginBottom="8sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" cardview:cardElevation="4sp"
cardview:cardCornerRadius="5sp" cardview:contentPaddingTop="10sp" cardview:contentPaddingBottom="10sp" cardview:contentPaddingRight="10sp" cardview:contentPaddingLeft="10sp">
<include layout="@layout/goals" android:layout_width="fill_parent" android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
<!-- Daily routines -->
<android.support.v7.widget.CardView android:id="@+id/cardViewDH" android:layout_margin="8sp" android:layout_marginBottom="8sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" cardview:cardElevation="4sp"
cardview:cardCornerRadius="5sp" cardview:contentPaddingTop="10sp" cardview:contentPaddingBottom="10sp" cardview:contentPaddingRight="10sp" cardview:contentPaddingLeft="10sp">
<include layout="@layout/dailyroutines" android:layout_width="fill_parent" android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
<!-- Weekly routines -->
<android.support.v7.widget.CardView android:id="@+id/cardViewWH" android:layout_margin="8sp" android:layout_marginBottom="8sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" cardview:cardElevation="4sp"
cardview:cardCornerRadius="5sp" cardview:contentPaddingTop="10sp" cardview:contentPaddingBottom="10sp" cardview:contentPaddingRight="10sp" cardview:contentPaddingLeft="10sp">
<include layout="@layout/weeklyroutines" android:layout_width="fill_parent" android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
<!-- Monthly routines -->
<android.support.v7.widget.CardView android:id="@+id/cardViewMH" android:layout_margin="8sp" android:layout_marginBottom="4sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" cardview:cardElevation="4sp"
cardview:cardCornerRadius="5sp" cardview:contentPaddingTop="10sp" cardview:contentPaddingBottom="10sp" cardview:contentPaddingRight="10sp" cardview:contentPaddingLeft="10sp">
<include layout="@layout/monthlyroutines" android:layout_width="fill_parent" android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView android:id="@+id/cardViewExperiences" android:layout_margin="8sp" android:layout_marginBottom="8sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"
cardview:cardElevation="4sp" cardview:cardCornerRadius="5sp" cardview:contentPaddingTop="10sp" cardview:contentPaddingBottom="10sp" cardview:contentPaddingRight="10sp" cardview:contentPaddingLeft="10sp">
<include layout="@layout/experiences" android:layout_width="fill_parent" android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView android:id="@+id/cardViewConfigNeeded" android:layout_margin="8sp" android:layout_marginBottom="8sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"
cardview:cardElevation="4sp" cardview:cardCornerRadius="5sp" cardview:contentPaddingTop="10sp" cardview:contentPaddingBottom="10sp" cardview:contentPaddingRight="10sp" cardview:contentPaddingLeft="10sp" android:visibility="gone">
<include layout="@layout/configneeded" android:layout_width="fill_parent" android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
这是我的代码:
CardView dailyHabits = FindViewById<CardView>(Resource.Id.cardViewDH);
int y = dailyHabits.Top;
ScrollView scrollView = FindViewById<ScrollView>(Resource.Id.scroll_view);
scrollView.Post(() => scrollView.SmoothScrollTo(0, y));
结果是滚动在下一个cardview的顶部结束,这很奇怪……
我注意到 CardView.ScrollX/ScrollY 为 0。我想这是正常行为,因为 CardView 不是 ScrollView 的直接子项。这就是我使用 CardView 的顶部 x 坐标的原因。
【问题讨论】:
标签: android xamarin.android android-scrollview