Kotlin

[Android] ViewPager.OnPageChangeListener() deprecated 에 따른 대체 메소드 addOnPageChangeListener() 사용하기

VierPager 클래스의 OnPageChangeListener()메소드가 deprecated되었다. 그리고 addOnPageChangeListener()메소드가 추가되었다. 사용법은 동일하다. 메소드명만 변경되었다고 해도 될 정도로……..

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	ViewPager viewPager = findViewById(R.id.viewpager);
}

 

[AS-IS]

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
		getSupportActionBar().setTitle(fragmentViewPagerAdapter.getPageTitle(position));
	}

	@Override
	public void onPageSelected(int position) {
		getSupportActionBar().setTitle(fragmentViewPagerAdapter.getPageTitle(position));
	}

	@Override
	public void onPageScrollStateChanged(int state) {

	}
});

 

[TO-BE]

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

	}

	@Override
	public void onPageSelected(int position) {
		getSupportActionBar().setTitle(fragmentViewPagerAdapter.getPageTitle(position));
	}

	@Override
	public void onPageScrollStateChanged(int state) {

	}
});

 

[참고]

stackoverflow.com/questions/30867852/viewpager-setonpagechangelistener-deprecated

 

[연관]

ViewPager.OnPageChangeListener

addOnPageChangeListener

 

 

Leave a Reply

error: Content is protected !!