diff options
author | Mario Bertschler <bmario@google.com> | 2017-11-15 11:53:32 -0800 |
---|---|---|
committer | Mario Bertschler <bmario@google.com> | 2017-11-15 16:19:34 -0800 |
commit | 510a64d099aa0c87244388c314927cfacf373ce3 (patch) | |
tree | 62441bacece2721be777cc92f81c5da463d6faaf /src/com/android/launcher3 | |
parent | ac9408a5cd7744a8dbc66a61114665ab6e4051de (diff) | |
download | android_packages_apps_Trebuchet-510a64d099aa0c87244388c314927cfacf373ce3.tar.gz android_packages_apps_Trebuchet-510a64d099aa0c87244388c314927cfacf373ce3.tar.bz2 android_packages_apps_Trebuchet-510a64d099aa0c87244388c314927cfacf373ce3.zip |
Adding a sliding tab strip to the custom tab layout.
Bug: 68713881
Change-Id: Ib0873482f80903611f183ccf430185f40a292f8e
Diffstat (limited to 'src/com/android/launcher3')
-rw-r--r-- | src/com/android/launcher3/allapps/AllAppsContainerView.java | 5 | ||||
-rw-r--r-- | src/com/android/launcher3/views/SlidingTabStrip.java | 107 |
2 files changed, 111 insertions, 1 deletions
diff --git a/src/com/android/launcher3/allapps/AllAppsContainerView.java b/src/com/android/launcher3/allapps/AllAppsContainerView.java index 271a133ea..efd7b97df 100644 --- a/src/com/android/launcher3/allapps/AllAppsContainerView.java +++ b/src/com/android/launcher3/allapps/AllAppsContainerView.java @@ -58,8 +58,8 @@ import com.android.launcher3.util.ComponentKey; import com.android.launcher3.util.ComponentKeyMapper; import com.android.launcher3.util.ItemInfoMatcher; import com.android.launcher3.util.PackageUserKey; -import com.android.launcher3.util.Themes; import com.android.launcher3.util.TransformingTouchDelegate; +import com.android.launcher3.views.SlidingTabStrip; import java.util.HashMap; import java.util.List; @@ -471,6 +471,7 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource, } private void setupWorkProfileTabs() { + final SlidingTabStrip tabs = findViewById(R.id.tabs); mViewPager.setAdapter(new TabsPagerAdapter()); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @@ -478,6 +479,7 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource, @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + tabs.updateIndicatorPosition(position, positionOffset); if (positionOffset == 0 && !mVisible || positionOffset > 0 && mVisible) { mVisible = positionOffset == 0; for (int i = 0; i < mAH.length; i++) { @@ -490,6 +492,7 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource, @Override public void onPageSelected(int pos) { + tabs.updateTabTextColor(pos); mFloatingHeaderHandler.setMainActive(pos == 0); applyTouchDelegate(); if (mAH[pos].recyclerView != null) { diff --git a/src/com/android/launcher3/views/SlidingTabStrip.java b/src/com/android/launcher3/views/SlidingTabStrip.java new file mode 100644 index 000000000..45c626146 --- /dev/null +++ b/src/com/android/launcher3/views/SlidingTabStrip.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.launcher3.views; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.AttributeSet; +import android.view.View; +import android.widget.Button; +import android.widget.LinearLayout; + +import com.android.launcher3.R; +import com.android.launcher3.util.Themes; + +public class SlidingTabStrip extends LinearLayout { + + private final Paint mSelectedIndicatorPaint; + private int mSelectedIndicatorHeight; + private int mIndicatorLeft = -1; + private int mIndicatorRight = -1; + private int mSelectedPosition = -1; + private float mSelectionOffset; + + public SlidingTabStrip(@NonNull Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + setOrientation(HORIZONTAL); + setWillNotDraw(false); + mSelectedIndicatorPaint = new Paint(); + mSelectedIndicatorPaint.setColor(Themes.getAttrColor(context, android.R.attr.colorAccent)); + mSelectedIndicatorHeight = getResources() + .getDimensionPixelSize(R.dimen.all_apps_tabs_indicator_height); + } + + public void updateIndicatorPosition(int position, float positionOffset) { + mSelectedPosition = position; + mSelectionOffset = positionOffset; + updateIndicatorPosition(); + } + + public void updateTabTextColor(int pos) { + for (int i=0; i < getChildCount(); i++) { + Button tab = (Button) getChildAt(i); + tab.setSelected(i == pos); + } + } + + @Override + protected void onLayout(boolean changed, int l, int t, int r, int b) { + super.onLayout(changed, l, t, r, b); + updateTabTextColor(0); + updateIndicatorPosition(0, 0); + } + + private void updateIndicatorPosition() { + final View tab = getChildAt(mSelectedPosition); + int left, right; + + if (tab != null && tab.getWidth() > 0) { + left = tab.getLeft(); + right = tab.getRight(); + + if (mSelectionOffset > 0f && mSelectedPosition < getChildCount() - 1) { + // Draw the selection partway between the tabs + View nextTitle = getChildAt(mSelectedPosition + 1); + left = (int) (mSelectionOffset * nextTitle.getLeft() + + (1.0f - mSelectionOffset) * left); + right = (int) (mSelectionOffset * nextTitle.getRight() + + (1.0f - mSelectionOffset) * right); + } + } else { + left = right = -1; + } + + setIndicatorPosition(left, right); + } + + private void setIndicatorPosition(int left, int right) { + if (left != mIndicatorLeft || right != mIndicatorRight) { + mIndicatorLeft = left; + mIndicatorRight = right; + invalidate(); + } + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + canvas.drawRect(mIndicatorLeft, getHeight() - mSelectedIndicatorHeight, + mIndicatorRight, getHeight(), mSelectedIndicatorPaint); + } +}
\ No newline at end of file |