【问题标题】:Using Anko Layouts in Fragments在片段中使用 Anko 布局
【发布时间】:2018-04-17 14:04:22
【问题描述】:

我创建了以下布局:

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 类中定义的组件。如何从我的 Fragment 中引用它们?

【问题讨论】:

    标签: android kotlin anko


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 2015-07-10
      • 1970-01-01
      相关资源
      最近更新 更多