android – Auto滚动到Horizo​​ntalScrollView

android – Auto滚动到Horizo​​ntalScrollView,第1张

概述我正在做自动水平滚动.所以我有15件物品.现在我想访问12项,所以我的索引是11.但我无法在索引发生时自动滚动它. horizontalScrollView.scrollTo(12, 0);@Overridepublic void onPageSelected(int page) { for(int i = 0; i < holeTitle.length; i++) { 我正在做自动水平滚动.所以我有15件物品.现在我想访问12项,所以我的索引是11.但我无法在索引发生时自动滚动它.
horizontalscrollview.scrollTo(12,0);@OverrIDepublic 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内进行自动滚动.
@OverrIDepublic voID onWindowFocusChanged(boolean hasFocus){    super.onWindowFocusChanged(hasFocus);    if(hasFocus){        // do smoothScrollTo(...);    }}

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

// Init variableshorizontalscrollview mHS;linearLayout mLL;  // onCreateVIEw method@OverrIDepublic 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个孩子.但是,在下面的评论中,您要求我在Horizo​​ntalScrollVIEw的中间自动滚动,我假设在每个设备上.您需要计算屏幕的宽度,容器的总宽度以及设备宽度内显示的子项数.这是一个简单的代码:

// 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;    ***/

具有所选值的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 childmHS.smoothScrollTo(positionleftWIDth,0);

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

总结

以上是内存溢出为你收集整理的android – Auto滚动到Horizo​​ntalScrollView全部内容,希望文章能够帮你解决android – Auto滚动到Horizo​​ntalScrollView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1137857.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存