自动滚动到Horizo​​ntalScrollView

时间:2014-04-04 14:50:38

标签: android horizontal-scrolling

我正在进行自动水平滚动。所以我有15件物品。现在我想访问12项,所以我的索引是11.但我无法在索引发生时自动滚动它。

 horizontalScrollView.scrollTo(12, 0);

@Override
public void onPageSelected(int page) {
    for(int i = 0; i < holeTitle.length; i++) {
        if(i == page) {
            title[i].setTextColor(0xffffffff);
horizontalScrollView.scrollTo(12, 0);

        }
        else {
            title[i].setTextColor(0xffe0e0e0);

        }
    }

}
请专家看看。

4 个答案:

答案 0 :(得分:15)

DmRomantsov 's answer是滚动到第12个按钮的正确方法。但是,getLeft()getRight()方法返回0,因为屏幕上尚未显示布局。现在计算布局父级和子级的宽度还为时过早。要实现它,您需要在onWindowFocusChanged内进行自动滚动。

@Override
public void onWindowFocusChanged(boolean hasFocus){
    super.onWindowFocusChanged(hasFocus);
    if(hasFocus){
        // do smoothScrollTo(...);
    }
} 

但是,在Fragment中,上面的方法不起作用。我只是写了一个线索,了解这个概念。要在Fragment中具有相同的行为,您只需要执行Runnable,以便显示您的UI时间。然后,使用面向水平的LinearLayout执行此操作:

// Init variables
HorizontalScrollView mHS;
LinearLayout mLL;  

// onCreateView method
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.layout_container, container, false);

    // Find your views
    mHS = (HorizontalScrollView)view.findViewById(R.id.hscrollview);
    mLL = (LinearLayout)view.findViewById(R.id.hscrollview_container);  

    // Do a Runnable on the inflated view
    view.post(new Runnable() {
        @Override
        public void run() {
            Log.v("","Left position of 12th child = "+mLL.getChildAt(11).getLeft());
            mHS.smoothScrollTo(mLL.getChildAt(11).getLeft(), 0);
        }
    });
    return view;
}

Middle Horizo​​ntalScrollView:

你的问题是自动滚动到你的第12个孩子。但是,在下面的评论中,您要求我在HorizontalScrollView的中间自动滚动,我假设在每台设备上。您需要计算屏幕的宽度容器的总宽度以及设备宽度中显示的子项数。这是一个简单的代码:

// Auto scroll to the middle (regardless of the width screen)
view.post(new Runnable() {
    @Override
    public void run() {
        // Width of the screen
        DisplayMetrics metrics = getActivity().getResources()
                                 .getDisplayMetrics();
        int widthScreen = metrics.widthPixels;
        Log.v("","Width screen total = " + widthScreen);

        // Width of the container (LinearLayout)
        int widthContainer = mLL.getWidth();
        Log.v("","Width container total = " + widthContainer );

        // Width of one child (Button)
        int widthChild = mLL.getChildAt(0).getWidth();
        Log.v("","Width child = " + widthChild);

        // Nb children in screen    
        int nbChildInScreen = widthScreen / widthChild;
        Log.v("","Width screen total / Width child = " + nbChildInScreen);

        // Width total of the space outside the screen / 2 (= left position)
        int positionLeftWidth = (widthContainer 
                                - (widthChild * nbChildInScreen))/2;
        Log.v("","Position left to the middle = " + positionLeftWidth);

        // Auto scroll to the middle
        mHS.smoothScrollTo(positionLeftWidth, 0);

    }
});

/** 
  * Your value might be resumed by:
  *
  * int positionLeftWidth = 
  *       ( mLL.getWidth() - ( mLL.getChildAt(0).getWidth() * 
  *       ( metrics.widthPixels / mLL.getChildAt(0).getWidth() ) ) ) / 2;  
  *
**/

具有所选值的中间Horizo​​ntalScrollView:

我有点误解了真正的要求。实际上,您想要自动滚动直到选定的子视图,并在屏幕中间显示此视图 然后,我将最后int positionLeftWidth更改为所选视图相对于其父级的左侧位置一个屏幕中包含的子级数所选视图的半宽。因此,除positionLeftWidth

外,代码与上述相同
// For example the chosen value is 7

// 7th Child position left    
int positionChildAt = mLL.getChildAt(6).getLeft();

// Width total of the auto-scroll (positionLeftWidth)
int positionLeftWidth = positionChildAt - // position 7th child from left less
                ( ( nbChildInScreen    // ( how many child contained in screen
                  * widthChild ) / 2 ) // multiplied by their width ) divide by 2
                + ( widthChild / 2 );  // plus ( the child view divide by 2 )

// Auto-scroll to the 7th child
mHS.smoothScrollTo(positionLeftWidth, 0);  

然后,无论getChildAt()方法中的值是什么,无论宽度屏幕如何,您都将在屏幕中间选择(在您的情况下)按钮。

答案 1 :(得分:3)

尝试

horizontalScrollView.smoothScrollTo(horizontalScrollView.getChildAt(11).getRight(),0);
第一个patameter - X coord,第二个 - Y.
抵消:

public final void smoothScrollBy (int dx, int dy)

绝对:

public final void smoothScrollTo (int x, int y)

答案 2 :(得分:2)

尝试horizontalScrollView.smoothScrollBy(12, 0);

答案 3 :(得分:0)

试试这是工作代码。位置将是您要滚动的位置

  final HorizontalScrollView mHorizontalScrollView = (HorizontalScrollView) .findViewById(R.id.horizontalScrollView);
        mHorizontalScrollView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mHorizontalScrollView.scrollTo(position, 0);
                mHorizontalScrollView.smoothScrollBy(1200, 0);
            }
        },100);