Java Code Examples for android.support.v4.view.ViewPager#addOnPageChangeListener()

The following examples show how to use android.support.v4.view.ViewPager#addOnPageChangeListener() . 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: AboutActivity.java    From android_gisapp with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about);
    setToolbar(R.id.main_toolbar);

    mViewPager = (ViewPager) findViewById(com.nextgis.maplibui.R.id.viewPager);
    PagerAdapter adapter = new TabsAdapter(getSupportFragmentManager());
    mViewPager.setAdapter(adapter);
    mViewPager.addOnPageChangeListener(this);

    TabLayout tabLayout = (TabLayout) findViewById(com.nextgis.maplibui.R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    TextView txtCopyrightText = (TextView) findViewById(R.id.copyright);
    txtCopyrightText.setText(Html.fromHtml(getString(R.string.copyright)));
    txtCopyrightText.setMovementMethod(LinkMovementMethod.getInstance());
}
 
Example 2
Source File: PageFragment.java    From ChangeTabLayout with Apache License 2.0 6 votes vote down vote up
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ViewPager mViewPager = (ViewPager) view.findViewById(R.id.vp);
    FragmentPagerItems pages = new FragmentPagerItems(getActivity());
    final int page = FragmentPagerItem.getPosition(getArguments());

    for (int i = 0; i < 5; i++) {
        Bundle bundle = new Bundle();
        bundle.putInt("page", page);
        bundle.putInt("position", i);
        pages.add(FragmentPagerItem.of(String.valueOf(i), DemoFragment.class, bundle));
    }
    FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(getChildFragmentManager(), pages); //Fragment嵌套Fragment时,要用getChildFragmentManager
    mViewPager.setAdapter(adapter);
    mViewPager.setOffscreenPageLimit(2);
    mViewPager.setPageMargin(30);
    final MainActivity mainActivity = (MainActivity) getActivity();
    mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            mainActivity.pageScrolled(page, position, positionOffset);
        }
    });
}
 
Example 3
Source File: CircularTabLayout.java    From CircularViewPager with MIT License 6 votes vote down vote up
private void setupWithViewPager(@NonNull ViewPager viewPager) {
    mViewPager = viewPager;

    PagerAdapter adapter = viewPager.getAdapter();
    if (adapter == null) {
        throw new IllegalArgumentException("ViewPager does not have a PagerAdapter set");
    }

    setTabsFromPagerAdapter(adapter);

    viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        public void onPageSelected(int position) {
            if (!mScrollingByManual) {
                smoothScrollToPosition(mCircularTabLayoutAdapter.moveToPosition(position));
            }
            mScrollingByManual = false;
        }
    });
}
 
Example 4
Source File: QQBrowserDemoActivity.java    From CoordinatorLayoutExample with Apache License 2.0 6 votes vote down vote up
protected void initData() {
        initBehavior();
        mNewsPager = (ViewPager) findViewById(R.id.id_uc_news_content);
        mTableLayout = (TabLayout) findViewById(R.id.id_uc_news_tab);
        mFragments = new ArrayList<TestFragment>();
        for (int i = 0; i < 4; i++) {
            mFragments.add(TestFragment.newInstance(String.valueOf(i), false));
            mTableLayout.addTab(mTableLayout.newTab().setText("Tab" + i));
        }
        mTableLayout.setTabMode(TabLayout.MODE_FIXED);
        mTableLayout.setOnTabSelectedListener(this);
        mNewsPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTableLayout));
        mNewsPager.setAdapter(new TestFragmentAdapter(mFragments, getSupportFragmentManager()));
        findViewById(R.id.news_tv_header_pager).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(QQBrowserDemoActivity.this, "点击我了", Toast.LENGTH_SHORT).show();
            }
        });
//        setViewPagerScrollEnable(mNewsPager,false);
    }
 
Example 5
Source File: ViewPageActivity.java    From AndroidAgeraTutorial with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_guide);
    ViewPager mViewPager = (ViewPager) findViewById(R.id.view_page);

    final GuideAdapter guideAdapter = new GuideAdapter(ViewPageActivity.this);
    guideAdapter.addAll(getGuideViewModels());
    mViewPager.setAdapter(guideAdapter);
    mViewPager.addOnPageChangeListener(this);

    mWelcomeDotView = (ViewPageDotView) findViewById(R.id.dot_view);
    mWelcomeDotView.setNumOfCircles(guideAdapter.getCount(), getResources().getDimensionPixelSize(R.dimen.height_very_small));

}
 
