Cardview ScrollX / ScrollY在ScrollView中为0

时间:2019-05-13 14:40:53

标签: android xamarin.android android-scrollview

我试图通过垂直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.ScrollX / ScrollY为0。我认为这是正常现象,因为CardView不是ScrollView的直接子代。这就是为什么我使用CardView的顶部x坐标。

2 个答案:

答案 0 :(得分:2)

如果不了解Xamarin,但让我解释一下本机Android中可能的解决方案。

如果我们按照以下方式将您的代码放入onCreate方法中,则根本不会滚动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CardView dailyHabits = findViewById(R.id.cardViewDH);
    final int y = dailyHabits.getTop();
    final ScrollView scrollView = findViewById(R.id.scroll_view);
    scrollView.post(new Runnable() {
        @Override
        public void run() {
            scrollView.smoothScrollTo(0, y);
        }
    });
}

问题是我们在绘制Cardview之前获得了CardView的最高值,结果为0。您可以在布局中添加树形观察者,也可以如下更新代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ScrollView scrollView = findViewById(R.id.scroll_view);
    final CardView dailyHabits = findViewById(R.id.cardViewDH);
    dailyHabits.post(new Runnable() {   // could be scrollView
        @Override
        public void run() {
            int y = dailyHabits.getTop();
            scrollView.smoothScrollTo(0, y);
        }
    });
}

现在,您应该在应用启动时将所需的滚动显示到所需卡片视图的顶部。

对于您的代码,可以将其转换为相同的效果:

CardView dailyHabits = FindViewById<CardView>(Resource.Id.cardViewDH);
ScrollView scrollView = FindViewById<ScrollView>(Resource.Id.scroll_view);
scrollView.Post(() => scrollView.SmoothScrollTo(0, dailyHabits.Top));

答案 1 :(得分:0)

如果要在Linearlayout中实现滚动CardView,建议您使用 android.support.v7.widget.RecyclerView ,这是github中的示例,您可以看一下:

https://github.com/xamarin/monodroid-samples/tree/master/android5.0/RecyclerViewer