android.support.v4.view.PagerAdapter Java Examples

The following examples show how to use android.support.v4.view.PagerAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SplashActivity.java    From Tok-Android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void showGuideView() {
    mViewPager.setVisibility(View.VISIBLE);
    List<View> viewList = new ArrayList<>();
    viewList.add(ViewUtil.inflateViewById(this, R.layout.layout_guide1));
    viewList.add(ViewUtil.inflateViewById(this, R.layout.layout_guide2));
    View lastView = ViewUtil.inflateViewById(this, R.layout.layout_guide3);
    TextView enter = lastView.findViewById(R.id.id_guide_enter_btn);
    enter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mSplashPresenter.enter();
        }
    });
    viewList.add(lastView);
    PagerAdapter vpAdapter = new GuideAdapter(viewList);
    mViewPager.setAdapter(vpAdapter);
    mIndicator.setViewPager(mViewPager, viewList.size());
}
 
Example #2
Source File: MainActivity.java    From phphub-android with Apache License 2.0 6 votes vote down vote up
protected void setupTabView() {
    final LayoutInflater inflater = LayoutInflater.from(this);
    final int[] tabIcons = {R.drawable.ic_recommended, R.drawable.ic_topics, R.drawable.ic_wiki, R.drawable.ic_me};
    FragmentPagerItems pages = FragmentPagerItems.with(this)
            .add(R.string.recommended, RecommendedFragment.class)
            .add(R.string.topics, TopicsFragment.class)
            .add(R.string.wiki, WikiFragment.class)
            .add(R.string.me, MeFragment.class)
            .create();
    FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
            getSupportFragmentManager(),
            pages);

    viewPager.setOffscreenPageLimit(pages.size());
    viewPager.setAdapter(adapter);
    viewpagerTab.setCustomTabView(new SmartTabLayout.TabProvider() {
        @Override
        public View createTabView(ViewGroup container, int position, PagerAdapter pagerAdapter) {
            View view = inflater.inflate(R.layout.custom_tab_icon, container, false);
            ImageView iconView = (ImageView) view.findViewById(R.id.iv_icon);
            iconView.setBackgroundResource(tabIcons[position % tabIcons.length]);
            return view;
        }
    });
    viewpagerTab.setViewPager(viewPager);
}
 
Example #3
Source File: MyViewPager.java    From ZhihuDaily with Apache License 2.0 6 votes vote down vote up
private void scrollOnce() {
    PagerAdapter adapter = getAdapter();
    int currentItem = getCurrentItem();
    int count;
    if (adapter == null || (count = adapter.getCount()) < 1) {
        stopAutoScroll();
        return;
    }
    if (currentItem < count) {
        currentItem++;
    }
    if (currentItem == count) {
        currentItem = 0;
    }
    L.i(TAG, "currentItem : " + currentItem);
    setCurrentItem(currentItem);
}
 
Example #4
Source File: IconPageIndicator.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
@Override
public void setViewPager(ViewPager view) {
    if (mViewPager == view) {
        return;
    }
    if (mViewPager != null) {
        mViewPager.setOnPageChangeListener(null);
    }
    PagerAdapter adapter = view.getAdapter();
    if (adapter == null) {
        throw new IllegalStateException("ViewPager does not have adapter instance.");
    }
    mViewPager = view;
    view.setOnPageChangeListener(this);
    notifyDataSetChanged();
}
 
Example #5
Source File: BackHandlerHelper.java    From FragmentBackHandler with Apache License 2.0 6 votes vote down vote up
/**
 * 将back事件分发给ViewPager中的Fragment,{@link #handleBackPress(FragmentManager)} 已经实现了对ViewPager的支持,所以自行决定是否使用该方法
 *
 * @return 如果处理了back键则返回 <b>true</b>
 * @see #handleBackPress(FragmentManager)
 * @see #handleBackPress(Fragment)
 * @see #handleBackPress(FragmentActivity)
 */