Example 6
Source File: FlycoPageIndicaor.java    From FlycoPageIndicator with MIT License 5 votes vote down vote up
@Override
public void setViewPager(ViewPager vp, int realCount) {
    this.vp = vp;
    if (isValid()) {
        this.count = realCount;
        vp.removeOnPageChangeListener(this);
        vp.addOnPageChangeListener(this);

        createIndicators();
    }
}
 
Example 7
Source File: SmartTabLayout.java    From RecyclerPager with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the associated view pager. Note that the assumption here is that the pager content
 * (number of tabs and tab titles) does not change after this call has been made.
 *
 * @param viewPager viewPager
 */
public void setViewPager(ViewPager viewPager) {
    tabStrip.removeAllViews();
    this.viewPager = viewPager;
    if (viewPager != null && viewPager.getAdapter() != null) {
        viewPager.addOnPageChangeListener(new InternalViewPagerListener());
        populateTabStrip();
    }
}
 
Example 8
Source File: Gallery.java    From Gallery with Apache License 2.0 5 votes vote down vote up
public Gallery(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Gallery);
    Constant.GALLEY_SCALE_RATE = a.getFloat(R.styleable.Gallery_scaleP, Constant.GALLEY_SCALE_RATE);
    int galleryPadding = a.getDimensionPixelSize(R.styleable.Gallery_gallery_padding, 0);
    if (galleryPadding > 0) {
        galleryPaddingLeft = galleryPaddingRight = galleryPaddingTop = galleryPaddingBottom = galleryPadding;
    } else {
        galleryPaddingLeft = a.getDimensionPixelSize(R.styleable.Gallery_gallery_padding_left, galleryPaddingLeft);
        galleryPaddingRight = a.getDimensionPixelSize(R.styleable.Gallery_gallery_padding_right, galleryPaddingRight);
        galleryPaddingTop = a.getDimensionPixelSize(R.styleable.Gallery_gallery_padding_top, galleryPaddingTop);
        galleryPaddingBottom = a.getDimensionPixelSize(R.styleable.Gallery_gallery_padding_bottom, galleryPaddingBottom);
    }
    galleryCenterMargin = a.getDimensionPixelSize(R.styleable.Gallery_gallery_pics_space, galleryCenterMargin);
    a.recycle();
    
    LayoutInflater.from(context).inflate(R.layout.gallery_layout, this, true);
    viewPager = (ViewPager) findViewById(R.id.view_pager);
    ((MarginLayoutParams)viewPager.getLayoutParams()).setMargins(galleryPaddingLeft, galleryPaddingTop, galleryPaddingRight, galleryPaddingBottom);
    viewPager.setPageMargin(galleryCenterMargin);

    viewPager.setOffscreenPageLimit(OFFSCREEN_PAGE_LIMIT);
    setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return viewPager.dispatchTouchEvent(event);
        }
    });
    viewPager.addOnPageChangeListener(new PageChangeListener());
}
 
Example 9
Source File: MainActivity.java    From QuickLyric with GNU General Public License v3.0 5 votes vote down vote up
private void setupDemoScreen() {
    ViewGroup rootView = findViewById(id.root_view);
    getLayoutInflater().inflate(layout.tutorial_view, rootView);
    final ViewPager pager = findViewById(id.pager);
    CircleIndicator indicator = findViewById(R.id.indicator);
    final IntroScreenSlidePagerAdapter pagerAdapter = new IntroScreenSlidePagerAdapter(this);
    pager.setAdapter(pagerAdapter);
    pager.addOnPageChangeListener(pagerAdapter);
    indicator.setViewPager(pager);
    pager.setCurrentItem(pagerAdapter.rightToLeft ? pagerAdapter.getCount() - 1 : 0);
    indicator.setOnPageChangeListener(pagerAdapter);
    Button skipButton = rootView.findViewById(id.pager_button);
    ImageButton arrowButton = rootView.findViewById(id.pager_arrow);
    skipButton.setOnClickListener(v -> {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
            pagerAdapter.exitAction();
        else
            pager.setCurrentItem(pagerAdapter.getCount() - 1);
        Bundle bundle = new Bundle();
        bundle.putInt("current_page", pager.getCurrentItem());
    });
    arrowButton.setOnClickListener(v -> pagerAdapter.nextAction());
    if (mDrawerToggle != null)
        ((DrawerLayout) drawer).setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    focusOnFragment = false;
    invalidateOptionsMenu();
}
 
