Automatically scrolling host tabs using horizontal scrolling - software

I am developing an example application with TabHost that contains a HorizontalScrollView . Here is my problem:

  • I have more than 10 tabs, when I click on any tab, should it set gravity as the center? How can I deal with this problem.

Thanks in advance!

+4
source share
2 answers

This will be associated with some calculations.

Basically you need to scroll to a position to center the element.

 int scrollX = (button.getLeft() - (screenWidth/2))+(button.getWidth()/2); hScrollView.scrollTo(scrollX,0); 

Where

 button -> It is the button you are trying to center. screenWidth -> Width of the screen. int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); hScrollView -> It is the horizontal scrollview. 

But even with this method, you will not be able to center the final elements, since scrollviews will not go beyond it.

+5
source
 public void onTabChanged(String tabId) { for (int i = 0; i < tablist.length; i++) { if (tabId.contentEquals(tablist[i])) { HorizontalScrollView scroll = (HorizontalScrollView)findViewById(R.id.horScrollView); scroll.smoothScrollTo(((int)(getResources().getDimensionPixelSize(R.dimen.tab_width) * (i))), 0); } } 

I used: implements OnTabChangeListener.

0
source

All Articles