Auto Scroll to HorizontalScrollView

北战南征 提交于 2019-12-06 23:59:17

问题


I am doing auto-horizontal scrolling. So i have 15 items. Now i want to access at 12 item so my index is 11. But i am unable to scroll it auto when a index occur.

 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);

        }
    }

}

please expert make a look.


回答1:


DmRomantsov's answer is the right way to scroll to the 12th button. However, getLeft() and getRight() methods return 0 because the layout is not displayed yet on the screen. It is too early to calculate the width of the layout parent and children. To achieve it, you need to do your auto-scroll inside onWindowFocusChanged.

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

However, inside a Fragment, this method above will not work. I just wrote it to give a clue, to understand the concept. To have the same behaviour in Fragment, you just need to do a Runnable which lets the time to your UI to be displayed. Then, do this with a LinearLayout oriented to horizontal:

// 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 HorizontalScrollView:

Your question was to auto-scroll until your 12th child. However, in the comments below, you ask me to auto-scroll at the middle of the HorizontalScrollView, I assume on every device. You need to calculate the width of the screen, the total width of the container and how many children are displayed inside the device width. Here is a simple code:

// 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 HorizontalScrollView with chosen Value:

I have a bit misunderstand the real request. Actually, you wanted to auto-scroll until a chosen child view, and display this view at the middle of the screen.
Then, I changed the last int positionLeftWidth which refers now to the left position of the chosen view relative to its parent, the number of children contained in one screen, and the half width of the chosen view. So, the code is the same as above, except 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);  

Then, whatever the value in getChildAt() method, and whatever the width screen, you will always have the chosen (in your case) button at the middle of the screen.




回答2:


Try

horizontalScrollView.smoothScrollTo(horizontalScrollView.getChildAt(11).getRight(),0);

first patameter - X coord, second - Y.
Offset:

public final void smoothScrollBy (int dx, int dy)

Absolute:

public final void smoothScrollTo (int x, int y)



回答3:


Try horizontalScrollView.smoothScrollBy(12, 0);




回答4:


Try this is working code. position will be where you want to scroll

  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);


来源:https://stackoverflow.com/questions/22866050/auto-scroll-to-horizontalscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!