gpt4 book ai didi

java - 如何从底部导航 fragment 移动到其他 fragment

转载 作者:行者123 更新时间:2023-12-01 17:07:03 25 4
gpt4 key购买 nike

我在其中声明底部应用栏的类的代码是:

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class NavigActivity extends AppCompatActivity {

private BottomAppBar mbottomAppBar;
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navig);
mbottomAppBar = findViewById(R.id.bottom_app_bar);
setSupportActionBar(mbottomAppBar);
fab= findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new AddNewProductFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
});

mbottomAppBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Fragment fragment = null;
switch (item.getItemId())
{
case R.id.home:
fragment = new HomeFragment();
break;
case R.id.notification:
fragment = new NotificationFragment();
break;
case R.id.my_invoices:
fragment = new InvoiceFragment();
break;
}
if(fragment != null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
return false;
}
});
mbottomAppBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//open bottom sheet
BottomSheetDialogFragment bottomSheetDialogFragment = BottomSheetNavigationFragment.newInstance();
bottomSheetDialogFragment.show(getSupportFragmentManager(), "Bottom Sheet Dialog Fragment");
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
}

Bottom Sheet 导航 fragment 的代码是:


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.navigation.NavigationView;

public class BottomSheetNavigationFragment extends BottomSheetDialogFragment {
public static BottomSheetNavigationFragment newInstance() {

Bundle args = new Bundle();

BottomSheetNavigationFragment fragment = new BottomSheetNavigationFragment();
fragment.setArguments(args);
return fragment;
}
//Bottom Sheet Callback
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {

@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}

}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
//check the slide offset and change the visibility of close button
if (slideOffset > 0.5) {
closeButton.setVisibility(View.VISIBLE);
} else {
closeButton.setVisibility(View.GONE);
}
}
};

private ImageView closeButton;

@SuppressLint("RestrictedApi")
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
//Get the content View
View contentView = View.inflate(getContext(), R.layout.bottom_navigation_drawer, null);
dialog.setContentView(contentView);

NavigationView navigationView = contentView.findViewById(R.id.navigation_view);

//implement navigation menu item click event
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
F
Fragment fragment;
switch (item.getItemId()) {
case R.id.user_details:

break;
case R.id.settings:

break;
case R.id.help:

break;
case R.id.logout:
MainActivity obj = new MainActivity();
obj.logout();
startActivity( new Intent(getActivity(), MainActivity.class));
break;
}

return false;
}
});
closeButton = contentView.findViewById(R.id.close_image_view);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//dismiss bottom sheet
dismiss();
}
});

//Set the coordinator layout behavior
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();

//Set callback
if (behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
}
}
}

我无法理解如何将屏幕从这个 fragment 移动到其他 fragment - 因为 getSupportFragmentManager() 在此不可用。当我尝试生成 Intent 时,它会抛出错误。

如有任何建议或帮助,我们将不胜感激。谢谢!

最佳答案

您可以使用可变 Activity 。在附件 fragment 时声明它,如下所示:

 private AppCompatActivity activity;

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
if (context instanceof AppCompatActivity) {
activity = (AppCompatActivity) context;
}
}

及之后

/.../

activity.getSupportFragmentManager /...some logic.../

关于java - 如何从底部导航 fragment 移动到其他 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61456907/

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