英文:
How to open a new fragment in bottom navigation view
问题
我正在开发一款健身安卓应用程序,其中我正在使用底部导航视图,有三个片段,分别是:主页片段(fragment:home),发现片段(fragment:Discover)和任务片段(fragment:task),以及个人资料。在主页片段中,我有一些按钮,我想在新的片段中打开它们,但我不知道如何做!请帮助我解决这个问题。
请帮助我解决这个问题,一旦项目完成,我将在GitHub上上传它。
英文:
I am developing fitness android app in which i am using bottom navigation view there are three fragment like fragment:home , fragment:Discover , fragment:task, and profile in home fragment i has some button in home fragment i want to open it in new fragment but i doesn't know how to do that! please help me to solve this issue enter image description here
Please help me to solve this issue once i will completed the project i will upload it on github
答案1
得分: -1
> 通过使用 FrameLayout 和 底部导航栏 来实现这一点。
>
> 为每个菜单项创建一个 Fragment。
//fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Home Fragment" />
</FrameLayout>
//fragment_discover.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DiscoverFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Discover Fragment" />
</FrameLayout>
//fragment_task.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TaskFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Task Fragment" />
</FrameLayout>
> 主要活动(Main Activity)
package com.example.harsh
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
lateinit var bottomNav : BottomNavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loadFragment(HomeFragment())
bottomNav = findViewById(R.id.bottomNav) as BottomNavigationView
bottomNav.setOnItemSelectedListener {
when (it.itemId) {
R.id.home -> {
loadFragment(HomeFragment())
true
}
R.id.discover -> {
loadFragment(DiscoverFragment())
true
}
R.id.task -> {
loadFragment(TaskFragment())
true
}
}
}
}
private fun loadFragment(fragment: Fragment){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.container,fragment)
transaction.commit()
}
}
> activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomNav"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNav"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/nav_menu"
android:scrollIndicators="left"/>
</RelativeLayout>
> 导航菜单文件 nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:icon="@drawable/ic_home"
android:title="Home" />
<item
android:id="@+id/discover"
android:icon="@drawable/ic_discover"
android:title="Discover" />
<item
android:id="@+id/task"
android:icon="@drawable/ic_task"
android:title="Task" />
</menu>
英文:
> One way of doing this by FrameLayout and Bottom Navigation Bar.
>
>
> Create Fragment for each menu item.
//fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Home Fragment" />
</FrameLayout>
//fragment_discover.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DiscoverFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Discover Fragment" />
</FrameLayout>
//fragment_task.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TaskFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Task Fragment" />
</FrameLayout>
> Main Activity
package com.example.harsh
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
lateinit var bottomNav : BottomNavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loadFragment(HomeFragment())
bottomNav = findViewById(R.id.bottomNav) as BottomNavigationView
bottomNav.setOnItemSelectedListener {
when (it.itemId) {
R.id.home -> {
loadFragment(HomeFragment())
true
}
R.id.discover -> {
loadFragment(DiscoverFragment())
true
}
R.id.task -> {
loadFragment(TaskFragment())
true
}
}
}
}
private fun loadFragment(fragment: Fragment){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.container,fragment)
transaction.commit()
}
}
> activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomNav"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNav"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/nav_menu"
android:scrollIndicators="left"/>
</RelativeLayout>
> Nav Menu File nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/home"
android:icon="@drawable/ic_home"
android:title="Home" />
<item
android:id="@+id/discover"
android:icon="@drawable/ic_discover"
android:title="Discover" />
<item
android:id="@+id/task"
android:icon="@drawable/ic_task"
android:title="Task" />
</menu>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论