public static boolean handleBackPress(ViewPager viewPager) {
    if (viewPager == null) return false;

    PagerAdapter adapter = viewPager.getAdapter();

    if (adapter == null) return false;

    int currentItem = viewPager.getCurrentItem();
    Fragment fragment;
    if (adapter instanceof FragmentPagerAdapter) {
        fragment = ((FragmentPagerAdapter) adapter).getItem(currentItem);
    } else if (adapter instanceof FragmentStatePagerAdapter) {
        fragment = ((FragmentStatePagerAdapter) adapter).getItem(currentItem);
    } else {
        fragment = null;
    }
    return isFragmentBackHandled(fragment);
}
 
Example #6
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_earthquake_main);
  Toolbar toolbar = findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);

  ViewPager viewPager = findViewById(R.id.view_pager);
  if (viewPager != null) {
    PagerAdapter pagerAdapter =
      new EarthquakeTabsPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(pagerAdapter);
    TabLayout tabLayout = findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);
  }

  // Retrieve the Earthquake View Model for this Activity.
  earthquakeViewModel = ViewModelProviders.of(this)
                          .get(EarthquakeViewModel.class);
}
 
Example #7
Source File: PagerTabsIndicator.java    From PagerTabIndicator with Apache License 2.0 6 votes vote down vote up
public void setViewPager(ViewPager viewPager) {
    if (viewPager == null) {
        tabsContainer.removeAllViews();
        if (this.viewPager != null) {
            this.viewPager.removeOnAdapterChangeListener(adapterChangeListener);
            this.viewPager.removeOnPageChangeListener(this);
        }
        this.viewPager = viewPager;
        return;
    }
    this.viewPager = viewPager;
    this.viewPager.addOnPageChangeListener(this);
    adapterChangeListener = new ViewPager.OnAdapterChangeListener() {
        @Override
        public void onAdapterChanged(@NonNull ViewPager viewPager, @Nullable PagerAdapter oldAdapter, @Nullable PagerAdapter newAdapter) {
            listenToAdapterChanges(newAdapter);
        }
    };
    viewPager.addOnAdapterChangeListener(adapterChangeListener);

    listenToAdapterChanges(viewPager.getAdapter());
}
 
Example #8
Source File: BezierIndicatorView.java    From KotlinMVPRxJava2Dagger2GreenDaoRetrofitDemo with Apache License 2.0 6 votes vote down vote up
public void setAdapter(PagerAdapter pagerAdapter) {
    if (mIndicatorPathList.size() > 0) mIndicatorPathList.clear();
    if (mCirclePaths.size() > 0) mCirclePaths.clear();
    if (null != pagerAdapter) {
        this.mPagerAdapter = pagerAdapter;
    }
    if (mHeight != 0) {
        this.mIndicatorCount = mPagerAdapter.getCount();
        for (int i = 0; i < mIndicatorCount; i++) {
            Indicator indicator = generateIndicator(i);
            Path path = calculateUnderHalfPath(indicator);
            mCirclePaths.add(path);
            mDefaultIndicatorList.add(indicator);
        }
        mCurrentPath = mCirclePaths.get(0);
        requestLayout();
        invalidate();
    }
}
 
Example #9
Source File: IconPageIndicator.java    From monolog-android with MIT License 6 votes vote down vote up
@Override
public void setViewPager(ViewPager view) {
    if (mViewPager == view) {
        return;
    }
    if (mViewPager != null) {
        mViewPager.setOnPageChangeListener(null);
    }
    PagerAdapter adapter = view.getAdapter();
    if (adapter == null) {
        throw new IllegalStateException("ViewPager does not have adapter instance.");
    }
    mViewPager = view;
    view.setOnPageChangeListener(this);
    notifyDataSetChanged();
}
 
Example #10
Source File: TabPageIndicator.java    From zhangshangwuda with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #11
Source File: TabPageIndicator.java    From Noyze with Apache License 2.0 6 votes vote down vote up
@Override
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();
}
 
