ogaclejapan / SmartTabLayout

A custom ViewPager title strip which gives continuous feedback to the user when scrolling
Apache License 2.0
7.09k stars 1.34k forks source link

Always not in Center ,help #64

Open chongbo2013 opened 9 years ago

chongbo2013 commented 9 years ago

Always not in Center ,help

chongbo2013 commented 8 years ago

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 commented 8 years ago

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

msdalp commented 8 years ago

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.

ogaclejapan commented 8 years ago

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>
engkan2kit commented 7 years ago

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.