[英]How to center views with a smooth scroll in an horizontalScrollView
I am trying to center a view with a smooth scroll in an horizontalScrollView, when the element/view is clicked/tabbed. 当元素/视图被单击/设置选项卡时,我试图将水平滚动视图中的平滑滚动居中放置视图。
Here's my latest attempt to achieve this. 这是我实现这一目标的最新尝试。
Code: 码:
public void scrollCenterBarItem(DrinkCategoryView toBarItem) {
int endPos = (int) toBarItem.getView().getX();
int halfWidth = (int) toBarItem.getView().getWidth() / 2;
barsScroller.smoothScrollTo(endPos + halfWidth, 0);
}
I don't find it neccesary to include all the code, but DrinkCategoryView is a class where the items views of the horizontalScrollView are located. 我认为没有必要包含所有代码,但是DrinkCategoryView是一个类,其中包含horizontalScrollView的项目视图。 the variable called barsScroller is the horizontalScrollView. 名为barsScroller的变量是horizontalScrollView。
And here is the xml part where the horizontalScrollView and linearlayout, containing the views is: 这是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: 这是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>
I hope you can understand what i am trying to achieve and maybe help me in the right direction. 希望您能理解我正在努力实现的目标,并可能在正确的方向帮助我。
Any help will be greatly appreciated. 任何帮助将不胜感激。
Try something like this: 尝试这样的事情:
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);
}
If this still not working, you may want to post your failed test result in the form of images. 如果仍然无法解决问题,则可能要以图像形式发布失败的测试结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.