/** * Refresh a tab at a certain index by removing it and reconstructing it. * * @param index The index of the tab view we wish to update. */ public void updateTab(int index) { removeTab(index); if (index < mPager.getAdapter().getCount()) { addTab(mPager.getAdapter().getPageTitle(index), index); } }
public void setViewPager(ViewPager viewPager) { mPager = viewPager; addTabs(mPager.getAdapter()); }
public ViewPagerTabs(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setFillViewport(true); mSidePadding = (int) (getResources().getDisplayMetrics().density * TAB_SIDE_PADDING_IN_DPS); final TypedArray a = context.obtainStyledAttributes(attrs, ATTRS); mTextSize = a.getDimensionPixelSize(0, 0); mTextStyle = a.getInt(1, 0); mTextColor = a.getColorStateList(2); mTextAllCaps = a.getBoolean(3, false); mTabStrip = new ViewPagerTabStrip(context); addView(mTabStrip, new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); a.recycle(); if (CompatUtils.isLollipopCompatible()) { // enable shadow casting from view bounds setOutlineProvider(VIEW_BOUNDS_OUTLINE_PROVIDER); } }
@Override public boolean onLongClick(View v) { final int[] screenPos = new int[2]; getLocationOnScreen(screenPos); final Context context = getContext(); final int width = getWidth(); final int height = getHeight(); final int screenWidth = context.getResources().getDisplayMetrics().widthPixels; Toast toast = Toast.makeText(context, mPager.getAdapter().getPageTitle(mPosition), Toast.LENGTH_SHORT); // Show the toast under the tab toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, (screenPos[0] + width / 2) - screenWidth / 2, screenPos[1] + height); toast.show(); return true; } }
private void addTab(CharSequence tabTitle, final int position) { View tabView; if (mTabIcons != null && position < mTabIcons.length) { View layout = LayoutInflater.from(getContext()).inflate( R.layout.unread_count_tab, null); View iconView = layout.findViewById(R.id.icon); textView.setText(Integer.toString(mUnreadCounts[position])); textView.setVisibility(View.VISIBLE); iconView.setContentDescription(getResources().getQuantityString( R.plurals.tab_title_with_unread_items, mUnreadCounts[position], final TextView textView = new TextView(getContext()); textView.setText(tabTitle); textView.setBackgroundResource(R.drawable.view_pager_tab_background);
mViewPagerTabs.configureTabIcons(mTabIcons); mViewPagerTabs.setViewPager(mViewPager); addOnPageChangeListener(mViewPagerTabs);
private int getRtlPosition(int position) { if (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) { return mTabStrip.getChildCount() - 1 - position; } return position; } }
private void addTabs(PagerAdapter adapter) { mTabStrip.removeAllViews(); final int count = adapter.getCount(); for (int i = 0; i < count; i++) { addTab(adapter.getPageTitle(i), i); } }