ViewPagerTabs.java revision d6ffb82e7684ea4a5caa9313d9bfc87980cadc91
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package com.android.contacts.common.list; 17 18import android.content.Context; 19import android.content.res.ColorStateList; 20import android.content.res.TypedArray; 21import android.graphics.Outline; 22import android.support.v4.view.PagerAdapter; 23import android.support.v4.view.ViewPager; 24import android.util.AttributeSet; 25import android.util.TypedValue; 26import android.view.Gravity; 27import android.view.LayoutInflater; 28import android.view.View; 29import android.view.ViewOutlineProvider; 30import android.widget.FrameLayout; 31import android.widget.HorizontalScrollView; 32import android.widget.LinearLayout; 33import android.widget.TextView; 34import android.widget.Toast; 35 36import com.android.contacts.common.R; 37 38/** 39 * Lightweight implementation of ViewPager tabs. This looks similar to traditional actionBar tabs, 40 * but allows for the view containing the tabs to be placed anywhere on screen. Text-related 41 * attributes can also be assigned in XML - these will get propogated to the child TextViews 42 * automatically. 43 */ 44public class ViewPagerTabs extends HorizontalScrollView implements ViewPager.OnPageChangeListener { 45 46 ViewPager mPager; 47 private ViewPagerTabStrip mTabStrip; 48 49 /** 50 * Linearlayout that will contain the TextViews serving as tabs. This is the only child 51 * of the parent HorizontalScrollView. 52 */ 53 final int mTextStyle; 54 final ColorStateList mTextColor; 55 final int mTextSize; 56 final boolean mTextAllCaps; 57 int mPrevSelected = -1; 58 int mSidePadding; 59 60 private int[] mTabIcons; 61 // For displaying the unread count next to the tab icon. 62 private int[] mUnreadCounts; 63 64 private static final ViewOutlineProvider VIEW_BOUNDS_OUTLINE_PROVIDER = 65 new ViewOutlineProvider() { 66 @Override 67 public void getOutline(View view, Outline outline) { 68 outline.setRect(0, 0, view.getWidth(), view.getHeight()); 69 } 70 }; 71 72 private static final int TAB_SIDE_PADDING_IN_DPS = 10; 73 74 // TODO: This should use <declare-styleable> in the future 75 private static final int[] ATTRS = new int[] { 76 android.R.attr.textSize, 77 android.R.attr.textStyle, 78 android.R.attr.textColor, 79 android.R.attr.textAllCaps 80 }; 81 82 /** 83 * Simulates actionbar tab behavior by showing a toast with the tab title when long clicked. 84 */ 85 private class OnTabLongClickListener implements OnLongClickListener { 86 final int mPosition; 87 88 public OnTabLongClickListener(int position) { 89 mPosition = position; 90 } 91 92 @Override 93 public boolean onLongClick(View v) { 94 final int[] screenPos = new int[2]; 95 getLocationOnScreen(screenPos); 96 97 final Context context = getContext(); 98 final int width = getWidth(); 99 final int height = getHeight(); 100 final int screenWidth = context.getResources().getDisplayMetrics().widthPixels; 101 102 Toast toast = Toast.makeText(context, mPager.getAdapter().getPageTitle(mPosition), 103 Toast.LENGTH_SHORT); 104 105 // Show the toast under the tab 106 toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 107 (screenPos[0] + width / 2) - screenWidth / 2, screenPos[1] + height); 108 109 toast.show(); 110 return true; 111 } 112 } 113 114 public ViewPagerTabs(Context context) { 115 this(context, null); 116 } 117 118 public ViewPagerTabs(Context context, AttributeSet attrs) { 119 this(context, attrs, 0); 120 } 121 122 public ViewPagerTabs(Context context, AttributeSet attrs, int defStyle) { 123 super(context, attrs, defStyle); 124 setFillViewport(true); 125 126 mSidePadding = (int) (getResources().getDisplayMetrics().density * TAB_SIDE_PADDING_IN_DPS); 127 128 final TypedArray a = context.obtainStyledAttributes(attrs, ATTRS); 129 mTextSize = a.getDimensionPixelSize(0, 0); 130 mTextStyle = a.getInt(1, 0); 131 mTextColor = a.getColorStateList(2); 132 mTextAllCaps = a.getBoolean(3, false); 133 134 mTabStrip = new ViewPagerTabStrip(context); 135 addView(mTabStrip, 136 new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); 137 a.recycle(); 138 139 // enable shadow casting from view bounds 140 setOutlineProvider(VIEW_BOUNDS_OUTLINE_PROVIDER); 141 } 142 143 public void setViewPager(ViewPager viewPager) { 144 mPager = viewPager; 145 addTabs(mPager.getAdapter()); 146 } 147 148 /** 149 * Set the tab icons and initialize an array for unread counts the same length as the icon 150 * array. 151 * 152 * @param tabIcons An array representing the tab icons in order. 153 */ 154 public void configureTabIcons(int[] tabIcons) { 155 mTabIcons = tabIcons; 156 mUnreadCounts = new int[tabIcons.length]; 157 } 158 159 public void setUnreadCount(int count, int position) { 160 if (mUnreadCounts == null || position >= mUnreadCounts.length) { 161 return; 162 } 163 mUnreadCounts[position] = count; 164 } 165 166 private void addTabs(PagerAdapter adapter) { 167 mTabStrip.removeAllViews(); 168 169 final int count = adapter.getCount(); 170 for (int i = 0; i < count; i++) { 171 addTab(adapter.getPageTitle(i), i); 172 } 173 } 174 175 private void addTab(CharSequence tabTitle, final int position) { 176 View tabView; 177 if (mTabIcons != null && position < mTabIcons.length) { 178 View layout = LayoutInflater.from(getContext()).inflate( 179 R.layout.unread_count_tab, null); 180 View iconView = layout.findViewById(R.id.icon); 181 iconView.setBackgroundResource(mTabIcons[position]); 182 iconView.setContentDescription(tabTitle); 183 TextView textView = (TextView) layout.findViewById(R.id.count); 184 if (mUnreadCounts != null && mUnreadCounts[position] > 0) { 185 textView.setText(Integer.toString(mUnreadCounts[position])); 186 textView.setVisibility(View.VISIBLE); 187 } else { 188 textView.setVisibility(View.INVISIBLE); 189 } 190 tabView = layout; 191 } else { 192 final TextView textView = new TextView(getContext()); 193 textView.setText(tabTitle); 194 textView.setBackgroundResource(R.drawable.view_pager_tab_background); 195 196 // Assign various text appearance related attributes to child views. 197 if (mTextStyle > 0) { 198 textView.setTypeface(textView.getTypeface(), mTextStyle); 199 } 200 if (mTextSize > 0) { 201 textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); 202 } 203 if (mTextColor != null) { 204 textView.setTextColor(mTextColor); 205 } 206 textView.setAllCaps(mTextAllCaps); 207 textView.setGravity(Gravity.CENTER); 208 209 tabView = textView; 210 } 211 212 tabView.setOnClickListener(new OnClickListener() { 213 @Override 214 public void onClick(View v) { 215 mPager.setCurrentItem(getRtlPosition(position)); 216 } 217 }); 218 219 tabView.setOnLongClickListener(new OnTabLongClickListener(position)); 220 221 tabView.setPadding(mSidePadding, 0, mSidePadding, 0); 222 223 mTabStrip.addView(tabView, position, new LinearLayout.LayoutParams( 224 LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1)); 225 226 // Default to the first child being selected 227 if (position == 0) { 228 mPrevSelected = 0; 229 tabView.setSelected(true); 230 } 231 } 232 233 /** 234 * Refresh a tab at a certain index by removing it and reconstructing it. 235 * 236 * @param index The index of the tab view we wish to update. 237 */ 238 public void updateTab(int index) { 239 View view = mTabStrip.getChildAt(index); 240 mTabStrip.removeView(view); 241 addTab(mPager.getAdapter().getPageTitle(index), index); 242 } 243 244 @Override 245 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 246 position = getRtlPosition(position); 247 int tabStripChildCount = mTabStrip.getChildCount(); 248 if ((tabStripChildCount == 0) || (position < 0) || (position >= tabStripChildCount)) { 249 return; 250 } 251 252 mTabStrip.onPageScrolled(position, positionOffset, positionOffsetPixels); 253 } 254 255 @Override 256 public void onPageSelected(int position) { 257 position = getRtlPosition(position); 258 int tabStripChildCount = mTabStrip.getChildCount(); 259 if ((tabStripChildCount == 0) || (position < 0) || (position >= tabStripChildCount)) { 260 return; 261 } 262 263 if (mPrevSelected >= 0 && mPrevSelected < tabStripChildCount) { 264 mTabStrip.getChildAt(mPrevSelected).setSelected(false); 265 } 266 final View selectedChild = mTabStrip.getChildAt(position); 267 selectedChild.setSelected(true); 268 269 // Update scroll position 270 final int scrollPos = selectedChild.getLeft() - (getWidth() - selectedChild.getWidth()) / 2; 271 smoothScrollTo(scrollPos, 0); 272 mPrevSelected = position; 273 } 274 275 @Override 276 public void onPageScrollStateChanged(int state) { 277 } 278 279 private int getRtlPosition(int position) { 280 if (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) { 281 return mTabStrip.getChildCount() - 1 - position; 282 } 283 return position; 284 } 285} 286 287