【问题标题】:How to pass data between Bottom Navigation fragments (or Navigation Drawer)?如何在底部导航片段(或导航抽屉)之间传递数据?
【发布时间】:2020-09-24 19:12:29
【问题描述】:

我有一种情况,我在 android dev 方面不够好,无法实现在底部导航目的地之间传递数据的方法。

我需要从 CreateFragment 中的 EditText 获取文本到一个 RecipesFragment,在那里我将在数据库中添加一个新项目,并且 RecyclerView 将根据这些数据进行更新(我正在使用 LiveData + MVVM)。

My Bottom Navigation Bar

所以,我正在寻求一种方法。我找不到任何适当/具体的解释,我认为在底部导航或导航抽屉中的片段之间传递数据是重要的功能,但我想对于像我这样的初学者来说很难:(。

附加信息:我在 MainActivity 中托管了一个 FrameLayout(id-fragment_container),我根据所选底部导航中的位置更改片段。 (检查代码)

MainActivity.java

public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bottomNavigationView = findViewById(R.id.bottom_navigation);
    bottomNavigationView.setOnNavigationItemSelectedListener(navListener);

    // Keeps the current fragment opened when the device rotates
    if (savedInstanceState == null) {

        // Sets the default selected item in NavigationView
        bottomNavigationView.setSelectedItemId(R.id.nav_home);
    }
}

// Handles switching the view based on NavigationView item selected
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.nav_home:
                        selectedFragment = new HomeFragment();
                        break;
                    case R.id.nav_recipes:
                        selectedFragment = new RecipesFragment();
                        break;
                    case R.id.nav_create:
                        selectedFragment = new CreateFragment();
                        break;
                }

                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        selectedFragment).commit();

                return true;
            }
        };

CreateFragment.java

private void saveRecipe() { //trigered on floating button press
    String title = edit_title.getText().toString();
    String ingredients = edit_ingredients.getText().toString();

    if (title.trim().isEmpty() || ingredients.trim().isEmpty()) {
        Toast.makeText(getActivity(), "Please insert a title and description", Toast.LENGTH_SHORT).show();
        return;
    }

    // THIS SHOULD PASS DATA TO RecipesFragment INTO RECYCLERVIEW
    // TODO

    //
}

RecipesFragment.java

    public void getRecipeFromCreateFragment() {

    // THERE SHOULD BE PASSED DATA FROM CREATE ACTIVITY
    // TODO





    //

    Recipe recipe =
            new Recipe("hh", "title", "ingredients", "instructions", "ss", "image");
    recipeViewModel.insert(recipe);

    Toast.makeText(getActivity(), "Recipe saved", Toast.LENGTH_SHORT).show();
}

【问题讨论】:

    标签: java android android-fragments navigation-drawer android-bottomnav


    【解决方案1】:

    更新:好的,我得到了一位 redditer 同事的帮助,解决方案非常简单,在我的案例中,我什至不应该考虑在片段之间传递数据。

    解决方案是在 CreateFragment 中通过 ViewModel 将配方简单地添加到数据库中,实际上不需要将数据传递给 RecipesFragment 然后将其添加到那里。

    但是,如果有人知道在导航视图(导航抽屉)中的片段之间传递数据的更好、更推荐的方法,那对未来会很有帮助。

    谢谢。

    【讨论】:

      猜你喜欢
      • 2020-04-19
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多