使用viewPager时隐藏不可见片段中的视图

时间:2015-08-24 12:07:45

标签: java android android-layout android-fragments

我在我的活动中使用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);
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

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

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 } }); 片段内override类中的setUserVisibleHint()方法。改编自this回答:

Fragment