gpt4 book ai didi

android - 在 fragment 中使用 Anko 布局

转载 作者:行者123 更新时间:2023-12-02 13:00:20 25 4
gpt4 key购买 nike

我创建了以下布局:

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 中引用它们?

最佳答案

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

但是,我建议使用 ViewModel所以你的 fragment 不会直接与你的观点对话。 Fragments 的作用是将 ViewModel 绑定(bind)到 View 。

关于android - 在 fragment 中使用 Anko 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49880262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com