如何实现单个FloatingActionButton访问不同Fragment?求技术帮助
Hey there! I see you want your single FloatingActionButton to trigger different actions based on whether the user is on InventoryFragment or RecipeFragment—let's get that sorted out.
The issue with your current code is that it doesn't distinguish between the two fragments; it just launches the same EditorActivity no matter which one is active. Here's how to fix that:
Step 1: Get the currently active fragment correctly
Since you're using a ViewPager, there are two reliable ways to fetch the visible fragment. Let's cover both:
Approach 1: Use ViewPager's auto-generated fragment tag
ViewPager automatically assigns tags to its fragments in the format android:switcher:[viewPagerId]:[position]. You can use this to grab the current fragment directly:
FloatingActionButton fab = findViewById(R.id.fab); // Make sure you have a reference to your ViewPager from the layout ViewPager viewPager = findViewById(R.id.viewPager); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int currentPagePosition = viewPager.getCurrentItem(); // Fetch the fragment using ViewPager's default tag structure Fragment currentFragment = getSupportFragmentManager() .findFragmentByTag("android:switcher:" + R.id.viewPager + ":" + currentPagePosition); if (currentFragment != null) { // Check which fragment is active and trigger the right action if (currentFragment instanceof InventoryFragment) { // Action for InventoryFragment: e.g., open an inventory-specific editor Intent intent = new Intent(MainActivity.this, InventoryEditorActivity.class); startActivity(intent); } else if (currentFragment instanceof RecipeFragment) { // Action for RecipeFragment: e.g., open a recipe-specific editor Intent intent = new Intent(MainActivity.this, RecipeEditorActivity.class); startActivity(intent); } } } });
Approach 2: Track fragments in your PagerAdapter
For more control, modify your FragmentPagerAdapter (or FragmentStatePagerAdapter) to keep a list of added fragments:
public class MyPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> fragmentList = new ArrayList<>(); public MyPagerAdapter(FragmentManager fm) { super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); } // Add this method to populate the fragment list public void addFragment(Fragment fragment) { fragmentList.add(fragment); } }
Then set up the adapter in your MainActivity like this:
ViewPager viewPager = findViewById(R.id.viewPager); MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager()); adapter.addFragment(new InventoryFragment()); adapter.addFragment(new RecipeFragment()); viewPager.setAdapter(adapter);
Now you can get the current fragment directly from the adapter:
fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int currentPagePosition = viewPager.getCurrentItem(); Fragment currentFragment = adapter.getItem(currentPagePosition); if (currentFragment instanceof InventoryFragment) { // Inventory-specific action goes here } else if (currentFragment instanceof RecipeFragment) { // Recipe-specific action goes here } } });
Quick Notes
- Replace
InventoryEditorActivityandRecipeEditorActivitywith your actual target activities (you can also adjust the action entirely—like showing a bottom sheet instead of launching an activity). - Double-check that your
ViewPagerhas the IDR.id.viewPager(match what's in your layout XML). - If you're using Jetpack
ViewPager2, let me know and I can tweak the code to fit that setup!
内容的提问来源于stack exchange,提问作者Wissam

