【发布时间】: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 中引用它们?
【问题讨论】: