public void notifyDataSetChanged() { tabsContainer.removeAllViews(); tabCount = pager.getAdapter().getCount(); for (int i = 0; i < tabCount; i++) { if (pager.getAdapter() instanceof IconTabProvider) { addIconTab(i, ((IconTabProvider) pager.getAdapter()).getPageIconResId(i)); } else { addTextTab(i, pager.getAdapter().getPageTitle(i).toString()); } } updateTabStyles(); getViewTreeObserver() .addOnGlobalLayoutListener( new OnGlobalLayoutListener() { @SuppressWarnings("deprecation") @SuppressLint("NewApi") @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { getViewTreeObserver().removeOnGlobalLayoutListener(this); } currentPosition = pager.getCurrentItem(); scrollToChild(currentPosition, 0); } }); }
public void notifyDataSetChanged() { tabsContainer.removeAllViews(); tabCount = pager.getAdapter().getCount(); for (int i = 0; i < tabCount; i++) { if (pager.getAdapter() instanceof IconTabProvider) { addIconTab(i, ((IconTabProvider) pager.getAdapter()).getPageIconResId(i)); } else { addTextTab(i, pager.getAdapter().getPageTitle(i).toString()); } } updateTabStyles(); getViewTreeObserver() .addOnGlobalLayoutListener( new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { getViewTreeObserver().removeGlobalOnLayoutListener(this); currentPosition = pager.getCurrentItem(); scrollToChild(currentPosition, 0); } }); }
/** * Notifies the pager indicator that the data set has changed. Be sure to notify the pager as well * (though you may wish to place that call in here yourself). */ public void notifyDataSetChanged() { if (pager != null && pager.getAdapter() != null) { // remove the old items (if any exist) itemContainer.removeAllViews(); // I'm sure this could be optimised a lot more, eg, // by reusing existing ImageViews, but it // does the job well enough for now. items.removeAll(items); // now create the new items. for (int i = 0; i < pager.getAdapter().getCount(); i++) { ImageView item = new ImageView(context); if (i == pager.getCurrentItem()) { item.setImageResource(R.drawable.bulb_lit4); } else { item.setImageResource(R.drawable.bulb_unlit4); } item.setTag(i); item.setOnClickListener(itemClickListener); items.add(item); itemContainer.addView(item); } } }
@Test public void shouldSetAndGetAdapter() throws Exception { assertNull(pager.getAdapter()); pager.setAdapter(adapter); assertSame(adapter, pager.getAdapter()); }
/** Initialize and add all tabs to the layout */ private void initTabs() { removeAllViews(); mTabs.clear(); if (mAdapter == null) return; for (int i = 0; i < mPager.getAdapter().getCount(); i++) { final int index = i; View tab = mAdapter.getView(i); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f); tab.setLayoutParams(params); this.addView(tab); mTabs.add(tab); if (i != mPager.getAdapter().getCount() - 1) { this.addView(getSeparator()); } tab.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { mPager.setCurrentItem(index); } }); } selectTab(mPager.getCurrentItem()); }
public void setUpWithViewPager(final ViewPager viewPager) { if (viewPager != null) { viewPager.addOnPageChangeListener( new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled( int position, float positionOffset, int positionOffsetPixels) { mCurrentIndex = viewPager.getCurrentItem(); mNewIndex = position; mPageOffset = positionOffset; postInvalidate(); } @Override public void onPageSelected(int position) {} @Override public void onPageScrollStateChanged(int state) {} }); if (viewPager.getAdapter() != null) { count = viewPager.getAdapter().getCount(); } } }
private void drawRect(Canvas canvas) { // modified by maddog05 if (mViewPager == null) return; if (mViewPager.getAdapter() == null) return; if (mViewPager.getAdapter().getCount() == 0) return; // finish modified float top = getPaddingTop(); float bottom = top + circleRadius * 2; float moveDistance = circleRadius * 2 + circlePadding; boolean isDragForward = mSelectedPage - mCurrentDragPage < 1; float relativePageOffset = isDragForward ? mPageOffset : 1.0f - mPageOffset; currentRelativePageOffset = relativePageOffset; float shiftedOffset = relativePageOffset * OFFSET_MULTIPLIER_NORMAL; float settleShiftedOffset = Math.max( 0, mapValue( relativePageOffset, startedSettlePageOffset, 1.0f, startedSettleNormalOffset, 1.0f)); float normalOffset = mState == ViewPager.SCROLL_STATE_SETTLING ? settleShiftedOffset : shiftedOffset; currentNormalOffset = normalOffset; float largerOffset = Math.min( mState == ViewPager.SCROLL_STATE_SETTLING ? relativePageOffset * OFFSET_MULTIPLIER_SETTLING : relativePageOffset * OFFSET_MULTIPLIER_DRAG, 1.0f); float circleCenter = getCircleCenter(isDragForward ? mCurrentDragPage : mSelectedPage); float normal = moveDistance * normalOffset; float large = moveDistance * largerOffset; float left = isDragForward ? circleCenter - circleRadius + normal : circleCenter - circleRadius - large; float right = isDragForward ? circleCenter + circleRadius + large : circleCenter + circleRadius - normal; RectF rectF = new RectF(left, top, right, bottom); canvas.drawRoundRect(rectF, circleRadius, circleRadius, activePaint); }
public void initViewPager(ViewPager viewPager) { // modified by maddog05 if (mViewPager == viewPager) return; if (mViewPager != null) viewPager.addOnPageChangeListener(this); if (viewPager.getAdapter() == null) throw new IllegalStateException("ViewPager doesn't have an adapter instance."); mViewPager = viewPager; mViewPager.addOnPageChangeListener(this); // finish modified circleCount = viewPager.getAdapter().getCount(); mCurrentDragPage = viewPager.getCurrentItem(); invalidate(); }
public void addNewTab(String title) { View tabView = View.inflate(mContext, R.layout.layout_tab, null); if (mTitles != null) { mTitles.add(title); } CharSequence pageTitle = mTitles == null ? mViewPager.getAdapter().getPageTitle(mTabCount) : mTitles.get(mTabCount); addTab(mTabCount, pageTitle.toString(), tabView); this.mTabCount = mTitles == null ? mViewPager.getAdapter().getCount() : mTitles.size(); updateTabStyles(); }
/** 更新数据 */ public void notifyDataSetChanged() { mTabsContainer.removeAllViews(); this.mTabCount = mTitles == null ? mViewPager.getAdapter().getCount() : mTitles.size(); View tabView; for (int i = 0; i < mTabCount; i++) { tabView = View.inflate(mContext, R.layout.layout_tab, null); CharSequence pageTitle = mTitles == null ? mViewPager.getAdapter().getPageTitle(i) : mTitles.get(i); addTab(i, pageTitle.toString(), tabView); } updateTabStyles(); }
@Override public void setViewPager(ViewPager view) { if (view.getAdapter() == null) { throw new IllegalStateException("ViewPager does not have adapter instance."); } if (!(view.getAdapter() instanceof TitleProvider)) { throw new IllegalStateException( "ViewPager adapter must implement TitleProvider to be used with TitlePageIndicator."); } mViewPager = view; mViewPager.setOnPageChangeListener(this); mTitleProvider = (TitleProvider) mViewPager.getAdapter(); invalidate(); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d(TAG, "onActivityResult: requestCode: " + requestCode); Log.d(TAG, "onActivityResult: resultCode: " + resultCode); pagerCity.getAdapter().notifyDataSetChanged(); pagerMaterial.getAdapter().notifyDataSetChanged(); getFragmentManager() .beginTransaction() // .setCustomAnimations(R.animator.slide_in_up, R.animator.slide_out_up) .replace(R.id.frame, new CurrentlyFragment(), FRAGMENT_TAG) .commit(); }
/** Notify the view that new data is available. */ public void notifyDatasetChanged() { // remove all old child views this.removeAllViews(); // add new tabs for (int i = 0; i < mPager.getAdapter().getCount(); i++) { addTab(i, ((ViewPagerTabProvider) mPager.getAdapter()).getTitle(i)); } applyStyles(); calculateNewPositions(true); this.requestLayout(); }
public static void removeCurItem(MenuItem item, int idx, ViewPager vp) { for (int i = 0; i < vp.getAdapter().getCount(); i++) { if (item.getItemId() == idx + i + 1) { vp.setCurrentItem(i); } } }
@Override public void perform(UiController uiController, View view) { ViewPager pager = (ViewPager) view; int pagerLength = pager.getAdapter().getCount(); if (mNumSwipes != -1) { pagerLength = Math.min(pagerLength, mNumSwipes); } for (int x = 1; x < pagerLength; x++) { switch (mDirection) { case LEFT: pager.setCurrentItem(pagerLength - (x + 1), true); uiController.loopMainThreadUntilIdle(); // If you'd like the ViewPager to pause // on each swipe for human inspection // replace loopMainThreadUntilIdle // with: // uiController.loopMainThreadForAtLeast(200); break; case RIGHT: pager.setCurrentItem(x, true); uiController.loopMainThreadUntilIdle(); // uiController.loopMainThreadForAtLeast(200); break; } } }
@Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Map<String, String> map = mLocalMusicAdapter.getItem(position); // 取得铃声名 String ringName = map.get(WeacConstants.RING_NAME); // 取得播放地址 String ringUrl = map.get(WeacConstants.RING_URL); // 更新当前铃声选中的位置 mLocalMusicAdapter.updateSelection(ringName); // 更新适配器刷新铃声列表显示 mLocalMusicAdapter.notifyDataSetChanged(); // 设置最后一次选中的铃声选择界面位置为本地音乐界面 RingSelectItem.getInstance().setRingPager(1); // 播放音频文件 AudioPlayer.getInstance(getActivity()).play(ringUrl, false, false); ViewPager pager = (ViewPager) getActivity().findViewById(R.id.fragment_ring_select_sort); PagerAdapter f = pager.getAdapter(); SystemRingFragment systemRingFragment = (SystemRingFragment) f.instantiateItem(pager, 0); RecorderFragment recorderFragment = (RecorderFragment) f.instantiateItem(pager, 2); // 取消系统铃声选中标记 if (systemRingFragment.mSystemRingAdapter != null) { systemRingFragment.mSystemRingAdapter.updateSelection(""); systemRingFragment.mSystemRingAdapter.notifyDataSetChanged(); } // 取消录音选中标记 if (recorderFragment.mRecorderAdapter != null) { recorderFragment.mRecorderAdapter.updateSelection(""); recorderFragment.mRecorderAdapter.notifyDataSetChanged(); } }
private void setCurrentItem(int position) { if (pager != null && pager.getAdapter() != null) { int numberOfItems = pager.getAdapter().getCount(); for (int i = 0; i < numberOfItems; i++) { ImageView item = items.get(i); if (item != null) { if (i == position) { item.setImageResource(R.drawable.bulb_lit4); } else { item.setImageResource(R.drawable.bulb_unlit4); } } } } }
/** * 从这里开始构建导航栏 * * @param viewPager * @return */ public IMode builder(ViewPager viewPager) { mViewPager = viewPager; viewPager.addOnPageChangeListener(this); mTabstrips = new ArrayList<Tabstrip>(); mTexts = new ArrayList<String>(); mCount = viewPager.getAdapter().getCount(); for (int i = 0; i < mCount; i++) { mTexts.add(viewPager.getAdapter().getPageTitle(i).toString()); Tabstrip tabstrip = new Tabstrip(mContext); tabstrip.setTabText(mTexts.get(i)); tabstrip.setTag(i); tabstrip.setOnClickListener(this); mTabstrips.add(tabstrip); } return this; }
private CharSequence getTitle(int i) { CharSequence title = mViewPager.getAdapter().getPageTitle(i); if (title == null) { title = EMPTY_TITLE; } return title; }
@Override public void onActivityCreated(final Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); viewpagerHolder = getActivity().findViewById(R.id.viewpager_holder); fragmentHolder = getActivity().findViewById(R.id.frame_for_fragment); pager = (ViewPager) viewpagerHolder.findViewById(R.id.pager); if (pager.getAdapter() == null) { fragmentAdapter = new MovieFragmentAdapter(getSupportFragmentManager(), 0); pager.setAdapter(fragmentAdapter); indicator = (TitlePageIndicator) viewpagerHolder.findViewById(R.id.indicator); indicator.setViewPager(pager); decoreIndicator(); setIndicatorListener(); } fragmentHolder.setVisibility(View.GONE); viewpagerHolder.setVisibility(View.VISIBLE); }
private void populateTabStrip() { final PagerAdapter adapter = mViewPager.getAdapter(); final View.OnClickListener tabClickListener = new TabClickListener(); for (int i = 0; i < adapter.getCount(); i++) { View tabView = null; TextView tabTitleView = null; if (mTabViewLayoutId != 0) { // If there is a custom tab view layout id set, try and inflate it tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false); tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId); } if (tabView == null) { tabView = createDefaultTabView(getContext()); } if (tabTitleView == null && TextView.class.isInstance(tabView)) { tabTitleView = (TextView) tabView; } tabTitleView.setText(adapter.getPageTitle(i)); tabView.setOnClickListener(tabClickListener); mTabStrip.addView(tabView); } }
@Test public void viewPager_CurrentPage() { MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class); ViewPager viewPager = (ViewPager) mainActivity.findViewById(R.id.viewPager); assertEquals(viewPager.getCurrentItem(), 2); assertEquals(viewPager.getAdapter().getPageTitle(2).toString(), "Today"); }
private void initViewPager() { seekBar.setMax(viewPager.getAdapter().getCount() - 1); seekBar.setOnSeekBarChangeListener(this); seekBar.setProgress(viewPager.getCurrentItem()); viewPager.addOnPageChangeListener( new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled( int position, float positionOffset, int positionOffsetPixels) {} @Override public void onPageSelected(int position) { seekBar.setProgress(position); updateCurPos(position, seekBar.getMax()); } @Override public void onPageScrollStateChanged(int state) {} }); viewPager.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) {} }); updateCurPos(seekBar.getProgress(), seekBar.getMax()); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mViewPager == null) { return; } final int count = mViewPager.getAdapter().getCount(); if (count == 0) { return; } if (mCurrentPage >= count) { setCurrentItem(count - 1); return; } final int paddingLeft = getPaddingLeft(); final float pageWidth = (getWidth() - paddingLeft - getPaddingRight()) / (1f * count); final float left = paddingLeft + pageWidth * (mCurrentPage + mPositionOffset); final float right = left + pageWidth; final float top = getPaddingTop(); final float bottom = getHeight() - getPaddingBottom(); canvas.drawRect(left, top, right, bottom, mPaint); }
private void populateTabStrip() { final PagerAdapter adapter = mViewPager.getAdapter(); final View.OnClickListener tabClickListener = new TabClickListener(); for (int i = 0; i < adapter.getCount(); i++) { View tabView = null; TextView tabTitleView = null; if (mTabViewLayoutId != 0) { // If there is a custom tab view layout id set, try and inflate it tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false); tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId); } if (tabView == null) { tabView = createDefaultTabView(getContext()); } if (tabTitleView == null && TextView.class.isInstance(tabView)) { tabTitleView = (TextView) tabView; } if (mDistributeEvenly) { LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams(); lp.width = 0; lp.weight = 1; } tabTitleView.setText(adapter.getPageTitle(i)); tabView.setOnClickListener(tabClickListener); String desc = mContentDescriptions.get(i, null); if (desc != null) { tabView.setContentDescription(desc); } mTabStrip.addView(tabView); if (i == mViewPager.getCurrentItem()) { tabView.setSelected(true); } } }
@Override public void setViewPager(ViewPager viewPager) { if (mViewPager == viewPager) { return; } if (mViewPager != null) { // Clear us from the old pager. mViewPager.setOnPageChangeListener(null); } if (viewPager.getAdapter() == null) { throw new IllegalStateException("ViewPager does not have adapter instance."); } mViewPager = viewPager; mViewPager.setOnPageChangeListener(this); invalidate(); post( new Runnable() { @Override public void run() { if (mFades) { post(mFadeRunnable); } } }); }
public void notifyDataSetChanged() { mTabLayout.removeAllViews(); PagerAdapter adapter = mViewPager.getAdapter(); IconPagerAdapter iconAdapter = null; if (adapter instanceof IconPagerAdapter) { iconAdapter = (IconPagerAdapter) adapter; } final int count = adapter.getCount(); for (int i = 0; i < count; i++) { CharSequence title = adapter.getPageTitle(i); if (title == null) { title = EMPTY_TITLE; } int iconResId = 0; if (iconAdapter != null) { iconResId = iconAdapter.getIconResId(i); } addTab(i, title, iconResId); } if (mSelectedTabIndex > count) { mSelectedTabIndex = count - 1; } setCurrentItem(mSelectedTabIndex); requestLayout(); }
@Subscribe public void onUserRetrieved(UserRetrievedEvent event) { CWUser u = event.getUser(); if (event.isTaskOver()) UserFragment.this.setRefresh(false); if (event.getError() != null) { Toast.makeText(getSherlockActivity(), event.getError().getMessage(), Toast.LENGTH_LONG) .show(); return; } else if (u == null) return; if (UserFragment.this.username == null) { UserFragment.this.username = u.getUsername(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getSherlockActivity()); prefs.edit().putString(Constants.PREF_USERNAME, u.getUsername()).commit(); } UserFragment.this.u = u; refreshNavigationListAdapter(); if (pagerAdapter == null || viewPager.getAdapter() == null) { viewPager.setAdapter(pagerAdapter = new PagerUserAdapter(getFragmentManager(), u)); pageIndicator.setViewPager(viewPager); pageIndicator.setOnPageChangeListener(UserFragment.this); pageIndicator.setBackgroundColor(getResources().getColor(R.color.coderwall_blue)); pageIndicator.setFooterColor(Color.WHITE); pageIndicator.setFooterIndicatorStyle(IndicatorStyle.Triangle); pageIndicator.setFooterLineHeight(0); } else pagerAdapter.refreshUser(u); }
/** * Determines the width of this view * * @param measureSpec A measureSpec packed into an int * @return The width of the view, honoring constraints from measureSpec */ private int measureLong(int measureSpec) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) { // We were told how big to be result = specSize; } else { // Calculate the width according the views count final int count = mViewPager.getAdapter().getCount(); result = (int) (getPaddingLeft() + getPaddingRight() + (count * 2 * mRadius) + (count - 1) * mRadius + 1); // Respect AT_MOST value if that was what is called for by measureSpec if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; }
private void populateTabStrip() { final PagerAdapter adapter = viewPager.getAdapter(); for (int i = 0; i < adapter.getCount(); i++) { final View tabView = (tabProvider == null) ? createDefaultTabView(adapter.getPageTitle(i)) : tabProvider.createTabView(tabStrip, i, adapter); if (tabView == null) { throw new IllegalStateException("tabView is null."); } if (distributeEvenly) { LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams(); lp.width = 0; lp.weight = 1; } if (internalTabClickListener != null) { tabView.setOnClickListener(internalTabClickListener); } tabStrip.addView(tabView); if (i == viewPager.getCurrentItem()) { tabView.setSelected(true); } } }