Example 10
Source File: ToolBarActivity.java    From Common with Apache License 2.0 5 votes vote down vote up
private void initScrollTab(ScrollTab[] tabs, final ViewPager pager, List<String> titles) {
    final ArrayList<Fragment> fragments = new ArrayList<>();
    for (int i = 0; i < titles.size(); i++) {
        TabFragment fragment = new TabFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("index", i);
        fragment.setArguments(bundle);
        fragments.add(fragment);
    }
    FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Fragment getItem(int arg0) {
            return fragments.get(arg0);
        }
    };
    pager.setOffscreenPageLimit(titles.size() - 1);
    pager.setAdapter(fragmentPagerAdapter);
    pager.addOnPageChangeListener(this);
    for (ScrollTab tab : tabs) {
        tab.setTitles(titles);
        tab.setNumber(1, "9", View.VISIBLE);//Set the red dot
        tab.setViewPager(pager);
        tab.setOnTabListener(new ScrollTab.OnTabListener() {
            @Override
            public void onChange(int index, View v) {
                pager.setCurrentItem(index, true);
            }
        });
    }
}
 
Example 11
Source File: PageIndicator.java    From Pasta-Music with Apache License 2.0 5 votes vote down vote up
/**
 * You must call this AFTER setting the Adapter for the ViewPager, or it won't display the right amount of points.
 * @param viewPager
 */
public void setViewPager(ViewPager viewPager) {
    mViewPager = viewPager;
    viewPager.addOnPageChangeListener(this);
    mTotalPages = viewPager.getAdapter().getCount();
    invalidate();
}
 
Example 12
Source File: RectIndicator.java    From ViewPagerHelper with Apache License 2.0 5 votes vote down vote up
/**
 * 添加数据
 * @param viewPager
 */
public void addPagerData(int count, ViewPager viewPager) {

    removeAllViews();
    if (count == 0) {
        return;
    }
    mCount = count;
    mViewPager = viewPager;
    GradientDrawable drawable = new GradientDrawable();
    drawable.setShape(GradientDrawable.RECTANGLE);
    drawable.setSize(mRectWidth, mRectHeight);
    drawable.setCornerRadius(mRoundSize);
    drawable.setColor(mDefaultColor);
    for (int i = 0; i < mCount; i++) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        if (i == mCount - 1) {
            params.setMargins(mMargin, 0, mMargin, 0);
        } else {
            params.setMargins(mMargin, 0, 0, 0);
        }

        ImageView imageView = new ImageView(getContext());

        imageView.setBackground(drawable);
        imageView.setLayoutParams(params);
        addView(imageView);
    }
    if (viewPager != null) {
        viewPager.addOnPageChangeListener(new PagerListener());
    }
}
 
Example 13
Source File: FlycoPageIndicaor.java    From FlycoPageIndicator with MIT License 5 votes vote down vote up
@Override
public void setViewPager(ViewPager vp) {
    this.vp = vp;
    if (isValid()) {
        this.count = vp.getAdapter().getCount();
        vp.removeOnPageChangeListener(this);
        vp.addOnPageChangeListener(this);

        createIndicators();
    }
}
 
Example 14
Source File: CircleIndicator.java    From ViewPagerHelper with Apache License 2.0 5 votes vote down vote up
/**
 * 添加数据
 * @param viewPager
 */
public void addPagerData(int count, ViewPager viewPager) {

    /**
     * 还原一些状态
     */
    removeAllViews();
    mMoveDistance = 0;

    if (count == 0) {
        return;
    }
    mViewPager = viewPager;
    mCount = count;

    GradientDrawable drawable = new GradientDrawable();
    drawable.setShape(GradientDrawable.OVAL);
    drawable.setSize(mSize, mSize);
    drawable.setColor(mDefaultColor);
    for (int i = 0; i < mCount; i++) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        if (i == mCount - 1) {
            params.setMargins(mMargin, 0, mMargin, 0);
        } else {
            params.setMargins(mMargin, 0, 0, 0);
        }

        ImageView imageView = new ImageView(getContext());

        imageView.setBackground(drawable);
        imageView.setLayoutParams(params);
        addView(imageView);
    }
    if (viewPager != null) {
        viewPager.addOnPageChangeListener(new PagerListener());
    }
}
 