Example #12
Source File: TabPageIndicator.java    From HomeGenie-Android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setViewPager(ViewPager view) {
    if (mViewPager == view) {
        return;
    }
    if (mViewPager != null) {
        mViewPager.setOnPageChangeListener(null);
    }
    final PagerAdapter adapter = view.getAdapter();
    if (adapter == null) {
        throw new IllegalStateException("ViewPager does not have adapter instance.");
    }
    mViewPager = view;
    view.setOnPageChangeListener(this);
    notifyDataSetChanged();
}
 
Example #13
Source File: AutoScrollViewPager.java    From AutoScrollViewPager with Apache License 2.0 5 votes vote down vote up
@Override
public void setAdapter(PagerAdapter adapter) {
    if (wrappedPagerAdapter != null && mObserver != null) {
        wrappedPagerAdapter.unregisterDataSetObserver(mObserver);
    }
    wrappedPagerAdapter = adapter;
    if (wrappedPagerAdapter != null && mObserver != null) {
        wrappedPagerAdapter.registerDataSetObserver(mObserver);
    }
    wrapperPagerAdapter = (wrappedPagerAdapter == null) ? null : new AutoScrollPagerAdapter(adapter);
    super.setAdapter(wrapperPagerAdapter);

    setCurrentItem(0, false);
}
 
Example #14
Source File: ExploreFragment.java    From FriendBook with GNU General Public License v3.0 5 votes vote down vote up
private void setAdapter(final PagerAdapter adapter) {
    viewPager.setOffscreenPageLimit(adapter.getCount() - 1);
    viewPager.setAdapter(adapter);
    CountEventHelper.countExploreTab(getContext(),
            adapter.getPageTitle(viewPager.getCurrentItem()).toString());
    viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            CountEventHelper.countExploreTab(getContext(),
                    adapter.getPageTitle(position).toString());
        }
    });
    tab.setupWithViewPager(viewPager);
}
 
Example #15
Source File: JazzyViewPager.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public View findViewFromObject(int position) {
    Object o = mObjs.get(Integer.valueOf(position));
    if (o == null) {
        return null;
    }
    PagerAdapter a = getAdapter();
    View v;
    for (int i = 0; i < getChildCount(); i++) {
        v = getChildAt(i);
        if (a.isViewFromObject(v, o))
            return v;
    }
    return null;
}
 
Example #16
Source File: HotTabPageIndicator.java    From letv with Apache License 2.0 5 votes vote down vote up
public void notifyDataSetChanged() {
    PagerAdapter adapter = this.mViewPager.getAdapter();
    int count = adapter.getCount();
    if (count != 0) {
        int width = TAB_MARGIN;
        for (int i = 0; i < count; i++) {
            width += getTabWidth(adapter.getPageTitle(i)) + TAB_MARGIN;
        }
        this.mMeanWidth = -1;
        if (this.mLayoutWidth > width && count <= 7) {
            this.mMeanWidth = this.mLayoutWidth / count;
        }
        super.notifyDataSetChanged();
    }
}
 
Example #17
Source File: BaseNavActivity.java    From octoandroid with GNU General Public License v3.0 5 votes vote down vote up
protected void inflateAdapter(PagerAdapter pagerAdapter) {
    if (mViewPager.getAdapter() == null) {
        setAdapterAndTabLayout(pagerAdapter);
    } else {
        PagerAdapter currentAdapter = mViewPager.getAdapter();
        Class adapterClass = currentAdapter.getClass();
        if (!adapterClass.equals(pagerAdapter.getClass())) {
            setAdapterAndTabLayout(pagerAdapter);
        } else {
            String message = pagerAdapter.getClass().getName() + " is already visible";
            Timber.d(message);
        }
    }
}
 
