简体   繁体   中英

Change image in unfocussed ViewPager Fragment

So I have a view pager implemented like this 在此处输入图片说明

In the main view I have button at the top right of the screen thats similar to a navigation bar. When I click it or swipe it I want it to disappear and when it refocuses on the main screen I want it to appear again. I've been trying it for hours and have had no luck.

I know I need to use setVisibility(View.GONE)/VISIBLE I just keep getting null pointers or the image just wont change.

Ive implemented OnPageChangeListener() and tried using otto events to change the button state as well, but the event is never being received.

In short the main goal is to be able to interact with elements on any view not visibly shown in the ViewPager

尝试使用ArrayList<Fragment> ,当您位于OnPageChangeListener() ,使用上一个和下一个Fragments做您想做的事情。

I have been encounter this problem days ago, I override setUserVisibleHint() finally solve it.

public class YourFragement extend android.support.v4.app.Fragment {
    ...
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            mView.setVisibility(View.VISIBLE);
        } else {
            mView.setVisibility(View.GONE);
        }
    }
    ...
}

someone said override setMenuVisibility() also work, but I haven't try it, more information discuss in this answer .

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