java - 如何更改 kotlin 中 viewpager 自动滑动的默认速度?

标签 java android kotlin android-viewpager

我使用下面的代码来更改viewpager中滑动的默认速度。 我尝试java代码,This Stackoverflow question's Help 。 我使用下面的 kotlin 代码来设置 View 寻呼机自动滑动动画的限制

val mScroller = ViewPager::class.java.getDeclaredField("mScroller");
mScroller.setAccessible(true);
val scroller = Scroller(requireContext(), DecelerateInterpolator());
mScroller.set(mPager, scroller);

但是代码不适用于 kotlin,并导致崩溃

05-08 19:21:59.690 23859-23859/com.mindfulness.greece A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 23859 (dfulness.greece)
05-08 19:21:59.820 7677-7677/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
05-08 19:21:59.820 7677-7677/? A/DEBUG: Build fingerprint: 'Android/sdk_google_phone_x86/generic_x86:6.0/MASTER/5056751:userdebug/test-keys'
05-08 19:21:59.820 7677-7677/? A/DEBUG: Revision: '0'
05-08 19:21:59.820 7677-7677/? A/DEBUG: ABI: 'x86'
05-08 19:21:59.821 7677-7677/? A/DEBUG: pid: 23859, tid: 23859, name: dfulness.greece  >>> com.mindfulness.greece <<<
05-08 19:21:59.821 7677-7677/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
05-08 19:21:59.830 7677-7677/? A/DEBUG:     eax 00000000  ebx b5ba0398  ecx 00000002  edx 00000000
05-08 19:21:59.831 7677-7677/? A/DEBUG:     esi 9b6bd6bc  edi 00000028
05-08 19:21:59.831 7677-7677/? A/DEBUG:     xcs 00000073  xds 0000007b  xes 0000007b  xfs 00000007  xss 0000007b
05-08 19:21:59.831 7677-7677/? A/DEBUG:     eip b5b111d0  ebp bf742000  esp bffa6030  flags 00210286
05-08 19:21:59.832 7677-7677/? A/DEBUG: backtrace:
05-08 19:21:59.832 7677-7677/? A/DEBUG:     #00 pc 0002a1d0  /system/lib/libhwui.so
05-08 19:21:59.832 7677-7677/? A/DEBUG:     #01 pc 0003e9eb  /system/lib/libhwui.so (android::uirenderer::DisplayListCanvas::drawCircle(float, float, float, SkPaint const&)+251)
05-08 19:21:59.832 7677-7677/? A/DEBUG:     #02 pc 000d581c  /system/lib/libandroid_runtime.so
05-08 19:21:59.833 7677-7677/? A/DEBUG:     #03 pc 737055f0  /data/dalvik-cache/x86/system@framework@boot.oat (offset 0x1eb2000)
05-08 19:21:59.833 7677-7677/? A/DEBUG:     #04 pc 00002ca3  /system/lib/libhwui.so (offset 0xb3000)
05-08 19:21:59.833 7677-7677/? A/DEBUG:     #05 pc 0003a193  /system/lib/libhwui.so (non-virtual thunk to android::uirenderer::DisplayListCanvas::~DisplayListCanvas()+9)
05-08 19:21:59.833 7677-7677/? A/DEBUG:     #06 pc 0003a20f  /system/lib/libhwui.so
05-08 19:21:59.833 7677-7677/? A/DEBUG:     #07 pc 027010eb  [heap]
05-08 19:21:59.980 7677-7677/? A/DEBUG: Tombstone written to: /data/tombstones/tombstone_05
05-08 19:21:59.981 7677-7677/? E/DEBUG: AM write failed: Broken pipe
05-08 19:22:00.100 1640-1696/system_process E/InputDispatcher: channel 'eb88da6 com.mindfulness.greece/com.mindfulness.greece.activity.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
05-08 19:22:00.191 1640-6583/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
05-08 19:22:00.192 1640-6583/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
05-08 19:22:00.192 1640-6583/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
05-08 19:22:00.259 1955-2211/com.android.launcher3 E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
05-08 19:22:00.259 1955-2211/com.android.launcher3 E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
05-08 19:22:00.260 1955-2211/com.android.launcher3 E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824

最佳答案

你可以看看,我想这会对你有帮助......

private fun init() {


    mPager = findViewById(R.id.pager) as ViewPager
    mPager!!.adapter = SlidingImage_Adapter(this@MainActivity, this.imageModelArrayList!!)

    val indicator = findViewById(R.id.indicator) as CirclePageIndicator

    indicator.setViewPager(mPager)

    val density = resources.displayMetrics.density

    //Set circle indicator radius
    indicator.setRadius(5 * density)

    NUM_PAGES = imageModelArrayList!!.size

    // Auto start of viewpager
    val handler = Handler()
    val Update = Runnable {
        if (currentPage == NUM_PAGES) {
            currentPage = 0
        }
        mPager!!.setCurrentItem(currentPage++, true)
    }
    val swipeTimer = Timer()
    swipeTimer.schedule(object : TimerTask() {
        override fun run() {
            handler.post(Update)
        }
    }, 3000, 3000)

    // Pager listener over indicator
    indicator.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {

        override fun onPageSelected(position: Int) {
            currentPage = position

        }

        override fun onPageScrolled(pos: Int, arg1: Float, arg2: Int) {

        }

        override fun onPageScrollStateChanged(pos: Int) {

        }
    })

}

companion object {

    private var mPager: ViewPager? = null
    private var currentPage = 0
    private var NUM_PAGES = 0
}

关于java - 如何更改 kotlin 中 viewpager 自动滑动的默认速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56042543/

相关文章:

java - URL中的方括号导致Tomcat异常

java - 在 GWT 中使用 UiBinder 将 Ui 绑定(bind)到 java 接口(interface)

java - 预验证类中的 Android 错误类引用解析为意外实现

java - 如果列表中尚不存在,则将项目添加到 arraylist

android - Android LiveData get() 语法如何工作?

javascript - 如果我不离开单元格/字段,Primefaces 将无法识别单元格/字段的值。

java - 为什么在 Hibernate 中使用 org.hibernate.TransactionException 并避免

java - 添加启动画面后自定义适配器的 getView 出现 NullPointerException

带有多个参数的 Kotlin setter

syntax - Kotlin中的函数式编程-分配函数