用android中的viewpager适配器有没有办法减慢滚动速度?
你知道,我一直在看这段代码.我无法弄清楚我错了什么.
try{
Field mScroller = mPager.getClass().getDeclaredField("mScroller");
mScroller.setAccessible(true);
Scroller scroll = new Scroller(cxt);
Field scrollDuration = scroll.getClass().getDeclaredField("mDuration");
scrollDuration.setAccessible(true);
scrollDuration.set(scroll, 1000);
mScroller.set(mPager, scroll);
}catch (Exception e){
Toast.makeText(cxt, "something happened", Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)
它没有改变什么,但没有例外发生?
我在 ViewPager2 中有一个自定义翻转转换,我禁用了用户输入,因此它只能以编程方式更改页面,但是,转换的速度太快了。
我需要的行为是 2 个片段,第一个片段加载,几毫秒后我以编程方式触发转换,我看到动画和第二个片段,就是这样。
这是寻呼机相关代码:
viewPager.apply {
adapter = ViewPagerAdapter(this@HostFragment)
setPageTransformer(VerticalFlipTransformation())
isUserInputEnabled = false
}
Run Code Online (Sandbox Code Playgroud)
在某个时刻,我会像这样触发转换:
viewPager.currentItem = 1
Run Code Online (Sandbox Code Playgroud)
这是我的适配器:
private class ViewPagerAdapter(fragment: Fragment) :
FragmentStateAdapter(fragment) {
override fun getItemCount() = 2
override fun createFragment(position: Int) = if (position == 0) {
Fragment1()
} else {
Fragment2()
}
}
Run Code Online (Sandbox Code Playgroud)
最后这是我正在使用的转换:
class VerticalFlipTransformation : ViewPager2.PageTransformer {
override fun transformPage(page: View, position: Float) {
page.translationX = -position * page.width
page.cameraDistance = 20000f
if (position < 0.5 && position > -0.5) …Run Code Online (Sandbox Code Playgroud)