Android 在片段上调用底部导航kotlin
下面的kotlin代码允许您修改显示在底部导航中的片段,显示主片段,但当我单击转到第二个和第三个片段时,该片段未显示,这是由于什么原因?片段是3个,一个是主片段,然后是其他2个片段,但这些片段并没有显示,在单击的那个一刻,startfragment被调用,但它显示了一个空视图 MainActivity.ktAndroid 在片段上调用底部导航kotlin,android,kotlin,android-fragments,Android,Kotlin,Android Fragments,下面的kotlin代码允许您修改显示在底部导航中的片段,显示主片段,但当我单击转到第二个和第三个片段时,该片段未显示,这是由于什么原因?片段是3个,一个是主片段,然后是其他2个片段,但这些片段并没有显示,在单击的那个一刻,startfragment被调用,但它显示了一个空视图 MainActivity.kt class MainActivity : AppCompatActivity() { private val bottleFragment = BottleFragment()
class MainActivity : AppCompatActivity() {
private val bottleFragment = BottleFragment()
private val homeFragment = HomeFragment()
private val profileInfoFragment = ProfileInfoFragment()
private val fragmentHistory = mutableListOf<Fragment>(homeFragment)
private var activeFragment: Fragment = homeFragment
var ID_AUTH_FIREBASE = ""
var ADMIN_KEY = ""
var email = ""
private val bottomNavigationListener = BottomNavigationView.OnNavigationItemSelectedListener {
when (it.itemId) {
R.id.navigation_home -> {
startFragment(homeFragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_profile -> {
System.out.println("Sono qui nel profilo")
startFragment(bottleFragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_bottle -> {
startFragment(profileInfoFragment)
return@OnNavigationItemSelectedListener true
}
else -> {
return@OnNavigationItemSelectedListener false
}
}
}
@SuppressLint("MissingPermission")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigation.setOnNavigationItemSelectedListener(bottomNavigationListener)
startFragment(homeFragment)
checkPermissions()
buttonlogout.setOnClickListener { logout() }
buttonsave.setOnClickListener { save() }
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
);
email = intent.getStringExtra("Email")
ID_AUTH_FIREBASE = intent.getStringExtra("ID_AUTH_FIREBASE")
ADMIN_KEY = intent.getStringExtra("ADMIN_KEY")
val datiPersona = APISupport.getDatiPersona(ID_AUTH_FIREBASE, email, ADMIN_KEY)
textNome.setText(datiPersona[0])
txtCognome.setText(datiPersona[1])
txtResidenza.setText(datiPersona[2])
txtTelefono.setText(datiPersona[3])
txtEmail.setText(email)
hometext.text = datiPersona[0] + " " + datiPersona[1]
txtcfdesc.setText(datiPersona[5])
txtnumeropolizaedit.setText(datiPersona[6])
val url: URL = URL(APISupport.geturlLogo(ID_AUTH_FIREBASE, email, ADMIN_KEY))
val bmp: Bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream())
imageLogo.setImageBitmap(bmp)
txtDataFineEdit.setText(datiPersona[8])
txtDataInizioEdit.setText(datiPersona[7])
}
override fun onResume() {
super.onResume()
checkPermissions()
}
override fun onPause() {
super.onPause()
checkPermissions()
}
fun save() {
var value = APISupport.savedatipersona(
ID_AUTH_FIREBASE,
email,
ADMIN_KEY,
txtCognome.text.toString(),
textNome.text.toString(),
txtTelefono.text.toString(),
txtResidenza.text.toString(),
txtDataInizioEdit.text.toString(),
txtDataFineEdit.text.toString(),
txtnumeropolizaedit.text.toString(),
txtcfdesc.text.toString()
)
}
fun logout() {
val intent = Intent(this, SplashActivity::class.java)
startActivity(intent)
}
fun startFragment(fragment: Fragment, removeLast: Boolean = false) {
if (fragment == homeFragment)
startHomeBackground()
else
stopHomeBackground()
if (removeLast)
fragmentHistory.removeAt(fragmentHistory.lastIndex)
if (fragmentHistory.last() != fragment)
fragmentHistory.add(fragment)
activeFragment = fragment
}
class MainActivity:AppCompatActivity(){
私有val-blottlefragment=blottlefragment()
private val homeFragment=homeFragment()
private val profileInfoFragment=profileInfoFragment()
private val fragmentHistory=mutableListOf(homeFragment)
私有变量activeFragment:Fragment=homeFragment
var ID_AUTH_FIREBASE=“”
var ADMIN_KEY=“”
var email=“”
private val bottomNavigationListener=BottomNavigationView.OnNavigationItemSelectedListener{
何时(it.itemId){
R.id.navigation\u主页->{
startFragment(homeFragment)
return@OnNavigationItemSelectedListener真的
}
R.id.navigation\u配置文件->{
System.out.println(“Sono qui nel profilo”)
startFragment(瓶装碎片)
return@OnNavigationItemSelectedListener真的
}
R.id.navigation_瓶->{
startFragment(profileInfoFragment)
return@OnNavigationItemSelectedListener真的
}
其他->{
return@OnNavigationItemSelectedListener假的
}
}
}
@SuppressLint(“丢失许可”)
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navigation.setOnNavigationItemSelectedListener(底部导航Listener)
startFragment(homeFragment)
检查权限()
buttonlogout.setOnClickListener{logout()}
buttonsave.setOnClickListener{save()}
window.setFlags(
WindowManager.LayoutParams.FLAG_全屏显示,
WindowManager.LayoutParams.FLAG_全屏显示
);
email=intent.getStringExtra(“电子邮件”)
ID\u AUTH\u FIREBASE=intent.getStringExtra(“ID\u AUTH\u FIREBASE”)
ADMIN\u KEY=intent.getStringExtra(“ADMIN\u KEY”)
val datiPersona=APISupport.getDatiPersona(ID\u AUTH\u FIREBASE,email,ADMIN\u KEY)
textNome.setText(datiPersona[0])
txtCognome.setText(datiPersona[1])
txtResidenza.setText(datiPersona[2])
txtTelefono.setText(datiPersona[3])
txtEmail.setText(电子邮件)
hometext.text=datiPersona[0]+“”+datiPersona[1]
txtcfdesc.setText(datiPersona[5])
txtnumeropolizaedit.setText(datiPersona[6])
val url:url=url(APISupport.geturlogo(ID\u AUTH\u FIREBASE,email,ADMIN\u KEY))
val bmp:Bitmap=BitmapFactory.decodeStream(url.openConnection().getInputStream())
设置图像位图(bmp)
txtDataFineEdit.setText(datiPersona[8])
txtdatanizationioedit.setText(datiPersona[7])
}
重写onResume(){
super.onResume()
检查权限()
}
覆盖暂停(){
super.onPause()
检查权限()
}
趣味储蓄{
var value=APISupport.savedatipersona(
ID_AUTH_FIREBASE,
电子邮件,
管理员密钥,
txtCognome.text.toString(),
textNome.text.toString(),
txtTelefono.text.toString(),
txtResidenza.text.toString(),
txtdatanizationioedit.text.toString(),
txtdatafinedit.text.toString(),
txtnumeropolizaedit.text.toString(),
txtcfdesc.text.toString()
)
}
注销乐趣(){
val intent=intent(这是SplashActivity::class.java)
星触觉(意图)
}
fun startFragment(片段:片段,removeLast:Boolean=false){
如果(片段==homeFragment)
startHomeBackground()
其他的
stopHomeBackground()
if(removeLast)
fragmentHistory.removeAt(fragmentHistory.lastIndex)
if(fragmentHistory.last()!=fragment)
碎片历史。添加(碎片)
activeFragment=片段
}
bottom_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/profile_nav_selector"
android:tooltipText="Profilo"
android:title="Profilo" />
<item
android:id="@+id/navigation_profile"
android:icon="@drawable/home_nav_selector"
android:tooltipText="Veicoli"
android:title="Veicoli" />
<item
android:id="@+id/navigation_bottle"
android:icon="@drawable/bottle_nav_selector"
android:tooltipText="Info"
android:title="Info" />
</menu>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="85dp"
android:background="#FFFAFA"
android:backgroundTint="@android:color/transparent"
app:itemBackground="@android:color/transparent"
app:itemIconSize="28dp"
app:itemIconTint="@color/bottom_navigation_color"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_navigation" />
mainacivity.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/profile_nav_selector"
android:tooltipText="Profilo"
android:title="Profilo" />
<item
android:id="@+id/navigation_profile"
android:icon="@drawable/home_nav_selector"
android:tooltipText="Veicoli"
android:title="Veicoli" />
<item
android:id="@+id/navigation_bottle"
android:icon="@drawable/bottle_nav_selector"
android:tooltipText="Info"
android:title="Info" />
</menu>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="85dp"
android:background="#FFFAFA"
android:backgroundTint="@android:color/transparent"
app:itemBackground="@android:color/transparent"
app:itemIconSize="28dp"
app:itemIconTint="@color/bottom_navigation_color"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_navigation" />