TabWidget wrapped in HorizontalScrollView does not scroll using ViewPager

I need to use TabHost instead of ActionBarTabs and make them scrollable. I wrapped my TabWidget in a HorizontalScrollView , but the HorizontalScrollView does not scroll in according to the ViewPager . I tried using scrollTo and fullScroll in several ways, but that doesn't change anything. What do I need to do for this to work correctly?

 <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <HorizontalScrollView android:id="@id/horizontalScrollView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:fillViewport="true" android:scrollbars="@null" > <TabWidget android:id="@android:id/tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" /> </HorizontalScrollView> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> <android.support.v4.view.ViewPager android:id="@id/viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> 

  @Override public void onTabChanged(String tabId) { int position = mTabHost.getCurrentTab(); mViewPager.setCurrentItem(position); mHorizontalScrollView.scrollTo(position, 0); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { mHorizontalScrollView.scrollTo(position, 0); } 
+4
source share
2 answers

The horizontal scroll view changes when you call .refreshDrawableState() after calling the .scrollTo(x,y) method.

Another thing to keep in mind is that .scrollTo(x,y) scrolls so that x is located on the left side of the screen. You may need to do some math with the coordinates of your tabs and the horizontal scroll width in order to arrange things correctly. You cannot call .scrollTo(position,0) and work with it the way you would like (if your tabs do not have a width of 1 pixel).

+5
source

ViewPager has brains for scroll behavior.

I would remove the HorizontalScrollView from your layout. The rest looks great.

Then I will look at this sample code provided by Google, which I based on the ViewPager interface, how you want to execute: http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app /FragmentTabsPager.html

You will notice that the corresponding methods that you specified above have nothing to do with scrolling, except to indicate which page to scroll. ViewPager handles smooth scrolling inside.

  @Override public void onTabChanged(String tabId) { int position = mTabHost.getCurrentTab(); mViewPager.setCurrentItem(position); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } 
-1
source

Source: https://habr.com/ru/post/1400772/


All Articles