android-fragments Android导航抽屉活动调用片段中的Click事件

mwg9r5ms  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(205)

我在Android Studio中创建了一个Navigation Drawer Activity项目,并添加了一些片段。通过它们进行导航工作正常。
现在,在主片段中,我想添加一些按钮,这些按钮将把用户带到一些片段,否则用户需要通过抽屉式菜单导航到这些片段。换句话说,当用户打开应用程序时,他应该可以选择导航到一些片段,而无需打开抽屉式菜单。但我无法让它工作。
到目前为止,最接近的是主Activity中的以下代码。然而,这只工作一次。当我导航到其他片段并返回到主片段时,它将不再工作。单击按钮应该会像用户导航到抽屉菜单一样。

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.appBarMain.toolbar);
        binding.appBarMain.fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navView;
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_hpfragment, R.id.nav_gallery, R.id.nav_slideshow)
                .setOpenableLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

        Button btnTest = binding.getRoot().findViewById(R.id.btnGotoDensityAlt);
        View.OnClickListener cl = new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                View x = navigationView.findViewById(R.id.nav_hpfragment);
                x.callOnClick();
            }
        };
        btnTest.setOnClickListener(cl);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}

正确的做法是什么?

eit6fx6z

eit6fx6z1#

我是这样解决这个问题的:
我在主活动中添加了这个函数:

public void navigateToHpCalc()
    {
        View view = binding.navView.findViewById(R.id.nav_hpfragment);
        view.callOnClick();
    }

我在片段中调用了这个函数,如下所示:

View.OnClickListener onGoToHpFragmentListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                MainActivity fa = (MainActivity)getActivity();
                fa.navigateToHpCalc();
            }
        };

完整代码:

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.appBarMain.toolbar);
        binding.appBarMain.fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navView;
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_hpfragment, R.id.nav_gallery, R.id.nav_slideshow)
                .setOpenableLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

    }

    public void navigateToHpCalc()
    {
        View view = binding.navView.findViewById(R.id.nav_hpfragment);
        view.callOnClick();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        HomeViewModel homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        final TextView textView = binding.textHome;
        final Button btn = binding.btnGotoDensityAlt;

        homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);

        View.OnClickListener onGoToHpFragmentListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                MainActivity fa = (MainActivity)getActivity();
                fa.navigateToHpCalc();
            }
        };

        View.OnClickListener onGoToGallery = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Navigation.findNavController(root).navigate(R.id.action_nav_home_to_nav_gallery);
            }
        };
        binding.btnGotoDensityAlt.setOnClickListener(onGoToHpFragmentListener);
        binding.btnGoToGallery.setOnClickListener(onGoToGallery);

        return root;
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}

我仍然不确定这是最好的做法,虽然这样做。

相关问题