Я пытаюсь центрировать представление с плавной прокруткой в горизонтальном прокрутке, когда элемент/представление щелкается/вкладывается.
Вот моя последняя попытка добиться этого.
Код:
public void scrollCenterBarItem(DrinkCategoryView toBarItem) {
int endPos = (int) toBarItem.getView().getX();
int halfWidth = (int) toBarItem.getView().getWidth() / 2;
barsScroller.smoothScrollTo(endPos + halfWidth, 0);
}
Я не считаю необходимым включать весь код, но DrinkCategoryView — это класс, в котором расположены представления элементов horizontalScrollView. переменная barScroller — это HorizontalScrollView.
А вот часть xml, где horizontalScrollView и linearlayout, содержащие представления:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bars_scroller"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:scrollbars="none">
<LinearLayout
android:id="@+id/drinksCategoryList"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</HorizontalScrollView>
</RelativeLayout>
And here is the item xml layout of the HorizontalScrollView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/drinkCategoryItem">
<com.ivankocijan.magicviews.views.MagicTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/drinkCategoryItemTextView"
android:textSize="18sp"
app:typeFace="@string/opensans_regular"
android:textColor="@color/black"
android:padding="16dp" />
</LinearLayout>
Надеюсь, вы сможете понять, чего я пытаюсь достичь, и, возможно, поможете мне в правильном направлении.
Любая помощь будет оценена.