今天我要设置ViewPager。setOnPageChangeListener,我发现它已弃用。
当前回答
这招对我很管用:
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。addOnPageChangeListener而不是viewpage . 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);
如果需要,创建一个接口; FragmentLifecycle { onPauseFragment(); onsumefragment (); } 如所示,在viewpager中的每个片段中实现接口
公共类FragmentCast扩展Fragment实现FragmentLifecycle { @Override onPauseFragment() { 日志。我(标签,“onPauseFragment () "); Toast.makeText(getActivity(), "onPauseFragment():" + TAG, Toast.LENGTH_SHORT).show(); } @Override onResumeFragment() { 日志。我(标签,“onResumeFragment () "); Toast.makeText(getActivity(), "onResumeFragment():" + TAG, Toast.LENGTH_SHORT).show(); }
List this is how u user the addOnPageListener enter viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { int currentPosition = 0; @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { FragmentLifecycle fragmentLifecycleToShow =(FragmentLifecycle) viewPager.getAdapter().instantiateItem(viewPager, viewPager.getCurrentItem()); fragmentLifecycleToShow.onResumeFragment(); FragmentLifecycle fragmentLifecycleToHide =(FragmentLifecycle) viewPager.getAdapter().instantiateItem(viewPager, currentPosition); fragmentLifecycleToHide.onPauseFragment(); currentPosition = position; } @Override public void onPageScrollStateChanged(int state) { } }); Implement your calls in the onPauseFragment() or onResumeFragment()
这招对我很管用:
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) {
}
});
以防有人需要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.
}
})
推荐文章
- 如何改变菜单项的文本颜色在安卓?
- Android选择器和文本颜色
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态
- Openssl不被视为内部或外部命令
- 无法执行dex:在Eclipse中超过GC开销限制
- 如何以编程方式将视图添加到视图
- 单击url会打开默认浏览器
- 使用Retrofit刷新OAuth令牌,而不修改所有调用
- 多个dex文件定义了landoid /support/v4/accessibilityservice/AccessibilityServiceInfoCompat
- 如何获得动作栏的高度?
- 从活动外部调用startActivity() ?