ViewPagerTabs.java revision d6ffb82e7684ea4a5caa9313d9bfc87980cadc91
1429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell/* 2429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * Copyright (C) 2014 The Android Open Source Project 3429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * 4429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * Licensed under the Apache License, Version 2.0 (the "License"); 5429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * you may not use this file except in compliance with the License. 6429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * You may obtain a copy of the License at 7429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * 8429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * http://www.apache.org/licenses/LICENSE-2.0 9429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * 10429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * Unless required by applicable law or agreed to in writing, software 11429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * distributed under the License is distributed on an "AS IS" BASIS, 12429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * See the License for the specific language governing permissions and 14429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * limitations under the License. 15429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell */ 16429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellpackage com.android.contacts.common.list; 17429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 18429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.content.Context; 19429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.content.res.ColorStateList; 20429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.content.res.TypedArray; 21429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.graphics.Outline; 22429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.support.v4.view.PagerAdapter; 23429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.support.v4.view.ViewPager; 24429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.util.AttributeSet; 25429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.util.TypedValue; 26429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.view.Gravity; 273a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chenimport android.view.LayoutInflater; 28429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.view.View; 29429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.view.ViewOutlineProvider; 30429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.widget.FrameLayout; 31429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.widget.HorizontalScrollView; 32429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.widget.LinearLayout; 33429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.widget.TextView; 34429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport android.widget.Toast; 35429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 36429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellimport com.android.contacts.common.R; 37429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 38429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell/** 39429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * Lightweight implementation of ViewPager tabs. This looks similar to traditional actionBar tabs, 40429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * but allows for the view containing the tabs to be placed anywhere on screen. Text-related 41429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * attributes can also be assigned in XML - these will get propogated to the child TextViews 42429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * automatically. 43429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell */ 44429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwellpublic class ViewPagerTabs extends HorizontalScrollView implements ViewPager.OnPageChangeListener { 45429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 46429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell ViewPager mPager; 47429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell private ViewPagerTabStrip mTabStrip; 48429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 49429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell /** 50429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * Linearlayout that will contain the TextViews serving as tabs. This is the only child 51429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * of the parent HorizontalScrollView. 52429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell */ 53429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final int mTextStyle; 54429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final ColorStateList mTextColor; 55429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final int mTextSize; 56429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final boolean mTextAllCaps; 57429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell int mPrevSelected = -1; 58429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell int mSidePadding; 59429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 6007ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee private int[] mTabIcons; 613a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen // For displaying the unread count next to the tab icon. 623a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen private int[] mUnreadCounts; 6307ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee 64429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell private static final ViewOutlineProvider VIEW_BOUNDS_OUTLINE_PROVIDER = 65429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell new ViewOutlineProvider() { 66429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell @Override 67429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell public void getOutline(View view, Outline outline) { 68429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell outline.setRect(0, 0, view.getWidth(), view.getHeight()); 69429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 70429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell }; 71429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 72429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell private static final int TAB_SIDE_PADDING_IN_DPS = 10; 73429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 74429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell // TODO: This should use <declare-styleable> in the future 75429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell private static final int[] ATTRS = new int[] { 76429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell android.R.attr.textSize, 77429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell android.R.attr.textStyle, 78429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell android.R.attr.textColor, 79429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell android.R.attr.textAllCaps 80429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell }; 81429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 82429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell /** 83429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell * Simulates actionbar tab behavior by showing a toast with the tab title when long clicked. 84429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell */ 85429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell private class OnTabLongClickListener implements OnLongClickListener { 86429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final int mPosition; 87429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 88429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell public OnTabLongClickListener(int position) { 89429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell mPosition = position; 90429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 91429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 92429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell @Override 93429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell public boolean onLongClick(View v) { 94429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final int[] screenPos = new int[2]; 95429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell getLocationOnScreen(screenPos); 96429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 97429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final Context context = getContext(); 98429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final int width = getWidth(); 99429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final int height = getHeight(); 100429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final int screenWidth = context.getResources().getDisplayMetrics().widthPixels; 101429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 102429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell Toast toast = Toast.makeText(context, mPager.getAdapter().getPageTitle(mPosition), 103429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell Toast.LENGTH_SHORT); 104429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 105429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell // Show the toast under the tab 106429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 107429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell (screenPos[0] + width / 2) - screenWidth / 2, screenPos[1] + height); 108429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 109429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell toast.show(); 110429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell return true; 111429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 112429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 113429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 114429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell public ViewPagerTabs(Context context) { 115429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell this(context, null); 116429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 117429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 118429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell public ViewPagerTabs(Context context, AttributeSet attrs) { 119429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell this(context, attrs, 0); 120429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 121429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 122429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell public ViewPagerTabs(Context context, AttributeSet attrs, int defStyle) { 123429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell super(context, attrs, defStyle); 124429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell setFillViewport(true); 125429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 126429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell mSidePadding = (int) (getResources().getDisplayMetrics().density * TAB_SIDE_PADDING_IN_DPS); 127429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 128429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final TypedArray a = context.obtainStyledAttributes(attrs, ATTRS); 129429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell mTextSize = a.getDimensionPixelSize(0, 0); 130429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell mTextStyle = a.getInt(1, 0); 131429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell mTextColor = a.getColorStateList(2); 132429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell mTextAllCaps = a.getBoolean(3, false); 133429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 134429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell mTabStrip = new ViewPagerTabStrip(context); 135429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell addView(mTabStrip, 136429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); 137429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell a.recycle(); 138429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 139429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell // enable shadow casting from view bounds 140429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell setOutlineProvider(VIEW_BOUNDS_OUTLINE_PROVIDER); 141429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 142429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 143429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell public void setViewPager(ViewPager viewPager) { 144429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell mPager = viewPager; 145429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell addTabs(mPager.getAdapter()); 146429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 147429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 1483a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen /** 1493a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen * Set the tab icons and initialize an array for unread counts the same length as the icon 1503a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen * array. 1513a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen * 1523a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen * @param tabIcons An array representing the tab icons in order. 1533a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen */ 1543a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen public void configureTabIcons(int[] tabIcons) { 15507ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee mTabIcons = tabIcons; 1563a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen mUnreadCounts = new int[tabIcons.length]; 1573a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen } 1583a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen 1593a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen public void setUnreadCount(int count, int position) { 1603a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen if (mUnreadCounts == null || position >= mUnreadCounts.length) { 1613a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen return; 1623a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen } 1633a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen mUnreadCounts[position] = count; 16407ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee } 16507ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee 166429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell private void addTabs(PagerAdapter adapter) { 167429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell mTabStrip.removeAllViews(); 168429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 169429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final int count = adapter.getCount(); 170429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell for (int i = 0; i < count; i++) { 171429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell addTab(adapter.getPageTitle(i), i); 172429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 173429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 174429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 175429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell private void addTab(CharSequence tabTitle, final int position) { 17607ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee View tabView; 17707ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee if (mTabIcons != null && position < mTabIcons.length) { 1783a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen View layout = LayoutInflater.from(getContext()).inflate( 1793a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen R.layout.unread_count_tab, null); 1803a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen View iconView = layout.findViewById(R.id.icon); 18107ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee iconView.setBackgroundResource(mTabIcons[position]); 18207ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee iconView.setContentDescription(tabTitle); 1833a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen TextView textView = (TextView) layout.findViewById(R.id.count); 1843a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen if (mUnreadCounts != null && mUnreadCounts[position] > 0) { 1853a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen textView.setText(Integer.toString(mUnreadCounts[position])); 1863a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen textView.setVisibility(View.VISIBLE); 1873a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen } else { 1883a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen textView.setVisibility(View.INVISIBLE); 1893a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen } 1903a20bedbe63d3a92391aca2429a5a5e5c4f165c5Nancy Chen tabView = layout; 19107ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee } else { 19207ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee final TextView textView = new TextView(getContext()); 19307ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee textView.setText(tabTitle); 19407ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee textView.setBackgroundResource(R.drawable.view_pager_tab_background); 19507ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee 19607ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee // Assign various text appearance related attributes to child views. 19707ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee if (mTextStyle > 0) { 19807ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee textView.setTypeface(textView.getTypeface(), mTextStyle); 19907ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee } 20007ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee if (mTextSize > 0) { 20107ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); 20207ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee } 20307ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee if (mTextColor != null) { 20407ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee textView.setTextColor(mTextColor); 20507ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee } 20607ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee textView.setAllCaps(mTextAllCaps); 20707ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee textView.setGravity(Gravity.CENTER); 20807ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee 20907ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee tabView = textView; 21007ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee } 21107ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee 21207ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee tabView.setOnClickListener(new OnClickListener() { 213429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell @Override 214429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell public void onClick(View v) { 2154d0ec405721176fcbf4c70bade5d34da68d523c3Yorke Lee mPager.setCurrentItem(getRtlPosition(position)); 216429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 217429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell }); 218429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 21907ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee tabView.setOnLongClickListener(new OnTabLongClickListener(position)); 220429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 22107ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee tabView.setPadding(mSidePadding, 0, mSidePadding, 0); 222d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen 223d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen mTabStrip.addView(tabView, position, new LinearLayout.LayoutParams( 224d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1)); 22507ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee 226429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell // Default to the first child being selected 227429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell if (position == 0) { 228429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell mPrevSelected = 0; 22907ddd3016f4db8de95636a1c7eabf5ebf6813ddbAndrew Lee tabView.setSelected(true); 230429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 231429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 232429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 233d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen /** 234d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen * Refresh a tab at a certain index by removing it and reconstructing it. 235d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen * 236d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen * @param index The index of the tab view we wish to update. 237d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen */ 238d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen public void updateTab(int index) { 239d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen View view = mTabStrip.getChildAt(index); 240d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen mTabStrip.removeView(view); 241d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen addTab(mPager.getAdapter().getPageTitle(index), index); 242d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen } 243d6ffb82e7684ea4a5caa9313d9bfc87980cadc91Nancy Chen 244429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell @Override 245429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 2464d0ec405721176fcbf4c70bade5d34da68d523c3Yorke Lee position = getRtlPosition(position); 247429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell int tabStripChildCount = mTabStrip.getChildCount(); 248429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell if ((tabStripChildCount == 0) || (position < 0) || (position >= tabStripChildCount)) { 249429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell return; 250429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 251429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 252429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell mTabStrip.onPageScrolled(position, positionOffset, positionOffsetPixels); 253429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 254429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 255429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell @Override 256429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell public void onPageSelected(int position) { 2574d0ec405721176fcbf4c70bade5d34da68d523c3Yorke Lee position = getRtlPosition(position); 25864355c717f4f739baae02d347744cfcc577ffe0bJay Shrauner int tabStripChildCount = mTabStrip.getChildCount(); 25964355c717f4f739baae02d347744cfcc577ffe0bJay Shrauner if ((tabStripChildCount == 0) || (position < 0) || (position >= tabStripChildCount)) { 26064355c717f4f739baae02d347744cfcc577ffe0bJay Shrauner return; 26164355c717f4f739baae02d347744cfcc577ffe0bJay Shrauner } 26264355c717f4f739baae02d347744cfcc577ffe0bJay Shrauner 26364355c717f4f739baae02d347744cfcc577ffe0bJay Shrauner if (mPrevSelected >= 0 && mPrevSelected < tabStripChildCount) { 264429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell mTabStrip.getChildAt(mPrevSelected).setSelected(false); 265429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 266429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final View selectedChild = mTabStrip.getChildAt(position); 267429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell selectedChild.setSelected(true); 268429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 269429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell // Update scroll position 270429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell final int scrollPos = selectedChild.getLeft() - (getWidth() - selectedChild.getWidth()) / 2; 271429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell smoothScrollTo(scrollPos, 0); 272429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell mPrevSelected = position; 273429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 274429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 275429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell @Override 276429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell public void onPageScrollStateChanged(int state) { 277429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell } 2784d0ec405721176fcbf4c70bade5d34da68d523c3Yorke Lee 2794d0ec405721176fcbf4c70bade5d34da68d523c3Yorke Lee private int getRtlPosition(int position) { 2804d0ec405721176fcbf4c70bade5d34da68d523c3Yorke Lee if (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) { 2814d0ec405721176fcbf4c70bade5d34da68d523c3Yorke Lee return mTabStrip.getChildCount() - 1 - position; 2824d0ec405721176fcbf4c70bade5d34da68d523c3Yorke Lee } 2834d0ec405721176fcbf4c70bade5d34da68d523c3Yorke Lee return position; 2844d0ec405721176fcbf4c70bade5d34da68d523c3Yorke Lee } 285429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell} 286429302fd99805fc6d88e974335289e22ff59c1eeBrian Attwell 287