Example #18
Source File: MaterialScrollingViewPager.java    From material-scrolling with Apache License 2.0 5 votes vote down vote up
private ObservableRecyclerView findRecyclerViewFrom(final int position) {
    final PagerAdapter adapter = getAdapter();
    if (adapter instanceof ContainRecyclerViewPagerAdapter) {
        return ((ContainRecyclerViewPagerAdapter) adapter).getRecyclerView(position);
    }
    return null;
}
 
Example #19
Source File: TabLayout.java    From tns-core-modules-widgets with Apache License 2.0 5 votes vote down vote up
private void populateTabStrip() {
    final PagerAdapter adapter = mViewPager.getAdapter();
    final OnClickListener tabClickListener = new TabClickListener();

    for (int i = 0; i < adapter.getCount(); i++) {
        View tabView = null;

        TabItemSpec tabItem;
        if (this.mTabItems != null && this.mTabItems.length > i) {
            tabItem = this.mTabItems[i];
        } else {
            tabItem = new TabItemSpec();
            tabItem.title = adapter.getPageTitle(i).toString();
        }

        tabView = createDefaultTabView(getContext(), tabItem);

        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);
        }
    }
}
 
Example #20
Source File: ArrayPagerAdapter.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Override
public int getItemPosition(Object o) {
    Integer result = positionDelta.get(o);

    if (result == null) {
        return (PagerAdapter.POSITION_UNCHANGED);
    }

    return (result);
}
 
Example #21
Source File: CocoQuery.java    From COCOFramework with Apache License 2.0 5 votes vote down vote up
/**
 * pager的adapter
 *
 * @param mAdapter
 * @return
 */
public ExtViewQuery adapter(final PagerAdapter mAdapter) {
    if (view instanceof ViewPager) {
        ((ViewPager) view).setAdapter(mAdapter);
    }
    return self();
}
 
Example #22
Source File: MainBottomTabLayout.java    From WechatLikeBottomTabUI with Apache License 2.0 5 votes vote down vote up
private void populateTabLayout() {
    final PagerAdapter adapter = mViewPager.getAdapter();
    final OnClickListener tabClickListener = new TabClickListener();
    mIconLayouts = new View[adapter.getCount()];

    for (int i = 0; i < adapter.getCount(); i++) {

        final View tabView = LayoutInflater.from(getContext()).inflate(R.layout.layout_mainbottom_tab, this, false);
        mIconLayouts[i] = tabView;
        TabIconView iconView = (TabIconView) tabView.findViewById(R.id.main_bottom_tab_icon);
        iconView.init(mIconRes[i][0], mIconRes[i][1]);
        TextView textView = (TextView) tabView.findViewById(R.id.main_bottom_tab_text);
        textView.setText(mTitles[i]);

        if (tabView == null) {
            throw new IllegalStateException("tabView is null.");
        }

        LayoutParams lp = (LayoutParams) tabView.getLayoutParams();
        lp.width = 0;
        lp.weight = 1;

        tabView.setOnClickListener(tabClickListener);
        addView(tabView);

        if (i == mViewPager.getCurrentItem()) {
            iconView.transformPage(0);
            tabView.setSelected(true);
            textView.setTextColor(mTextSelectedColor);
        }
    }
}
 
Example #23
Source File: FragmentAdapter.java    From MVVM-JueJin with MIT License 5 votes vote down vote up
@BindingAdapter(value = {"fragments", "curIndex"}, requireAll = false)
public static void bind(ViewPager container, final List<? extends Fragment> fragments, int curIndex) {
    if(!(container.getContext() instanceof FragmentActivity))
        throw new IllegalArgumentException(TAG + "context must instanceof FragmentActivity");
    FragmentManager fm = ((FragmentActivity) container.getContext()).getSupportFragmentManager();

    if (fragments != null && !fragments.isEmpty()) {
        PagerAdapter adapter = container.getAdapter();
        if(adapter == null) {
            // initialize, fragments are only added once !!!
            container.setAdapter(adapter = new FragmentPagerAdapter(fm) {
                @Override
                public Fragment getItem(int position) {
                    return fragments.get(position);
                }

                @Override
                public int getCount() {
                    return fragments.size();
                }
            });
        }

        adapter.notifyDataSetChanged();

        if(curIndex >= 0 && curIndex < fragments.size())
            container.setCurrentItem(curIndex);
    }
}
 
