问题
I'm using HorizontalScrollView for show header in my app,I also need to scroll header left-right on clicking on left-right button.I have used some code that is working successfully in scrolling right-to-left but i'm unable to scroll left-to-right.please suggest me how to solve this
My code for scroll right-to-left
horiScrollView.scrollBy(20, 20);
please suggest me how to solve this problem.Thanks
回答1:
Make the first number negative. the right number doesn't do anything (it's vertical change). First number is how much to move horizontally - so positive numbers mean go right, negative numbers mean go left.
回答2:
I had exactly the same problem - I have a button of language and by that the horiScrollView
sliding is set.
I do that with a new class that extends horizontalScrollView, and there in the onMesuare
method I set the side by calling horiScrollView.scrollBy(20, 20)
, like you did.
In this method, I check if the language is changed, and if so, scroll to the right position I want. This is the only method I found and it works perfectly.
In that way I also separated the horizontalScrollView
code from the entire screen, the horizontalScrollView
in my app in controlling all the screen, so I prefer to make a distinction between the activity and the horizontalScrollView
.
回答3:
This code work well for me:
mHorScrollView.post(new Runnable() {
@Override
public void run() {
if (rightButton) {
mHorScrollView.smoothScrollTo(20, 0);
} else {
mHorScrollView.smoothScrollTo(-20, 0);
}
}
});
来源:https://stackoverflow.com/questions/12842915/scroll-horizontalscrollview-left-right