gpt4 book ai didi

android - 如何禁用某些 fragment 的底部导航

转载 作者:行者123 更新时间:2023-12-05 00:15:14 25 4
gpt4 key购买 nike

您好,我在主要 Activity 中添加了底部导航,其余所有布局都是 fragment ,现在我有一个登录 fragment ,我不想显示底部导航,我什至不想将底部导航包含到每个 fragment 分别,是否有任何方法可以在某些 fragment (例如登录 fragment )中禁用底部导航

if you need more reference please tell me i will update the question

Main_Activity.java

public class MainActivity extends AppCompatActivity {

BottomNavigationView bottomNavigationView;
Deque<Integer> integerDeque = new ArrayDeque<>(4);
boolean flag = true;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
setContentView(R.layout.activity_main);
Window window = this.getWindow();
window.setStatusBarColor(this.getResources().getColor(R.color.black));
bottomNavigationView = findViewById(R.id.bottom_navigation_view);
integerDeque.push(R.id.nav_home);
loadFragments(new Home_Fragment());
bottomNavigationView.setSelectedItemId(R.id.nav_home);

bottomNavigationView.setOnNavigationItemSelectedListener(
item -> {
int id = item.getItemId();
if (integerDeque.contains(id)) {
if (id == R.id.nav_home) {
integerDeque.size();
if (flag) {
integerDeque.addFirst(R.id.nav_home);
flag = false;
}
}
integerDeque.remove(id);
}
integerDeque.push(id);
loadFragments(getFragment(item.getItemId()));
return false;
}
);

}

@SuppressLint("NonConstantResourceId")
private Fragment getFragment(int itemId) {
switch (itemId) {
case R.id.nav_home:
bottomNavigationView.getMenu().getItem(0).setChecked(true);
return new Home_Fragment();
case R.id.nav_following:
bottomNavigationView.getMenu().getItem(1).setChecked(true);
return new Following_Fragment();
case R.id.nav_upload:
bottomNavigationView.getMenu().getItem(2).setChecked(true);
return new Upload_Fragment();
case R.id.nav_notification:
bottomNavigationView.getMenu().getItem(3).setChecked(true);
return new Notification_Fragment();
case R.id.nav_profile:
bottomNavigationView.getMenu().getItem(4).setChecked(true);
return new Profile_Fragment();
}
bottomNavigationView.getMenu().getItem(0).setChecked(true);
return new Home_Fragment();
}

public void loadFragments(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment, fragment.getClass().getSimpleName())
.commit();
}
}

@Override
public void onBackPressed() {
integerDeque.pop();
if (!integerDeque.isEmpty()) {
loadFragments(getFragment(integerDeque.peek()));
} else {
finish();
}
}
}

更新1//根据答案添加login_fragment,但它不起作用

Login_Fragment.java

    public class Login_Fragment extends Fragment {
@Nullable
@org.jetbrains.annotations.Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
if (getActivity() != null && getActivity() instanceof MainActivity)
((MainActivity) getActivity()).bottomNavigationView.setVisibility(View.GONE);
return inflater.inflate(R.layout.login_fragment, container, false);
}
}

更新 2:- 好的,答案中给出的方法有效,但是当我转到上一个 fragment 时,所有 fragment 的底部导航都消失了,或者我说整个应用程序没有底部导航

最佳答案

您可以在 fragment 中执行以下操作:

BottomNavigationView navBar = getActivity().findViewById(R.id.bottomBar);
navBar.setVisiblity(View.GONE);

 public class Login_Fragment extends Fragment {
@Nullable
@org.jetbrains.annotations.Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
if (getActivity() != null && getActivity() instanceof MainActivity)
((MainActivity) getActivity()).bottomNavigationView.setVisibility(View.GONE);
return inflater.inflate(R.layout.login_fragment, container, false);

}
}

关于android - 如何禁用某些 fragment 的底部导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68417873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com