Android 在BottomNavigationView中的片段之间切换
我正在使用一个带有底部导航视图的简单应用程序。我有3个片段(布局和java)。我在MainActivity.java中声明了BottonAvigationView。我的bottonnavigation有3个项目,用于3个碎片。因此,在MainActivity.java中,当我选择一个项目时,它会启动一个片段。因此,当我再次选择另一个项目时,什么也没有发生,因为在java片段中,我需要声明BottonAvigationView,但我不知道如何将它设置为将实际片段与另一个片段切换。 我尝试了此链接,但没有成功: 对不起,我的英语不好 代码如下: 主要活动Android 在BottomNavigationView中的片段之间切换,android,android-fragments,bottomnavigationview,Android,Android Fragments,Bottomnavigationview,我正在使用一个带有底部导航视图的简单应用程序。我有3个片段(布局和java)。我在MainActivity.java中声明了BottonAvigationView。我的bottonnavigation有3个项目,用于3个碎片。因此,在MainActivity.java中,当我选择一个项目时,它会启动一个片段。因此,当我再次选择另一个项目时,什么也没有发生,因为在java片段中,我需要声明BottonAvigationView,但我不知道如何将它设置为将实际片段与另一个片段切换。 我尝试了此链接,
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
selectedFragment = HomeFragment.newInstance();
break;
case R.id.navigation_dashboard:
selectedFragment = DashboardFragment.newInstance();
break;
case R.id.navigation_notifications:
selectedFragment = NotificationsFragment.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content, selectedFragment);
transaction.commit();
return true;
}
片段Java示例
public class HomeFragment extends Fragment {
public static HomeFragment newInstance() {
HomeFragment fragment = new HomeFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.navigation_home, container, false);
return inflater.inflate(R.layout.navigation_home, container, false);
}
你可以试试:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
selectedFragment = HomeFragment.newInstance();
break;
case R.id.navigation_dashboard:
selectedFragment = DashboardFragment.newInstance();
break;
case R.id.navigation_notifications:
selectedFragment = NotificationsFragment.newInstance();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit();
return true;
}
在这种情况下,最好尝试从
fragmentManager
像fragmentManager.findffragmentbytag(tag)
那样获取存在片段。您可以更顺畅地切换,例如,您不需要从网络加载某些内容(如果您在片段或片段的呈现者中有这样的代码)您不必每次都创建新实例。您可以保存片段状态。请点击下面的链接
fragmentManager.beginTransaction().hide(toBeHidden).show(toBeShown).commit();
您应该从每个片段创建一次新距离。稍后,您可以隐藏活动片段,然后显示新片段
Fragment activeFragment;
ArrayList<Fragment> fragment;
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_fragment:
selectedFragment = ShowMyFragment.newInstance();
replaceFragment(selectedFragment);
return true;
}
});
private void replaceFragment(Fragment selectedFragment) {
boolean lastOpened = false;
for ( int i=0; i<fragment.size();i++ )
{
if ( fragment.get(i) == selectedFragment ) {
lastOpened = true;
break;
}
}
if (!lastOpened) {
getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit();
}
else
{
getSupportFragmentManager().beginTransaction().hide(activeFragment).show(selectedFragment).commit();
}
activeFragment = selectedFragment;
}
Fragment-activeFragment;
ArrayList片段;
公共布尔值onNavigationItemSelected(@NonNull MenuItem item){
片段selectedFragment=null;
开关(item.getItemId()){
案例R.id.U片段:
selectedFragment=ShowMyFragment.newInstance();
替换片段(selectedFragment);
返回true;
}
});
私有void replaceFragment(片段selectedFragment){
布尔值=假;
对于(inti=0;i,只需在onCreate中添加这样的空白实现
bottomNavView.setOnNavigationItemReselectedListener{}我的完整解决方案,我认为对青少年有用:
因此,我们的主要活动是:
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class MainMenuActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomNav: BottomNavigationView = findViewById(R.id.bottom_naviagtion)
bottomNav.setOnNavigationItemSelectedListener(navListener)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction().replace(
R.id.fragment_container,
HomeFragment()
).commit()
}
}
private val navListener: BottomNavigationView.OnNavigationItemSelectedListener =
BottomNavigationView.OnNavigationItemSelectedListener { item ->
var selectedFragment: Fragment? = null
when (item.itemId) {
R.id.bottom_home -> selectedFragment =
HomeFragment()
R.id.bottom_events -> selectedFragment =
EventFragment()
R.id.bottom_contacts -> selectedFragment =
ContactsFragment()
R.id.bottom_menu -> selectedFragment =
MenuFragment()
}
supportFragmentManager.beginTransaction().replace(
R.id.fragment_container,
selectedFragment!!
).commit()
true
}
}
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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_naviagtion"/>
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_above="@id/bottom_naviagtion"
android:background="@drawable/shadow"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_naviagtion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_menu"
android:background="?android:attr/windowBackground" />
</RelativeLayout>
HomeFragment(fragment_home.xml)的.xml文件看起来像(其他片段看起来相同):
NavigationBottomMenu.xml外观(bottom_menu.xml):
从drawble文件夹中导入了使用过的图标,如Vector Asset为什么总是创建新实例?在我的主要活动布局中,我有一个带有FrameLayout和BottomNavigationView的线性布局,我想我需要创建一个新实例,不是吗?你可以创建一次片段,然后再使用它们。你能给我看一段代码吗?以及我将如何swit在选定项上的片段之间切换?例如,片段homeFragment=homeFragment.newInstance();片段notificationsFragment=notificationsFragment.newInstance();在切换情况下,您可以设置selectedFragment=homeFragment或其他一些东西。谢谢,看起来正在工作。再问一个问题:我有三个片段(Home、Dashboard和Notifications)。Home fragment是我的应用程序的主菜单。因此,如果我在仪表板或通知中,并且我按下按钮,我想将onKeyDown设置为Home fragment的back,并且,在Home fragment中,如果我按下back按钮,我想关闭de app。你知道怎么做吗?如果你想在片段之间转换,请调用addToBackStack().谢谢@Thien,有很多代码,但我会试试。Thanks@ThienHuynh第一次运行时如何设置片段?@RazorHead navigationView.getMenu().getItem(0).setChecked(true);使用项0表示HomeFragment是第一个。如果在片段中更改工具栏,则无法工作。屏幕旋转后,工具栏菜单将覆盖
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class HomeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/home"
android:textSize="30sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/bottom_home"
android:icon="@drawable/home_selector"
android:title="Home" />
<item
android:id="@+id/bottom_events"
android:icon="@drawable/events_selector"
android:title="Events" />
<item
android:id="@+id/bottom_contacts"
android:icon="@drawable/contacts_selector"
android:title="Contacts"/>
<item
android:id="@+id/bottom_menu"
android:icon="@drawable/menu_selector"
android:title="Menu" />
</menu>