Java 如何在水平滚动视图中使用平滑滚动将视图居中
当元素/视图被单击/选项卡时,我尝试在水平滚动视图中使用平滑滚动将视图居中 下面是我实现这一目标的最新尝试 代码: 我认为没有必要包含所有代码,但DrinkCategoryView是一个类,horizontalScrollView的项目视图位于该类中。 名为BarsScroll的变量是horizontalScrollView 下面是xml部分,其中包含视图的horizontalScrollView和linearlayout是:Java 如何在水平滚动视图中使用平滑滚动将视图居中,java,android,horizontalscrollview,smooth-scrolling,Java,Android,Horizontalscrollview,Smooth Scrolling,当元素/视图被单击/选项卡时,我尝试在水平滚动视图中使用平滑滚动将视图居中 下面是我实现这一目标的最新尝试 代码: 我认为没有必要包含所有代码,但DrinkCategoryView是一个类,horizontalScrollView的项目视图位于该类中。 名为BarsScroll的变量是horizontalScrollView 下面是xml部分,其中包含视图的horizontalScrollView和linearlayout是: <HorizontalScrollView
<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>
下面是HorizontalScrollView的项目xml布局:
<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>
我希望你能理解我正在努力实现的目标,也许能帮助我朝着正确的方向前进
任何帮助都将不胜感激。尝试以下方法:
public void scrollCenterBarItem(DrinkCategoryView toBarItem) {
int endPos = (int) toBarItem.getView().getX();
int halfWidth = (int) toBarItem.getView().getWidth() / 2;
barsScroller.smoothScrollTo(endPos + halfWidth - barScroller.getWidth() / 2, 0);
}
如果这仍然不起作用,您可能希望以图像的形式发布失败的测试结果。尝试以下方法:
public void scrollCenterBarItem(DrinkCategoryView toBarItem) {
int endPos = (int) toBarItem.getView().getX();
int halfWidth = (int) toBarItem.getView().getWidth() / 2;
barsScroller.smoothScrollTo(endPos + halfWidth - barScroller.getWidth() / 2, 0);
}
如果这仍然不起作用,您可能希望以图像的形式发布失败的测试结果。非常感谢,它工作得非常好:D接受和+1非常感谢,它工作得非常好:D接受和+1