在Kotlin中组合两个setcontentview



对于DoaHome.kt,我想放两个setcontentview。第一个是导航抽屉的setContentView(R.layout.cover_home_doa(。另一个是可扩展列表的setContentView(binding.root(。

这是DoaHome.kt 的代码

package com.example.senangumrah
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.example.senangumrah.databinding.ActivityDoaHomeBinding
import kotlinx.android.synthetic.main.activity_persediaan_home.*
import kotlinx.android.synthetic.main.cover_home_amalan_selepas.*
import kotlinx.android.synthetic.main.cover_home_amalan_selepas.navigationView
import kotlinx.android.synthetic.main.cover_home_doa.*
import kotlinx.android.synthetic.main.cover_home_persediaan.*
import kotlinx.android.synthetic.main.layout_side_menu.*
class DoaHome : AppCompatActivity(), View.OnClickListener {
private lateinit var binding: ActivityDoaHomeBinding
private lateinit var listViewAdapter: ExpandableListViewAdapter
private lateinit var chapterList: List<String>
private lateinit var topicList: HashMap<String, List<String>>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityDoaHomeBinding.inflate(layoutInflater)
setContentView(R.layout.cover_home_doa)
setContentView(binding.root)
onSetNavigationDrawerEvents()
showList()
listViewAdapter = ExpandableListViewAdapter( context = this, chapterList, topicList)
binding.eListView.setAdapter(listViewAdapter)
val btnback : ImageView = findViewById(R.id.buttonback)
btnback.setOnClickListener {
finish()
}

}


private fun showList() {
chapterList = ArrayList()
topicList = HashMap()
(chapterList as ArrayList<String>).add("Sebelum Berlepas")
(chapterList as ArrayList<String>).add("Setelah Sampai")
(chapterList as ArrayList<String>).add("Sebelum Masuk Mekah")
(chapterList as ArrayList<String>).add("Ketika di Kaabah")
(chapterList as ArrayList<String>).add("Ketika Tawaf")
(chapterList as ArrayList<String>).add("Keluar dari Mekah")
(chapterList as ArrayList<String>).add("Sebelum Masuk Madinah")
(chapterList as ArrayList<String>).add("Ketika di Masjid Nabawi")
(chapterList as ArrayList<String>).add("Ketika di Raudhah")
(chapterList as ArrayList<String>).add("Keluar dari Madinah")
val topic1 : MutableList<String> = ArrayList()
topic1.add("Doa Naik Kenderaan")
topic1.add("Doa Keselamatan Diri dan Harta")
topic1.add("Doa Dipermudahkan Urusan")
val topic2 : MutableList<String> = ArrayList()
topic2.add("Topic 1")
topic2.add("Topic 2")
topic2.add("Topic 3")
val topic3 : MutableList<String> = ArrayList()
topic3.add("Topic 1")
topic3.add("Topic 2")
topic3.add("Topic 3")
val topic4 : MutableList<String> = ArrayList()
topic4.add("Topic 1")
topic4.add("Topic 2")
topic4.add("Topic 3")
val topic5 : MutableList<String> = ArrayList()
topic5.add("Topic 1")
topic5.add("Topic 2")
topic5.add("Topic 3")
val topic6 : MutableList<String> = ArrayList()
topic6.add("Topic 1")
topic6.add("Topic 2")
topic6.add("Topic 3")
val topic7 : MutableList<String> = ArrayList()
topic7.add("Topic 1")
topic7.add("Topic 2")
topic7.add("Topic 3")
val topic8 : MutableList<String> = ArrayList()
topic8.add("Topic 1")
topic8.add("Topic 2")
topic8.add("Topic 3")
val topic9 : MutableList<String> = ArrayList()
topic9.add("Topic 1")
topic9.add("Topic 2")
topic9.add("Topic 3")
val topic10 : MutableList<String> = ArrayList()
topic10.add("Topic 1")
topic10.add("Topic 2")
topic10.add("Topic 3")
topicList[chapterList[0]] = topic1
topicList[chapterList[1]] = topic2
topicList[chapterList[2]] = topic3
topicList[chapterList[3]] = topic4
topicList[chapterList[4]] = topic5
topicList[chapterList[5]] = topic6
topicList[chapterList[6]] = topic7
topicList[chapterList[7]] = topic8
topicList[chapterList[8]] = topic9
topicList[chapterList[9]] = topic10
}
private fun onSetNavigationDrawerEvents() {
navigation_button.setOnClickListener(this)
ll_First.setOnClickListener(this)
ll_Second.setOnClickListener(this)
ll_Third.setOnClickListener(this)
ll_Fourth.setOnClickListener(this)
ll_Fifth.setOnClickListener(this)
ll_Sixth.setOnClickListener(this)
close.setOnClickListener(this)
}
override fun onClick(v: View) {
when (v.id) {
R.id.navigation_button -> {
doaDrawerLayout.openDrawer(navigationView, true)
}
R.id.ll_First -> {
val button = findViewById<TextView>(R.id.menu_persediaan)
button.setOnClickListener{
val intent = Intent(this, PersediaanHome::class.java)
startActivity(intent)
doaDrawerLayout.closeDrawer(navigationView, true)
}
}
R.id.ll_Second -> {
val button = findViewById<TextView>(R.id.menu_kursus)
button.setOnClickListener{
val intent = Intent(this, KursusHome::class.java)
startActivity(intent)
doaDrawerLayout.closeDrawer(navigationView, true)
}
}
R.id.ll_Third -> {
val button = findViewById<TextView>(R.id.menu_amalansemasa)
button.setOnClickListener{
val intent = Intent(this, AmalanSemasaHome::class.java)
startActivity(intent)
doaDrawerLayout.closeDrawer(navigationView, true)
}
}
R.id.ll_Fourth -> {
val button = findViewById<TextView>(R.id.menu_doa)
button.setOnClickListener{
val intent = Intent(this, TempatZiarahHome::class.java)
startActivity(intent)
doaDrawerLayout.closeDrawer(navigationView, true)
}
}
R.id.ll_Fifth -> {
val button = findViewById<TextView>(R.id.menu_tempat)
button.setOnClickListener{
val intent = Intent(this,  TempatZiarahHome::class.java)
startActivity(intent)
doaDrawerLayout.closeDrawer(navigationView, true)
}
}
R.id.ll_Sixth -> {
val button = findViewById<TextView>(R.id.menu_amalanselepas)
button.setOnClickListener{
val intent = Intent(this, AmalanSelepasHome::class.java)
startActivity(intent)
doaDrawerLayout.closeDrawer(navigationView, true)
}
}
R.id.close -> {
showToast("Amalan Selepas Umrah Home")
doaDrawerLayout.closeDrawer(navigationView, true)
}
else -> {
showToast("Amalan Selepas Umrah Home")
doaDrawerLayout.closeDrawer(navigationView, true)
}
}
}
private fun showToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
override fun onBackPressed() {
if ( doaDrawerLayout.isDrawerOpen(navigationView)) {
doaDrawerLayout.closeDrawer(navigationView, true)
} else {
super.onBackPressed()
}
}
override fun onDestroy() {
super.onDestroy()
}
}

当我把两个setcontentview。。。我的界面是空白的。。内容不会出现,导航抽屉也不起作用。。然后在那之后应用程序突然停止响应。

setContentView设置"活动"的唯一根视图,因此只调用一次是有意义的。如果要为抽屉和其他内容使用单独的布局文件,可以创建一个顶层布局文件来定义整个布局,也可以使用<include/>元素将其他布局文件包括在此顶层布局中。

最新更新