在片段中使用Anko布局

时间:2018-04-17 14:04:23

标签: android kotlin anko

我创建了以下布局:

class ExploreUI<T> : AnkoComponent<T> {

    override fun createView(ui: AnkoContext<T>): View {

        return with(ui) {
            frameLayout {
                val loadingIndicator = include<View>(R.layout.loading_indicator)

                val content = nestedScrollView {
                    isFillViewport = true
                    visibility = View.GONE

                    verticalLayout {

                        /** New Releases */
                        themedTextView(R.string.new_releases, theme = R.style.DarkThemeHeaderText) {
                            id = R.id.releasesHeader
                        }.lparams {
                            padding = dip(10)
                        }.apply {

                        }

                        val releasesRecycler = recyclerView {
                        }.lparams(width = matchParent, height = wrapContent)

                        include<View>(R.layout.show_more)

                        /** Recommended playlists */
                        themedTextView(R.string.recommended_playlists, theme = R.style.DarkThemeHeaderText) {
                            id = R.id.releasesHeader
                        }.lparams {
                            padding = dip(10)
                        }.apply {

                        }

                        val playlistsRecycler = recyclerView {
                        }.lparams(width = matchParent, height = wrapContent)

                        include<View>(R.layout.show_more)

                        /** Recently played */
                        themedTextView(R.string.recently_played, theme = R.style.DarkThemeHeaderText) {
                            id = R.id.releasesHeader
                        }.lparams {
                            padding = dip(10)
                        }.apply {

                        }

                        val recentsRecycler = recyclerView {
                        }.lparams(width = matchParent, height = wrapContent)

                        include<View>(R.layout.show_more)

                        /** Popular artists */
                        themedTextView(R.string.popular_artists, theme = R.style.DarkThemeHeaderText) {
                            id = R.id.releasesHeader
                        }.lparams {
                            padding = dip(10)
                        }.apply {

                        }

                        val artistsRecycler = recyclerView {
                        }.lparams(width = matchParent, height = wrapContent)

                        include<View>(R.layout.show_more)

                        /** Recommended tracks */
                        themedTextView(R.string.most_recommeneded, theme = R.style.DarkThemeHeaderText) {
                            id = R.id.releasesHeader
                        }.lparams {
                            padding = dip(10)
                        }.apply {

                        }

                        val recommendedTracksRecycler = recyclerView {
                        }.lparams(width = matchParent, height = wrapContent)

                        include<View>(R.layout.show_more)

                        /** All time hits */
                        themedTextView(R.string.based_on_your_listening, theme = R.style.DarkThemeHeaderText) {
                            id = R.id.releasesHeader
                        }.lparams {
                            padding = dip(10)
                        }.apply {

                        }

                        val hitsRecycler = recyclerView {
                        }.lparams(width = matchParent, height = wrapContent)

                        include<View>(R.layout.show_more)
                    }
                }
            }
        }
    }

}

然后我按照以下内容在onCreateView上夸大布局:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
            ExploreUI().createView(AnkoContext.Companion.create(activity as AppCompatActivity, this))

我遇到的问题是访问我的UI类中定义的组件。我如何从我的片段中引用它们?

1 个答案:

答案 0 :(得分:0)

问题的直接答案是将您感兴趣的值作为属性公开。

但是,我建议您使用ViewModel,以便您的片段不会直接与您的视图对话。片段的作用是将ViewModel绑定到视图。

相关问题