Example 15
Source File: BannerView.java    From MvpRoute with Apache License 2.0 4 votes vote down vote up
/**
	 * 初始化BannerView  加入ViewPager 加入RadioGroup
	 *
	 * @param context
	 * @param attrs
	 */
	private void init(Context context, AttributeSet attrs) {
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BannerView);

		try {
			isPlayStart = typedArray.getBoolean(R.styleable.BannerView_start_plary, DEFAULAT_PALYER);
			selectTab = typedArray.getResourceId(R.styleable.BannerView_tab_select, 0);
			tabMargin = typedArray.getDimensionPixelSize(R.styleable.BannerView_tab_margin, DEFAULT_MARGIN);
			tabBottomMargin = typedArray.getDimensionPixelSize(R.styleable.BannerView_tab_bottom_margin, DEFAULT_BOOTTOM_MARGIN);
			speed = typedArray.getInt(R.styleable.BannerView_viewpager_speed, DEFAULT_VIEWPAGER_SPEED);
			intervalTime = typedArray.getInt(R.styleable.BannerView_intervalTime, DEFAULT_INTERVATIME);
			tabWidth = typedArray.getDimensionPixelSize(R.styleable.BannerView_tab_width, DEFAULT_TAB_WIDTH);
			tabHeight = typedArray.getDimensionPixelSize(R.styleable.BannerView_tab_height, DEFAULT_TAB_HEIGHT);
//			int type = typedArray.get(R.styleable.BannerView_nav_gravity,);
//			LogUtils.d("type==>" + type);

		} finally {
			typedArray.recycle();
		}

		imgs = new LinkedList<>();
		titles = new LinkedList<>();

		viewpager = new ViewPager(context);
		//设置viewpager的切换时间  默认切换时间为1000 单位毫秒
		setPageChangeDuration(speed);
		//设置viewpager的页面切换监听
		viewpager.addOnPageChangeListener(this);
		viewpager.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
		//添加view
		addView(viewpager);
		//创建指示器控件
		radioGroup = new RadioGroup(context);
		radioGroup.setOrientation(RadioGroup.HORIZONTAL);
		LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		//设置下方偏移量
		params.setMargins(0, 0, 0, tabBottomMargin);
		//设置相对属性
		params.addRule(RelativeLayout.CENTER_HORIZONTAL);
		params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		radioGroup.setLayoutParams(params);
		addView(radioGroup);
	}
 
Example 16
Source File: ShadowTransformer.java    From ViewPagerCards with Apache License 2.0 4 votes vote down vote up
public ShadowTransformer(ViewPager viewPager, CardAdapter adapter) {
    mViewPager = viewPager;
    viewPager.addOnPageChangeListener(this);
    mAdapter = adapter;
}
 
Example 17
Source File: SmileyPicker.java    From iBeebo with GNU General Public License v3.0 4 votes vote down vote up
public SmileyPicker(Context paramContext, AttributeSet paramAttributeSet) {
    super(paramContext, paramAttributeSet);
    mContext = paramContext;
    LayoutInflater mInflater = LayoutInflater.from(paramContext);
    View view = mInflater.inflate(R.layout.writeweiboactivity_smileypicker, null);

    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    viewPager.setAdapter(new SmileyPagerAdapter());
    leftPoint = (ImageView) view.findViewById(R.id.left_point);
    centerPoint = (ImageView) view.findViewById(R.id.center_point);
    rightPoint = (ImageView) view.findViewById(R.id.right_point);

    if (true) {
        rightPoint.setVisibility(View.VISIBLE);
    } else {
        rightPoint.setVisibility(View.GONE);
    }
    leftPoint.getDrawable().setLevel(1);
    viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            switch (position) {
                case 0:
                    leftPoint.getDrawable().setLevel(1);
                    centerPoint.getDrawable().setLevel(0);
                    rightPoint.getDrawable().setLevel(0);
                    break;
                case 1:
                    leftPoint.getDrawable().setLevel(0);
                    centerPoint.getDrawable().setLevel(1);
                    rightPoint.getDrawable().setLevel(0);
                    break;
                case 2:
                    leftPoint.getDrawable().setLevel(0);
                    centerPoint.getDrawable().setLevel(0);
                    rightPoint.getDrawable().setLevel(1);
                    break;

            }
        }
    });
    addView(view);
}
 
Example 18
Source File: TabPagerIndicator.java    From PLDroidShortVideo with Apache License 2.0 4 votes vote down vote up
/**
 * 必须先设置ViewPager
 * @param viewPager
 * @param currentPos
 */
public void setViewPager(ViewPager viewPager, int currentPos){
    mViewPager = viewPager;
    viewPager.addOnPageChangeListener(mListener);
    viewPager.setCurrentItem(currentPos);
}
 
Example 19
Source File: ViewPagerIdlingResource.java    From aws-device-farm-sample-app-for-android with Apache License 2.0 4 votes vote down vote up
public ViewPagerIdlingResource(ViewPager pager) {
    this.viewPager = pager;
    pager.addOnPageChangeListener(this);
    isIdle = true;
}
 
Example 20
Source File: BottomSheetUtils.java    From FabulousFilter with Apache License 2.0 4 votes vote down vote up
public static void setupViewPager(ViewPager viewPager) {
    final View bottomSheetParent = findBottomSheetParent(viewPager);
    if (bottomSheetParent != null) {
        viewPager.addOnPageChangeListener(new BottomSheetViewPagerListener(viewPager, bottomSheetParent));
    }
}