Kotlin / anko

Pleasant Android application development
Apache License 2.0
15.89k stars 1.29k forks source link

Use custom layout which extends AdapterView<BaseAdapter> #481

Closed iam1492 closed 7 years ago

iam1492 commented 7 years ago

I want to use my own custom layout which extends AdapterView

class SearchFilterLayout : AnkoComponent<SearchFilterActivity> {
    override fun createView(ui: AnkoContext<SearchFilterActivity>): View = with(ui) {
        verticalLayout {
            id = R.id.root
            textView(R.string.category) {
                id = R.id.category_label
                textSize = 15f
                textColor = ContextCompat.getColor(context, R.color.hoian_black)
            }.lparams(width = wrapContent, height = wrapContent) {
                bottomMargin = dip(8)
            }

            chessboardLayout {
                id = R.id.category_chessboard
                colCount = 2
            }.lparams(width = matchParent, height = wrapContent)
        }
    }
}
inline fun ViewManager.chessboardLayout(): ChessBoardLayout = chessboardLayout {}
inline fun ViewManager.chessboardLayout(init: ChessBoardLayout.() -> Unit) : ChessBoardLayout{
    return ankoView({ ctx: Context -> ChessBoardLayout(ctx) }, 0) {
        init()
    }
}

But when I run my app. I saw the following errors:

java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
                    at android.widget.AdapterView.addView(AdapterView.java:477)
                    at org.jetbrains.anko.internals.AnkoInternals.addView(Internals.kt:47)
                    at com.towneers.www.ui.adapter.SearchFilterCategoryAdapter.getView(SearchFilterCategoryAdapter.kt:88)
                    at com.jungkai.chessboardlayout.ChessBoardLayout.obtainView(ChessBoardLayout.java:210)
                    at com.jungkai.chessboardlayout.ChessBoardLayout.onMeasure(ChessBoardLayout.java:173)
                    at android.view.View.measure(View.java:21998)
                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
                    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
                    at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
                    at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
                    ...

How to solve this problem? I'm new to anko and not easy to solve this. You can see ChessBoardLayout from here: https://github.com/n42corp/chessboardlayout/blob/master/library/src/main/java/com/jungkai/chessboardlayout/ChessBoardLayout.java

iam1492 commented 7 years ago

I was made mistake that I use with(context) {} and there addview called. I just remove that and now everythink OK!