From 2869157ba9b071f56691a285394500fa4c8a0a79 Mon Sep 17 00:00:00 2001 From: Fan Zhang Date: Wed, 23 Mar 2016 15:31:08 -0700 Subject: [PATCH] Display setting and support in a tab layout - DO NOT MERGE Bug: 28269035 Bug: 28139604 --- res/drawable/ic_feedback_24dp.xml | 32 ++++ res/drawable/ic_forum_24dp.xml | 30 ++++ res/drawable/ic_help_24dp.xml | 32 ++++ res/layout/dashboard_container.xml | 21 +++ res/layout/dashboard_container_header.xml | 23 +++ res/layout/sliding_tab_indicator_view.xml | 22 +++ res/layout/sliding_tab_title_view.xml | 28 +++ res/layout/support_escalation_card.xml | 45 +++++ res/layout/support_fragment.xml | 67 +++++++ res/layout/support_tile.xml | 41 +++++ res/values/dimens.xml | 2 + res/values/strings.xml | 34 ++++ .../settings/InstrumentedFragment.java | 7 +- .../android/settings/SettingsActivity.java | 14 +- .../dashboard/DashboardContainerFragment.java | 136 ++++++++++++++ .../settings/dashboard/DashboardSummary.java | 13 +- .../settings/dashboard/SupportFragment.java | 71 ++++++++ .../settings/overlay/FeatureFactory.java | 2 + .../settings/overlay/FeatureFactoryImpl.java | 8 +- .../overlay/SupportFeatureProvider.java | 8 + .../settings/widget/SlidingTabLayout.java | 168 ++++++++++++++++++ 21 files changed, 782 insertions(+), 22 deletions(-) create mode 100644 res/drawable/ic_feedback_24dp.xml create mode 100644 res/drawable/ic_forum_24dp.xml create mode 100644 res/drawable/ic_help_24dp.xml create mode 100644 res/layout/dashboard_container.xml create mode 100644 res/layout/dashboard_container_header.xml create mode 100644 res/layout/sliding_tab_indicator_view.xml create mode 100644 res/layout/sliding_tab_title_view.xml create mode 100644 res/layout/support_escalation_card.xml create mode 100644 res/layout/support_fragment.xml create mode 100644 res/layout/support_tile.xml create mode 100644 src/com/android/settings/dashboard/DashboardContainerFragment.java create mode 100644 src/com/android/settings/dashboard/SupportFragment.java create mode 100644 src/com/android/settings/overlay/SupportFeatureProvider.java create mode 100644 src/com/android/settings/widget/SlidingTabLayout.java diff --git a/res/drawable/ic_feedback_24dp.xml b/res/drawable/ic_feedback_24dp.xml new file mode 100644 index 00000000000..f616f955fd8 --- /dev/null +++ b/res/drawable/ic_feedback_24dp.xml @@ -0,0 +1,32 @@ + + + + + + \ No newline at end of file diff --git a/res/drawable/ic_forum_24dp.xml b/res/drawable/ic_forum_24dp.xml new file mode 100644 index 00000000000..34c086952e1 --- /dev/null +++ b/res/drawable/ic_forum_24dp.xml @@ -0,0 +1,30 @@ + + + + + + \ No newline at end of file diff --git a/res/drawable/ic_help_24dp.xml b/res/drawable/ic_help_24dp.xml new file mode 100644 index 00000000000..5fd1e79b28c --- /dev/null +++ b/res/drawable/ic_help_24dp.xml @@ -0,0 +1,32 @@ + + + + + + \ No newline at end of file diff --git a/res/layout/dashboard_container.xml b/res/layout/dashboard_container.xml new file mode 100644 index 00000000000..4cab75c8e0f --- /dev/null +++ b/res/layout/dashboard_container.xml @@ -0,0 +1,21 @@ + + + + diff --git a/res/layout/dashboard_container_header.xml b/res/layout/dashboard_container_header.xml new file mode 100644 index 00000000000..a12f2498cb6 --- /dev/null +++ b/res/layout/dashboard_container_header.xml @@ -0,0 +1,23 @@ + + + + diff --git a/res/layout/sliding_tab_indicator_view.xml b/res/layout/sliding_tab_indicator_view.xml new file mode 100644 index 00000000000..9b2942f06e1 --- /dev/null +++ b/res/layout/sliding_tab_indicator_view.xml @@ -0,0 +1,22 @@ + + + + diff --git a/res/layout/sliding_tab_title_view.xml b/res/layout/sliding_tab_title_view.xml new file mode 100644 index 00000000000..0386345cb08 --- /dev/null +++ b/res/layout/sliding_tab_title_view.xml @@ -0,0 +1,28 @@ + + + + \ No newline at end of file diff --git a/res/layout/support_escalation_card.xml b/res/layout/support_escalation_card.xml new file mode 100644 index 00000000000..3ca7562a12e --- /dev/null +++ b/res/layout/support_escalation_card.xml @@ -0,0 +1,45 @@ + + + + + + + + + \ No newline at end of file diff --git a/res/layout/support_fragment.xml b/res/layout/support_fragment.xml new file mode 100644 index 00000000000..d81849d5923 --- /dev/null +++ b/res/layout/support_fragment.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/layout/support_tile.xml b/res/layout/support_tile.xml new file mode 100644 index 00000000000..74d460e4bc6 --- /dev/null +++ b/res/layout/support_tile.xml @@ -0,0 +1,41 @@ + + + + + + + \ No newline at end of file diff --git a/res/values/dimens.xml b/res/values/dimens.xml index ef7efa270a7..5c81f121b21 100755 --- a/res/values/dimens.xml +++ b/res/values/dimens.xml @@ -59,6 +59,8 @@ 40dip 0dp + 16dp + 3dp 300dip diff --git a/res/values/strings.xml b/res/values/strings.xml index 3eb6d3df240..a9572965736 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -7392,6 +7392,10 @@ Control lock screen notifications Show or hide notification content + + All + + Support %d dp @@ -7414,4 +7418,34 @@ Demo mode + + Remove from Device + + Apps + + %1$s, last used %2$d days ago + + %1$s, never used before + + %1$s, not sure when last used + + Free up %1$s + + + 24/7 support + + + Phone + + + Email + + + Help forum + + + Help articles + + + Send feedback diff --git a/src/com/android/settings/InstrumentedFragment.java b/src/com/android/settings/InstrumentedFragment.java index 0e3528a11e7..3977426601d 100644 --- a/src/com/android/settings/InstrumentedFragment.java +++ b/src/com/android/settings/InstrumentedFragment.java @@ -16,11 +16,11 @@ package com.android.settings; -import com.android.internal.logging.MetricsLogger; - import android.os.Bundle; import android.support.v14.preference.PreferenceFragment; +import com.android.internal.logging.MetricsLogger; + /** * Instrumented fragment that logs visibility state. */ @@ -30,6 +30,9 @@ public abstract class InstrumentedFragment extends PreferenceFragment { // Used by PreferenceActivity for the dummy fragment it adds, no useful data here. public static final int PREFERENCE_ACTIVITY_FRAGMENT = UNDECLARED + 1; + public static final int DASHBOARD_CONTAINER = UNDECLARED + 2; + + public static final int SUPPORT_FRAGMENT = UNDECLARED + 3; /** * Declare the view of this category. diff --git a/src/com/android/settings/SettingsActivity.java b/src/com/android/settings/SettingsActivity.java index 176ab7ebd64..cde3a2d6e22 100644 --- a/src/com/android/settings/SettingsActivity.java +++ b/src/com/android/settings/SettingsActivity.java @@ -49,6 +49,7 @@ import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.SearchView; + import com.android.internal.util.ArrayUtils; import com.android.settings.Settings.WifiSettingsActivity; import com.android.settings.accessibility.AccessibilitySettings; @@ -68,7 +69,7 @@ import com.android.settings.applications.UsageAccessDetails; import com.android.settings.applications.WriteSettingsDetails; import com.android.settings.applications.VrListenerSettings; import com.android.settings.bluetooth.BluetoothSettings; -import com.android.settings.dashboard.DashboardSummary; +import com.android.settings.dashboard.DashboardContainerFragment; import com.android.settings.dashboard.SearchResultsSummary; import com.android.settings.datausage.DataUsageSummary; import com.android.settings.deviceinfo.PrivateVolumeForget; @@ -613,7 +614,7 @@ public class SettingsActivity extends SettingsDrawerActivity // Show Search affordance mDisplaySearch = true; mInitialTitleResId = R.string.dashboard_title; - switchToFragment(DashboardSummary.class.getName(), null, false, false, + switchToFragment(DashboardContainerFragment.class.getName(), null, false, false, mInitialTitleResId, mInitialTitle, false); } } @@ -687,7 +688,7 @@ public class SettingsActivity extends SettingsDrawerActivity } /** - * Sets the id of the view continaing the main content. Should be called before calling super's + * Sets the id of the view containing the main content. Should be called before calling super's * onCreate. */ protected void setMainContentId(int contentId) { @@ -729,7 +730,7 @@ public class SettingsActivity extends SettingsDrawerActivity setTitleFromBackStack(); } - private int setTitleFromBackStack() { + private void setTitleFromBackStack() { final int count = getFragmentManager().getBackStackEntryCount(); if (count == 0) { @@ -738,13 +739,11 @@ public class SettingsActivity extends SettingsDrawerActivity } else { setTitle(mInitialTitle); } - return 0; + return; } FragmentManager.BackStackEntry bse = getFragmentManager().getBackStackEntryAt(count - 1); setTitleFromBackStackEntry(bse); - - return count; } private void setTitleFromBackStackEntry(FragmentManager.BackStackEntry bse) { @@ -1196,6 +1195,7 @@ public class SettingsActivity extends SettingsDrawerActivity if (current != null && current instanceof SearchResultsSummary) { mSearchResultsFragment = (SearchResultsSummary) current; } else { + setContentHeaderView(null); mSearchResultsFragment = (SearchResultsSummary) switchToFragment( SearchResultsSummary.class.getName(), null, false, true, R.string.search_results_title, null, true); diff --git a/src/com/android/settings/dashboard/DashboardContainerFragment.java b/src/com/android/settings/dashboard/DashboardContainerFragment.java new file mode 100644 index 00000000000..831ac508f63 --- /dev/null +++ b/src/com/android/settings/dashboard/DashboardContainerFragment.java @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2016 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.settings.dashboard; + +import android.app.Activity; +import android.app.Fragment; +import android.app.FragmentManager; +import android.content.Context; +import android.os.Bundle; +import android.support.v13.app.FragmentPagerAdapter; +import android.support.v4.view.ViewPager; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.View; +import android.view.ViewGroup; + +import com.android.settings.InstrumentedFragment; +import com.android.settings.R; +import com.android.settings.overlay.FeatureFactory; +import com.android.settings.overlay.SupportFeatureProvider; +import com.android.settings.widget.SlidingTabLayout; +import com.android.settingslib.drawer.SettingsDrawerActivity; +import com.android.settingslib.HelpUtils; + +/** + * Container for Dashboard fragments. + */ +public final class DashboardContainerFragment extends InstrumentedFragment { + + private static final int INDEX_SUMMARY_FRAGMENT = 0; + private static final int INDEX_SUPPORT_FRAGMENT = 1; + + private ViewPager mViewPager; + private View mHeaderView; + private DashboardViewPagerAdapter mPagerAdapter; + + @Override + protected int getMetricsCategory() { + return DASHBOARD_CONTAINER; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setHasOptionsMenu(true); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { + final View content = inflater.inflate(R.layout.dashboard_container, parent, false); + mViewPager = (ViewPager) content.findViewById(R.id.pager); + mPagerAdapter = new DashboardViewPagerAdapter(getContext(), getChildFragmentManager()); + mViewPager.setAdapter(mPagerAdapter); + mHeaderView = inflater.inflate(R.layout.dashboard_container_header, parent, false); + ((SlidingTabLayout) mHeaderView).setViewPager(mViewPager); + return content; + } + + @Override + public void onResume() { + super.onResume(); + if (mPagerAdapter.getCount() > 1) { + final Activity activity = getActivity(); + if (activity instanceof SettingsDrawerActivity) { + ((SettingsDrawerActivity) getActivity()).setContentHeaderView(mHeaderView); + } + } + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + super.onCreateOptionsMenu(menu, inflater); + if (getActivity() == null) return; + HelpUtils.prepareHelpMenuItem(getActivity(), menu, R.string.help_uri_dashboard, + getClass().getName()); + } + + private static final class DashboardViewPagerAdapter extends FragmentPagerAdapter { + + private final Context mContext; + private final SupportFeatureProvider mSupportFeatureProvider; + + public DashboardViewPagerAdapter(Context context, FragmentManager fragmentManager) { + super(fragmentManager); + mContext = context; + mSupportFeatureProvider = + FeatureFactory.getFactory(context).getSupportFeatureProvider(); + } + + @Override + public CharSequence getPageTitle(int position) { + switch (position) { + case INDEX_SUMMARY_FRAGMENT: + return mContext.getString(R.string.page_tab_title_summary); + case INDEX_SUPPORT_FRAGMENT: + return mContext.getString(R.string.page_tab_title_support); + } + return super.getPageTitle(position); + } + + @Override + public Fragment getItem(int position) { + switch (position) { + case INDEX_SUMMARY_FRAGMENT: + return new DashboardSummary(); + case INDEX_SUPPORT_FRAGMENT: + return new SupportFragment(); + default: + throw new IllegalArgumentException( + String.format( + "Position %d does not map to a valid dashboard fragment", + position)); + } + } + + @Override + public int getCount() { + return mSupportFeatureProvider == null ? 1 : 2; + } + } +} diff --git a/src/com/android/settings/dashboard/DashboardSummary.java b/src/com/android/settings/dashboard/DashboardSummary.java index 66b3730e610..80054d0f5d3 100644 --- a/src/com/android/settings/dashboard/DashboardSummary.java +++ b/src/com/android/settings/dashboard/DashboardSummary.java @@ -21,13 +21,11 @@ import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.util.Log; import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; import android.view.View; import android.view.ViewGroup; + import com.android.internal.logging.MetricsLogger; import com.android.internal.logging.MetricsProto.MetricsEvent; -import com.android.settingslib.HelpUtils; import com.android.settings.InstrumentedFragment; import com.android.settings.R; import com.android.settings.Settings; @@ -83,7 +81,6 @@ public class DashboardSummary extends InstrumentedFragment List categories = ((SettingsActivity) getActivity()).getDashboardCategories(); mSummaryLoader = new SummaryLoader(getActivity(), categories); - setHasOptionsMenu(true); Context context = getContext(); mConditionManager = ConditionManager.get(context); mSuggestionParser = new SuggestionParser(context, @@ -98,14 +95,6 @@ public class DashboardSummary extends InstrumentedFragment super.onDestroy(); } - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - super.onCreateOptionsMenu(menu, inflater); - if (getActivity() == null) return; - HelpUtils.prepareHelpMenuItem(getActivity(), menu, R.string.help_uri_dashboard, - getClass().getName()); - } - @Override public void onResume() { long startTime = System.currentTimeMillis(); diff --git a/src/com/android/settings/dashboard/SupportFragment.java b/src/com/android/settings/dashboard/SupportFragment.java new file mode 100644 index 00000000000..2e4fedc5995 --- /dev/null +++ b/src/com/android/settings/dashboard/SupportFragment.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2016 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.settings.dashboard; + +import android.annotation.DrawableRes; +import android.annotation.IdRes; +import android.annotation.StringRes; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import com.android.settings.InstrumentedFragment; +import com.android.settings.R; + +/** + * Fragment for support tab in SettingsGoogle. + */ +public final class SupportFragment extends InstrumentedFragment { + + private View mContent; + + @Override + protected int getMetricsCategory() { + return SUPPORT_FRAGMENT; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + mContent = inflater.inflate(R.layout.support_fragment, container, false); + // Update escalation items. + updateEscalationCard(R.id.escalation_by_phone, R.string.support_escalation_by_phone); + updateEscalationCard(R.id.escalation_by_email, R.string.support_escalation_by_email); + // Update other support items. + updateSupportTile(R.id.forum_tile, R.drawable.ic_forum_24dp, R.string.support_forum_title); + updateSupportTile(R.id.article_tile, R.drawable.ic_help_24dp, + R.string.support_articles_title); + // Update feedback item. + updateSupportTile(R.id.feedback_tile, R.drawable.ic_feedback_24dp, + R.string.support_feedback_title); + return mContent; + } + + private void updateEscalationCard(@IdRes int cardId, @StringRes int title) { + final View card = mContent.findViewById(cardId); + ((TextView) card.findViewById(R.id.title)).setText(title); + } + + private void updateSupportTile(@IdRes int tileId, @DrawableRes int icon, @StringRes int title) { + final View tile = mContent.findViewById(tileId); + ((ImageView) tile.findViewById(android.R.id.icon)).setImageResource(icon); + ((TextView) tile.findViewById(android.R.id.title)).setText(title); + } +} diff --git a/src/com/android/settings/overlay/FeatureFactory.java b/src/com/android/settings/overlay/FeatureFactory.java index b0b2a3f48eb..4dc9ba2eb45 100644 --- a/src/com/android/settings/overlay/FeatureFactory.java +++ b/src/com/android/settings/overlay/FeatureFactory.java @@ -59,6 +59,8 @@ public abstract class FeatureFactory { return sFactory; } + public abstract SupportFeatureProvider getSupportFeatureProvider(); + public static final class FactoryNotFoundException extends RuntimeException { public FactoryNotFoundException(Throwable throwable) { super("Unable to create factory. Did you misconfigure Proguard?", throwable); diff --git a/src/com/android/settings/overlay/FeatureFactoryImpl.java b/src/com/android/settings/overlay/FeatureFactoryImpl.java index 036b4e7075f..d884080a823 100644 --- a/src/com/android/settings/overlay/FeatureFactoryImpl.java +++ b/src/com/android/settings/overlay/FeatureFactoryImpl.java @@ -19,5 +19,11 @@ package com.android.settings.overlay; /** * {@link FeatureFactory} implementation for AOSP Settings. */ -public class FeatureFactoryImpl extends FeatureFactory { +public final class FeatureFactoryImpl extends FeatureFactory { + + @Override + public SupportFeatureProvider getSupportFeatureProvider() { + return null; + } + } diff --git a/src/com/android/settings/overlay/SupportFeatureProvider.java b/src/com/android/settings/overlay/SupportFeatureProvider.java new file mode 100644 index 00000000000..3b654f469ae --- /dev/null +++ b/src/com/android/settings/overlay/SupportFeatureProvider.java @@ -0,0 +1,8 @@ +package com.android.settings.overlay; + +/** + * Feature provider for support tab. + */ +public interface SupportFeatureProvider { + +} diff --git a/src/com/android/settings/widget/SlidingTabLayout.java b/src/com/android/settings/widget/SlidingTabLayout.java new file mode 100644 index 00000000000..176bebc8457 --- /dev/null +++ b/src/com/android/settings/widget/SlidingTabLayout.java @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2016 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.settings.widget; + +import android.content.Context; +import android.support.v4.view.PagerAdapter; +import android.support.v4.view.ViewPager; +import android.util.AttributeSet; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.FrameLayout; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.android.settings.R; + +/** + * To be used with ViewPager to provide a tab indicator component which give constant feedback as + * to the user's scroll progress. + */ +public final class SlidingTabLayout extends FrameLayout implements View.OnClickListener { + + private final LinearLayout mTitleView; + private final View mIndicatorView; + private final LayoutInflater mLayoutInflater; + + private ViewPager mViewPager; + private int mSelectedPosition; + private float mSelectionOffset; + + public SlidingTabLayout(Context context, AttributeSet attrs) { + super(context, attrs); + mLayoutInflater = LayoutInflater.from(context); + mTitleView = new LinearLayout(context); + mTitleView.setGravity(Gravity.CENTER_HORIZONTAL); + mIndicatorView = mLayoutInflater.inflate(R.layout.sliding_tab_indicator_view, this, false); + + addView(mTitleView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); + addView(mIndicatorView, mIndicatorView.getLayoutParams()); + } + + /** + * Sets the associated view pager. Note that the assumption here is that the pager content + * (number of tabs and tab titles) does not change after this call has been made. + */ + public void setViewPager(ViewPager viewPager) { + mTitleView.removeAllViews(); + + mViewPager = viewPager; + if (viewPager != null) { + viewPager.addOnPageChangeListener(new InternalViewPagerListener()); + populateTabStrip(); + } + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + final int titleCount = mTitleView.getChildCount(); + if (titleCount > 0) { + final int width = MeasureSpec.makeMeasureSpec( + mTitleView.getMeasuredWidth() / titleCount, MeasureSpec.EXACTLY); + final int height = MeasureSpec.makeMeasureSpec( + mIndicatorView.getMeasuredHeight(), MeasureSpec.EXACTLY); + mIndicatorView.measure(width, height); + } + } + + @Override + protected void onLayout(boolean changed, int left, int top, int right, int bottom) { + if (mTitleView.getChildCount() > 0) { + mTitleView.layout(0, 0, mTitleView.getMeasuredWidth(), mTitleView.getMeasuredHeight()); + final int indicatorBottom = getMeasuredHeight(); + final int indicatorHeight = mIndicatorView.getMeasuredHeight(); + mIndicatorView.layout(0, indicatorBottom - indicatorHeight, + mIndicatorView.getMeasuredWidth(), indicatorBottom); + } + } + + @Override + public void onClick(View v) { + final int titleCount = mTitleView.getChildCount(); + for (int i = 0; i < titleCount; i++) { + if (v == mTitleView.getChildAt(i)) { + mViewPager.setCurrentItem(i); + return; + } + } + } + + private void onViewPagerPageChanged(int position, float positionOffset) { + mSelectedPosition = position; + mSelectionOffset = positionOffset; + mIndicatorView.setTranslationX(getIndicatorLeft()); + } + + private void populateTabStrip() { + final PagerAdapter adapter = mViewPager.getAdapter(); + + for (int i = 0; i < adapter.getCount(); i++) { + final TextView tabTitleView = (TextView) mLayoutInflater.inflate( + R.layout.sliding_tab_title_view, mTitleView, false); + + tabTitleView.setText(adapter.getPageTitle(i)); + tabTitleView.setOnClickListener(this); + + mTitleView.addView(tabTitleView); + if (i == mViewPager.getCurrentItem()) { + tabTitleView.setSelected(true); + } + } + } + + private int getIndicatorLeft() { + View selectedTitle = mTitleView.getChildAt(mSelectedPosition); + int left = selectedTitle.getLeft(); + if (mSelectionOffset > 0f && mSelectedPosition < (getChildCount() - 1)) { + View nextTitle = mTitleView.getChildAt(mSelectedPosition + 1); + left = (int) (mSelectionOffset * nextTitle.getLeft() + + (1.0f - mSelectionOffset) * left); + } + return left; + } + + private final class InternalViewPagerListener implements ViewPager.OnPageChangeListener { + private int mScrollState; + + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + final int titleCount = mTitleView.getChildCount(); + if ((titleCount == 0) || (position < 0) || (position >= titleCount)) { + return; + } + onViewPagerPageChanged(position, positionOffset); + } + + @Override + public void onPageScrollStateChanged(int state) { + mScrollState = state; + } + + @Override + public void onPageSelected(int position) { + if (mScrollState == ViewPager.SCROLL_STATE_IDLE) { + onViewPagerPageChanged(position, 0f); + } + final int titleCount = getChildCount(); + for (int i = 0; i < titleCount; i++) { + getChildAt(i).setSelected(position == i); + } + } + } +}