今天我要设置ViewPager。setOnPageChangeListener,我发现它已弃用。
当前回答
你可以通过SimpleOnPageChangeListener来实现 守则如下:
private Fragment page = null;
/** Defining a listener for pageChange */
ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
page = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager_selectPhotos + ":" + position);
System.out.println(page);
switch (position) {
case 0:
break;
case 1:
break;
default:
break;
}
}
};
/** Setting the pageChange listener to the viewPager */
_pager.setOnPageChangeListener(pageChangeListener);
其他回答
这招对我很管用:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
conterchange.setText(""+(1+position));
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
: viewpage。setOnPageChangeListener现在已弃用。需要使用ViewPager。addOnPageChangeListener代替。
例如,
ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
你可以在这里找到这个。
以防有人需要kotlin中的解决方案:
viewPager.addOnPageChangeListener (object: ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onPageSelected(position: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
})
你可以通过SimpleOnPageChangeListener来实现 守则如下:
private Fragment page = null;
/** Defining a listener for pageChange */
ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
page = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager_selectPhotos + ":" + position);
System.out.println(page);
switch (position) {
case 0:
break;
case 1:
break;
default:
break;
}
}
};
/** Setting the pageChange listener to the viewPager */
_pager.setOnPageChangeListener(pageChangeListener);
这是新的方法,导航栏与viewpager它为我工作。
binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
switch (position) {
case 0:
binding.bottomNavigation.getMenu().findItem(R.id.nav_item_Home).setChecked(true);
break;
case 1:
binding.bottomNavigation.getMenu().findItem(R.id.nav_item_Booking).setChecked(true);
break;
case 2:
binding.bottomNavigation.getMenu().findItem(R.id.nav_item_Transaction).setChecked(true);
break;
case 3:
binding.bottomNavigation.getMenu().findItem(R.id.nav_item_HealthyFood).setChecked(true);
break;
}
}
});
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout
- Activity和FragmentActivity的区别
- 右对齐文本在android TextView
- 权限拒绝:start前台需要android.permission.FOREGROUND_SERVICE
- 如何更改android操作栏的标题和图标
- Android Split字符串
- 让一个链接在安卓浏览器启动我的应用程序?
- 如何在Android工作室的外部库中添加一个jar ?
- GridLayout(不是GridView)如何均匀地拉伸所有子元素
- 如何让一个片段删除自己,即它的等效完成()?