自动滚动到Horizo​​ntalScrollView

Rah*_*wat 7 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);

        }
    }

}
Run Code Online (Sandbox Code Playgroud)

请专家一起来看看.

fll*_*llo 15

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

@Override
public void onWindowFocusChanged(boolean hasFocus){
    super.onWindowFocusChanged(hasFocus);
    if(hasFocus){
        // do smoothScrollTo(...);
    }
} 
Run Code Online (Sandbox Code Playgroud)

但是,在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;
}
Run Code Online (Sandbox Code Playgroud)

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;  
  *
**/
Run Code Online (Sandbox Code Playgroud)

具有所选值的Middle 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);  
Run Code Online (Sandbox Code Playgroud)

然后,无论getChildAt()方法中的值如何,无论宽度屏幕如何,您将始终在屏幕中间选择(在您的情况下)按钮.