scrollTo()在Kotlin Android应用程序中不起作用

时间:2018-06-24 15:17:21

标签: kotlin android-scrollview

我有一个Kotlin方法,该方法可以排列垂直的彩色点带:

    private fun setupStripOfPegsForColumn(col: Int, scrollToMemoryPeg: MemoryPeg?) {

    // Remove all from strip then layout strip according to model

    //Add the strip of MemoryPegs to the scrollview:
    val colOfPegs = memoryModel.getColumnOfMemoryPegsNotCrossedInUI(col = col)

    // Remove the memPegs.....
    val provisionalScrollview = memPegStripScrollViews[col-1]
    provisionalScrollview!!.removeAllViews()

    val holder = LinearLayout(hoose.context)
    holder.orientation = LinearLayout.VERTICAL

    var index = 0

    for (memoryPeg in colOfPegs) {

        val imageButton = ImageButton(hoose.context)
        imageButton.scaleType = ImageView.ScaleType.FIT_XY
        imageButton.padding = 0
        imageButton.imageResource = hoose.context.resources.getIdentifier(PegColour.pegName(memoryPeg.pegColour.ordinal), "drawable", hoose.context.packageName)
        imageButton.backgroundResource = 0
        imageButton.layoutParams = FrameLayout.LayoutParams((gameDimensions.pegSize - gameDimensions.pegPadding).toInt(), (gameDimensions.pegSize - gameDimensions.pegPadding).toInt())
        imageButton.padding = 0
        imageButton.top = gameDimensions.pegSize * index
        index += 1

        holder.addView(imageButton)
    }

    provisionalScrollview.addView(holder)

    if (scrollToMemoryPeg != null) {
        val countRuledOut = memoryModel.getColumnOfMemoryPegsCrossedInUILessThanRow(col = col, row = scrollToMemoryPeg.row).count()
        provisionalScrollview.scrollTo(0, (scrollToMemoryPeg.row-1-countRuledOut)*gameDimensions.pegSize)

        // scrollView.scrollTo(0,300) -> can control another scrollview here like this

        // provisionalScrollview.removeAllViews() -> works here like this
    }
}

一切都很好,除了我无法让provisionalScrollview.scrollTo做任何事情。

scrollToMemoryPeg != null时,scrollview应该滚动,但是不起作用。

如前所述,我可以滚动另一个scrollview(命名为scrollView)或从provisionalScrollview删除子视图,但是scrollTo()在{{1}上不起作用}。有什么想法吗?是与布局有关还是存在中间视图provisionalScrollview

0 个答案:

没有答案