简体   繁体   English

打开某些片段时如何在工具栏上隐藏/显示图标

[英]How hide/show icon on toolbar when opening some fragments

I have few fragments and one activity.我的片段很少,只有一项活动。 The app contains toolbar which is always visible.该应用程序包含始终可见的工具栏。 There is icon on my toolbar.我的工具栏上有图标。 And i need to hide this icon when user opens 2,4,5 fragments and show this icon when users open 1 and 3 fragment.我需要在用户打开 2、4、5 片段时隐藏此图标,并在用户打开 1 和 3 片段时显示此图标。 I don't need all code for this logic, I need advice how can I implement it and where add some logic for this behavior我不需要此逻辑的所有代码,我需要建议如何实现它以及在哪里为此行为添加一些逻辑

Step 1: create an interface FragmentListener with one method:第 1 步:使用一种方法创建接口 FragmentListener:

public interface MyFragmentListener {
    void onChangeToolbarImage(boolean show);
}

Step 2: implement in YourActivity:第 2 步:在 YourActivity 中实现:

ImageView toolarImage= findViewById(R.id.toolbarimage)()///

     @Override
    public void onChangeToolbarImage(boolean show) {
       if()
        { //check your imageView is Visible or not

        toolbarImage.setVisibility(show); //change your ImageView's visibility
       }
    }

Step 3: in each fragment you need get instance from interface:第 3 步:在每个片段中,您需要从接口获取实例:

private MyFragmentListener fragmentListener;

Step 4: override onAtach in your Fragment:第 4 步:覆盖片段中的 onAtach:

  @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof MyFragmentListener)
            fragmentListener = (MyFragmentListener) context;

        fragmentListener.onChangeToolbarTitleImage(true or false);
    }

The following is true assuming you are using jetpack's navigation and single activity:假设您使用的是 jetpack 的导航和单个活动,则以下情况为真:

Add destination change listener to your main nav controller inside your activity ( addOnDestinationChangedListener , the interface is NavController.OnDestinationChangedListener ).将目标更改侦听器添加到您的活动中的主导航 controller( addOnDestinationChangedListener ,接口是NavController.OnDestinationChangedListener )。 Inside the listener, you could check for destination.id in onDestinationChanged implementation.在侦听器内部,您可以在onDestinationChanged实现中检查destination.id Actually, you could create two sets like this实际上,您可以像这样创建两个集合

private val twoFourFiveDestinations =
        setOf(R.id.two, R.id.four, R.id.five) 
private val oneThreeDestinations =
        setOf(R.id.one, R.id.three)

just to make check like this if(twoFourFiveDestinations.contains(destination.id)... and manage your icon visibility accordingly, it will make life easier.只是为了进行这样的检查if(twoFourFiveDestinations.contains(destination.id)...并相应地管理您的图标可见性,这将使生活更轻松。

Alternative solution would be to hand over icon management to fragments.替代解决方案是将图标管理移交给片段。 You could define some interface for comm with activity, and manage toolbar icon when respective fragment is up and running.您可以定义一些与活动通信的接口,并在相应的片段启动并运行时管理工具栏图标。 But you'd need to do that in every fragment of your question.但是您需要在问题的每个片段中都这样做。

Create a companion object or static variable if you are using java.如果您使用的是 java,请创建伴随的 object 或 static 变量。

class Util {companion object { lateinit var toolbarImg: ImageView }}  

Inside your Main Activity onCreate initialize your toolbar and imageview在您的主活动 onCreate 中初始化您的工具栏和 imageview

toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
Util.toolbarImg = toolbar.findViewById(R.id.cartImg)

XML XML

    <com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <ImageView
            android:id="@+id/cartImg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:visibility="visible"
             />

    </androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>

Now all you need to do is control the visibility of this ImageView.现在您需要做的就是控制这个 ImageView 的可见性。 On Fragment transaction分片交易

To Hide隐藏

if(Util.toolbarImg.visibility == View.VISIBLE){
Util.toolbarImg.visibility = View.GONE }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM