Cardroll ScrollX / ScrollY равны 0 в ScrollView - PullRequest
4 голосов
/ 13 мая 2019

Я пытаюсь прокрутить до вершины CardView, который является потомком ScrollView, через вертикальную LinearLayout.

Вот макет:

<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 / ScrollY0. Я предполагаю, что это нормальное поведение, так как CardView не является прямым потомком ScrollView.Вот почему я использую верхнюю x-координату CardView.

Ответы [ 2 ]

2 голосов
/ 20 мая 2019

Если вы не знаете 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);
        }
    });
}

Проблема в том, что мы получаем верхнее значение карты, прежде чем оно нарисовано, что приводит к 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));
0 голосов
/ 14 мая 2019

Если вы хотите реализовать прокрутку CardView в Linearlayout, я предлагаю вам использовать android.support.v7.widget.RecyclerView для этого, вот пример в github, вы можете посмотреть:

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...