简体   繁体   中英

How to hide item from the MainActivity toolbar in a fragment

In my application I have Toolbar in the MainActivity and inside the MainActivity I have a ViewPager to show 4 fragment.

The toolbar contains some images ( button ).

I want in one of these fragments to hide the image from the toolbar . I wrote the code below, but it hides the image in all the fragments .

My code:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                troy.setVisibility(View.GONE);
            }
        }, 50);
    }
}

I want to hide it just in my current fragment , not all of them.

How can I do it?

You can use addOnPageChangeListener() of your ViewPager

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {       

    }

    @Override
    public void onPageSelected(int position) {
         if (position == 0) { // Condition may vary according to your needs...
             troy.setVisibility(View.GONE);
         } else {
             troy.setVisibility(View.VISIBLE);
         }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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