Example #24
Source File: SliderLayout.java    From LoyalNativeSlider with MIT License 5 votes vote down vote up
private SliderAdapter getRealAdapter() {
    PagerAdapter adapter = mViewPager.getAdapter();
    if (adapter != null) {
        return ((InfinitePagerAdapter) adapter).getRealAdapter();
    }
    return null;
}
 
Example #25
Source File: PagerIndicator.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * clear self means unregister the dataset observer and remove all the child views(indicators).
 */
public void destroySelf(){
    if(mPager == null || mPager.getAdapter() == null){
        return;
    }
    InfinitePagerAdapter wrapper = (InfinitePagerAdapter)mPager.getAdapter();
    PagerAdapter adapter = wrapper.getRealAdapter();
    if(adapter!=null){
        adapter.unregisterDataSetObserver(dataChangeObserver);
    }
    removeAllViews();
}
 
Example #26
Source File: SmartTabLayout.java    From RecyclerPager with Apache License 2.0 5 votes vote down vote up
@Override protected void populateTabStrip() {
    final PagerAdapter adapter = viewPager.getAdapter();
    if (adapter == null) {
        return;
    }
    for (int i = 0; i < adapter.getCount(); i++) {

        CharSequence title = adapter.getPageTitle(i);
        final View tabView = (tabProvider == null) ? createDefaultTabView(title)
                : tabProvider.createTabView(tabStrip, i, title);
        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 == getCurrentItem()) {
            tabView.setSelected(true);
        }
    }
}
 
Example #27
Source File: MainActivity.java    From text_converter with GNU General Public License v3.0 5 votes vote down vote up
protected PagerAdapter getPageAdapter(String initValue) {
    if (Premium.isPremium(this)) {
        return new PagerSectionAdapterPro(this, getSupportFragmentManager(), initValue);
    } else {
        return new PagerSectionAdapter(this, getSupportFragmentManager(), initValue);
    }
}
 
Example #28
Source File: TabActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.app_bar_tabs);

  ViewPager viewPager = findViewById(R.id.view_pager);
  PagerAdapter pagerAdapter =
    new FixedTabsPagerAdapter(getSupportFragmentManager());
  viewPager.setAdapter(pagerAdapter);

  TabLayout tabLayout = findViewById(R.id.tab_layout);
  tabLayout.setupWithViewPager(viewPager);
}
 
Example #29
Source File: Tutorial.java    From Slide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getTheme().applyStyle(new FontPreferences(this).getCommentFontStyle().getResId(), true);
    getTheme().applyStyle(new FontPreferences(this).getPostFontStyle().getResId(), true);

    getTheme().applyStyle(new ColorPreferences(this).getFontStyle().getBaseId(), true);

    setContentView(R.layout.activity_tutorial);


    // Instantiate a ViewPager and a PagerAdapter.
    mPager = findViewById(R.id.vp);
    /*
  The pager adapter, which provides the pages to the view pager widget.
 */
    PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);

    if(getIntent().hasExtra("page")){
        mPager.setCurrentItem(1);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = this.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Palette.getDarkerColor(Color.parseColor("#FF5252")));
    }

}
 
Example #30
Source File: LoopViewPager.java    From LoopingViewPager with Apache License 2.0 5 votes vote down vote up
@Override
public void setAdapter(PagerAdapter adapter) {
    mAdapter = new LoopPagerAdapterWrapper(adapter);
    mAdapter.setBoundaryCaching(mBoundaryCaching);
    super.setAdapter(mAdapter);
    setCurrentItem(0, false);
}