切换片段,就像导航抽屉一样(带有返回箭头)

huangapple go评论82阅读模式
英文:

Switch fragment like Nav drawer does (with back arrow)

问题

我正在尝试制作一个应用程序,但在一个问题上遇到了困难。
我想通过点击按钮从“Accueil”(主页)转到例如“Depense”(支出)。
正如您在 GIF 图片中所看到的(在帖子末尾),如果我通过菜单这样做,顶部会出现一个返回箭头。而且,如果我从主页转到“Avance”(预付款),我会得到菜单图标。

我已经找出了这是从哪里来的,我使用了标准的导航抽屉,如果我在代码中添加了我的片段,我会得到菜单,如果没有,我会得到箭头。

mAppBarConfiguration = new AppBarConfiguration.Builder(
    R.id.nav_home, R.id.nav_gallery, R.id.nav_avance)
    .setDrawerLayout(drawer)
    .build();

R.id.nav_avance 在其中,但 R.id.depense 不在。这就是为什么我会得到箭头。

我试图做与标准抽屉相同的事情,当我点击时,但我无法做到。
我目前在我的“Accueil”(主页)片段代码中尝试了以下内容:

Button b = root.findViewById(R.id.player_1);
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Fragment fragment = new DepenseFragment();

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.nav_host_fragment, fragment);
        ft.commit();
   }
});

但是,如果我点击按钮,新的片段会出现,但是.. 顶部会有菜单图标,并且旧文本仍然是“Accueil”,而不是“Depense”。

请问您是否可以帮助我实现这一点?
供您参考,我希望在这个新的片段中,根据我点击的按钮,传递一个相应的数字。
例如,如果我点击 player_1,我希望传递数字 1.. player_2 传递数字 2 给新的片段等等。

谢谢您的帮助 切换片段,就像导航抽屉一样(带有返回箭头)

英文:

I'm trying to make an application and I'm struggling on a problem.
I want to get from "Accueil" (home) to for exemple "Depense" (Expense) by a button click.
As you may check in the gif (at end of post), if I do so by the menu, i've got a back arrow on top. And, If i go from home to "Avance" (Advance), i've got the menu icon.

I manage to find out where this come from, I use the standard navigation drawer and If i add my fragment in is code I've got the menu, if not, i've got the arrow.

        mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_avance)
            .setDrawerLayout(drawer)
            .build();

R.id.nav_avance is in but R.id.depense is not. That's why i've got the arrow.

I'm trying to make the same as the standard drawer does when I click but I can't manage to do so.
I've tried this for the moment in my "Accueil" (home) fragment code :

        Button b = root.findViewById(R.id.player_1);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = new DepenseFragment();

                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction ft = fragmentManager.beginTransaction();
                ft.replace(R.id.nav_host_fragment, fragment);
                ft.commit();
           }
    });

But If i click on the button, the new fragment appear BUT .. I've got the menu icon on top, and the old text which is "Accueil" and not "Depense".

Could you please help me achieve this ?
For information, I want, in this new fragment, a number according to the button I've click.
For example, if I click player_1, I want to pass 1.. player_2 pass 2 to the new fragment etc.

Thanks for your help 切换片段,就像导航抽屉一样(带有返回箭头)

切换片段,就像导航抽屉一样(带有返回箭头)

答案1

得分: 0

我认为没有“内置”解决方案,也就是说,当您点击按钮时,您必须手动操作/更新ActionBar中的图标。因此,我会在您的onClick()方法中添加以下代码:

ActionBar actionbar = this.getSupportActionBar();
if (actionbar != null) {
    // 获取要显示的图标
    Drawable icon = this.getResources().getDrawable(R.drawable.baseline_arrow_back_white_24);

    // 如果需要,根据您的主题进行调整
    icon.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);

    // 设置/更新图标
    actionbar.setHomeAsUpIndicator(backArrow);
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeButtonEnabled(true);
}

请注意,this 引用了您的活动对象,所以您需要适应那部分。由于我不知道 b 位于何处,我只能猜测您需要如何进行调整。例如,如果 b 已经在您的活动中,您可能会将 this 替换为 MainActivity.this

如果 this.getSupportActionBar() 返回 null,您可能需要调整您的 XML 布局。请参阅此处作为起点:
https://developer.android.com/training/appbar/setting-up

有关所使用方法的详细说明(例如 setDisplayHomeAsUpEnabled),请查看此处:
https://developer.android.com/reference/android/app/ActionBar

请注意,上述的 drawable 图标来自这里(两个图标都可用):
https://material.io/resources/icons/?style=baseline

英文:

I think that there is no "build-in" solution, i.e., you have to manually manipluate/update the icon in the ActionBar when you click on your button. Hence, I would add the following code to your onClick() method:

ActionBar actionbar = this.getSupportActionBar();
if (actionbar != null) {
    // get the drawable which you want to show
    Drawable icon = this.getResources().getDrawable(R.drawable.baseline_arrow_back_white_24);

    // if necessary, adapt it to your theme
    icon.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);

    // set/update the icon
    actionbar.setHomeAsUpIndicator(backArrow);
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeButtonEnabled(true);
}

Please note that this refers to your activity object, so you have to adapt that part. As I don't know "where" b is located I can only guess how you have to adapt it. For example if b is already in your Activity you might replace this with MainActivity.this.

If this.getSupportActionBar() returns null, you might to adapt your XML layout. Please see here as a starting point:
https://developer.android.com/training/appbar/setting-up

Please look here for a detailed description of the used methods (e.g., setDisplayHomeAsUpEnabled):
https://developer.android.com/reference/android/app/ActionBar

Please note that the drawable above comes from here (both icons are available):
https://material.io/resources/icons/?style=baseline

答案2

得分: 0

这是我在经过(少数)几个小时的研究和尝试后得出的答案。

Button b = root.findViewById(R.id.player_1);
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        NavOptions.Builder builder = new NavOptions.Builder();
        NavController navController = Navigation.findNavController(root);

        NavOptions options = builder.build();
        try {
            navController.navigate(R.id.nav_joueur, null, options);
        } catch (IllegalArgumentException e) {
        }
    }
});

我已经发现抽屉使用导航控制器来更改显示的片段。
我设法获取它并进行更改,以获得与点击菜单时相同的行为 切换片段,就像导航抽屉一样(带有返回箭头)

英文:

Here come my own answer after (few) hours of research and try.

        Button b = root.findViewById(R.id.player_1);
        b.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View v) {
                NavOptions.Builder builder = new NavOptions.Builder();
                NavController navController = Navigation.findNavController(root);

                NavOptions options = builder.build();
                try {
                    navController.navigate(R.id.nav_joueur, null, options);
                } catch (IllegalArgumentException e) {
                }
        }
    });

I have found that the drawer use the navigation controler in order to change the displayed fragment.
I've manage to get it and change it in order to get the same behavior as is I click on the menu 切换片段,就像导航抽屉一样(带有返回箭头)

huangapple
  • 本文由 发表于 2020年10月24日 17:56:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64512112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定