当使用viewPager时,从不可见片段隐藏视图



我在我的活动中使用ViewPager来显示6个页面(片段),这些页面都有一个视图,当页面被选中时"弹出"。当我向后或向前滚动时,我想隐藏片段中的视图,以便能够再次正确地开始动画。现在,当我在片段之间滑动时,我看到旧的"弹出"图像,当新的片段被选中时,动画再次发生。

public class MyFragment extends Fragment implements MylActivity.MyFragmentInterface {
    private static final String TAG = "MyFragment";
    private int layoutId;
    private ViewGroup rootView;
    private ImageView popup;
    @Override
    public void fragmentStartAnimation() {
        switch (layoutId) {
            case R.layout.fragment_2:
            case R.layout.fragment_3:
            case R.layout.fragment_4:
            case R.layout.fragment_5:
                Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.popup);
                 popup = (ImageView) rootView.findViewById(R.id.popup);
                popup.setVisibility(View.VISIBLE);
                popup.startAnimation(animation);
                break;
        }
    }
    @Override
    public void fragmentClearAnimation() {
        switch (layoutId) {
            case R.layout.fragment_2:
            case R.layout.fragment_3:
            case R.layout.fragment_4:
            case R.layout.fragment_5:
                if (popup != null) {
                   popup.setVisibility(View.INVISIBLE);
                }
                break;
        }
    }

    public void setPageNo(int pageNo) {
        switch (pageNo) {
            case 1:
                layoutId = R.layout.fragment_1;
                break;
            case 2:
                layoutId = R.layout.fragment_2;
                break;
            case 3:
                layoutId = R.layout.fragment_3;
                break;
            case 4:
                layoutId = R.layout.fragment_4;
                break;
            case 5:
                layoutId = R.layout.fragment_5;
                break;
            case 6:
                layoutId = R.layout.fragment_6;
                break;
            default:
                layoutId = 0;
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (layoutId == 0) {
            return new TextView(container.getContext());
        }
        if (rootView == null){
            rootView = (ViewGroup) inflater.inflate(layoutId, container, false);
            float actualHeight = container.getResources().getDisplayMetrics().heightPixels;
            // all images have exaclty same height of 591px fit for screen of 960px
            rootView.getChildAt(0).getLayoutParams().height = (int)(actualHeight/1.62 + 0.5);
        }else{

  ViewGroup parent = (ViewGroup) rootView.getParent();
        parent.removeView(rootView.findViewById(R.id.popup));
    }
    return rootView;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (!isVisibleToUser){
        if (rootView!= null && popup != null) {
            Log.d(TAG, "not visible");
            ViewGroup parent = (ViewGroup) rootView.getParent();
            parent.removeView(popup);
        }
    }
}

}

您可以使用ViewPager.OnPageChangeListener。当页面发生变化时,将所有动画重置为原始状态:

viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    public void onPageScrollStateChanged(int state) {
        //Empty
    }
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        //Empty
    }
    public void onPageSelected(int position) {
        // Refresh your animations
    }
});

或者,您可以尝试在您的片段内的Fragment类中的setUserVisibleHint()方法override。改编自这个答案:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    // Make sure that we are currently visible
    if (this.isVisible()) {
        // If we are becoming invisible, then...
        if (!isVisibleToUser) {
            //Reset your animation, as fragment is being scrolled out of view
        }
    }
}

最新更新