简体   繁体   English

如何在Horizo​​ntalScrollView中使用平滑滚动使视图居中

[英]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是一个类,其中包含horizo​​ntalScrollView的项目视图。 the variable called barsScroller is the horizontalScrollView. 名为barsScroller的变量是horizo​​ntalScrollView。

And here is the xml part where the horizontalScrollView and linearlayout, containing the views is: 这是xml部分,其中包含视图的horizo​​ntalScrollView和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: 这是Horizo​​ntalScrollView的项目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.

相关问题 带有视图的 Appbar 在滚动时平滑调整大小 - Appbar with views smooth resize on scroll 如何使 Horizo​​ntalScrollView 从右到左滚动 android - How to make HorizontalScrollView RIGHT to LEFT Scroll android 操作Horizo​​ntalScrollView的视图 - Manipulating views of HorizontalScrollView 如何平滑水平列表视图的滚动? - HOW TO SMOOTH THE SCROLL OF HORIZONTAL LISTVIEW? 如何将listview放入horizo​​ntalscrollview中,以便列表可以水平滚动? - how to put listview inside a horizontalscrollview so that list can scroll hoizontally? 在 Horizo​​ntalScrollView 和 BarChart 滚动之间切换 - Switching between HorizontalScrollView and BarChart scroll 以编程方式将重力设置为Horizo​​ntalScrollView的垂直居中内容 - Programmatically set gravity to vertically center content of HorizontalScrollView 包含在Horizo​​ntalScrollView中的TabWidget不会使用ViewPager滚动 - TabWidget wrapped in HorizontalScrollView doesn't scroll with ViewPager ScrollView+HorizontalScrollView+Scale 不能正确滚动 - ScrollView+HorizontalScrollView+Scale is not properly scroll 如何在 Android 上水平居中 ListPopupWindow/Other 视图? - How to center a ListPopupWindow/Other views on Android horizontally?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM