自动滚动到HorizontalScrollView

拉胡尔·拉瓦特(Rahul Rawat)

我正在执行自动水平滚动。所以我有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);

        }
    }

}

请高手看看。

弗洛

DmRomantsov的答案是滚动到第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;
}

中间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()方法中的值如何,以及屏幕的宽度如何,您总是会在屏幕中间找到(在您的情况下)选择的按钮。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章