今天我要设置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) {
}
});
其他回答
如果需要,创建一个接口; 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()
以防有人需要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.
}
})
这招对我很管用:
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);
推荐文章
- Manifest合并失败:uses-sdk:minSdkVersion 14
- 为什么Android工作室说“等待调试器”如果我不调试?
- 如何检查我的EditText字段是否为空?
- Android从图库中选择图像
- 后台任务,进度对话框,方向改变-有任何100%工作的解决方案吗?
- Android:垂直对齐多行EditText(文本区域)
- Android无尽列表
- Android room persistent: AppDatabase_Impl不存在
- 错误:执行失败的任务':app:compileDebugKotlin'。>编译错误。详细信息请参见日志
- 在Android中使用URI生成器或使用变量创建URL
- 缩放图像以填充ImageView宽度并保持纵横比
- 列表视图的自定义适配器
- 在Android中设置TextView span的颜色
- 如何以编程方式在RelativeLayout中布局视图?
- Android Facebook集成无效键散列