SmartTabLayout icon indicating copy to clipboard operation
SmartTabLayout copied to clipboard

Always not in Center ,help

Open chongbo2013 opened this issue 9 years ago • 5 comments

Always not in Center ,help

chongbo2013 avatar Aug 24 '15 06:08 chongbo2013

private void scrollToTab(int tabIndex, int positionOffset) { final int tabStripChildCount = tabStrip.getChildCount(); if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) { return; }

View selectedTab = tabStrip.getChildAt(tabIndex);
if (selectedTab == null) {
  return;
}

final boolean isLayoutRtl = Utils.isLayoutRtl(this);

if (tabStrip.isIndicatorAlwaysInCenter()) {
  View firstTab = tabStrip.getChildAt(0);
  int x;
  if (isLayoutRtl) {
    int first = Utils.getWidth(firstTab) + Utils.getMarginEnd(firstTab);
    int selected = Utils.getWidth(selectedTab) + Utils.getMarginEnd(selectedTab);
    x = Utils.getEnd(selectedTab) - Utils.getMarginEnd(selectedTab) - positionOffset;
    x -= (first - selected) / 2;
  } else {
    int first = Utils.getWidth(firstTab) + Utils.getMarginStart(firstTab);
    int selected = Utils.getWidth(selectedTab) + Utils.getMarginStart(selectedTab);
    x = Utils.getStart(selectedTab) - Utils.getMarginStart(selectedTab) + positionOffset;
    x -= (first - selected) / 2-Utils.getWidth(selectedTab)/2; //Fix always not in Center.

  }

  scrollTo(x, 0);
  return;

}

chongbo2013 avatar Sep 24 '15 07:09 chongbo2013

update this code: x -= (first - selected) / 2-Utils.getWidth(selectedTab)/2; //Fix always not in Center.

chongbo2013 avatar Sep 24 '15 07:09 chongbo2013

I saw this problem as well. Tried a basic application with with only RelativeLayout and had no problem at all. However if you try to use in a mixed layout group (relative inside linear etc.) it fails to center the selected tab.

As @xufeifandj mentioned before it should be a problem with decreasing the x position by half of the selected tab which is x -= (first - selected) / 2-Utils.getWidth(selectedTab)/2. I will try to run some tests and update this later.

msdalp avatar Oct 09 '15 11:10 msdalp

I tried a complex layout, but it does not occur in my environment : ( Please tell me the steps to reproduce. someone...


<android.support.v4.widget.DrawerLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.v7.widget.Toolbar
      android:id="@id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/primary"
      android:minHeight="?attr/actionBarSize"
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
      app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
      />

    <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      >

      <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <com.ogaclejapan.smarttablayout.SmartTabLayout
          android:id="@id/viewpagertab"
          android:layout_width="match_parent"
          android:layout_height="48dp"
          android:paddingBottom="4dp"
          app:stl_defaultTabTextColor="@color/white"
          app:stl_defaultTabTextMinWidth="120dp"
          app:stl_indicatorAlwaysInCenter="true"
          app:stl_indicatorColor="@color/accent"
          app:stl_indicatorCornerRadius="1.5dp"
          app:stl_indicatorInterpolation="smart"
          app:stl_indicatorThickness="3dp"
          app:stl_underlineColor="@color/transparent"
          />

        <android.support.v4.view.ViewPager
          android:id="@id/viewpager"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          />

      </LinearLayout>

    </RelativeLayout>

  </LinearLayout>

</android.support.v4.widget.DrawerLayout>

ogaclejapan avatar Nov 22 '15 10:11 ogaclejapan

I am having the same problem now. In my previous implementation of my app, the always on center works. I was using a single activity app with multiple fragments. The activity loaded the pager fragment the the pager fragment loaded the pages as child fragments.

In my current implementation, I am instead using multiple activities but using the same layout for each activity for the Navigation and toolbar. Only the content of the framelayout is changed. Now the always on center does not work.

engkan2kit avatar Jul 13 '17 19:07 engkan2kit