片段动画问题

时间:2018-10-15 06:44:29

标签: android animation android-viewpager fragment

片段中的简单动画。我有三个片段,我必须在片段中创建视图动画,但是现在面临的问题是动画在第二个片段中不起作用,当我重新访问该片段时,动画也无法正常工作。

适配器类

public class MyPageAdapter extends FragmentPagerAdapter {
    private final int NUM_ITEMS = 3;
    Context mContext;

    public MyPageAdapter(FragmentManager fm, Context mContext) {
        super(fm);
        this.mContext = mContext;
    }

    public int getCount() {
        return NUM_ITEMS;
    }

    public Fragment getItem(int position) {
        // MainActivity.updatedposition(position);

        if (position == 0)
            return SliderOne.newInstance();
        else if (position == 1)
            return SliderTwo.newInstance();
        else if (position == 2)
            return SliderThree.newInstance();
        return null;
    }
}

滑块活动

pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            public void onPageScrollStateChanged(int state) {
            }

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

            public void onPageSelected(int position) {
                if (position == 0) {
                    SliderOne fragment = new SliderOne();
                    fragment.initAnimation(mContext);
                }
                else if (position == 1) {
                    SliderTwo fragment = new SliderTwo();
                    fragment.initAnimation(mContext);
                }
                if (position == 2) {
                    SliderThree fragment = new SliderThree();
                    fragment.initAnimation(mContext);
                }
                // Check if this is the page you want.
            }
        });

片段类

public class SliderThree extends Fragment implements PageSelectedAnimation {

    ImageView logo;
    TextView tv_appname;
    CustomTextView tv_msg_2;
    Animation smalltobig, nothingtocome, btnanim;
    Context mContext;

    public static SliderThree newInstance() {
        SliderThree fragment = new SliderThree();
        return fragment;
    }


    public SliderThree() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.slider_three, null);
        mContext = getActivity();
        logo = root.findViewById(R.id.logo);
        tv_appname = root.findViewById(R.id.tv_appname);
        tv_msg_2 = root.findViewById(R.id.tv_msg_2);
        tv_msg_2.setText(Html.fromHtml("Message other users to negotiate<br><p>the perfect trade or deal.Always</p><p>use your best judgement when</p><p>buying or selling.</p>"));

        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void initAnimation(Context mContext) {
        smalltobig = AnimationUtils.loadAnimation(mContext, R.anim.smalltobig);
        nothingtocome = AnimationUtils.loadAnimation(mContext, R.anim.nothingtocome);
        btnanim = AnimationUtils.loadAnimation(mContext, R.anim.nothingtocome);
        logo.setAnimation(smalltobig);
        logo.setAnimation(nothingtocome);
        tv_appname.setAnimation(btnanim);
    }
}

已创建界面

public interface PageSelectedAnimation {

    void initAnimation(Context context);
}

错误:-

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.view.View android.view.View.findViewById(int)”         在com.xxx.slider.SliderThree.initAnimation(SliderThree.java:79)         在com.xxx.activities.SliderActivity $ 2.onPageSelected(SliderActivity.java:103)

共享我的代码: Fragment Code

1 个答案:

答案 0 :(得分:0)

我从您正在使用的标签中假设,您正在ViewPager上包含这些片段。

ViewPager为当前和相邻节点充电,因此在需要时它们可以准备就绪。因此,您的动画在充电后会用完屏幕。

我建议使用ViewPager.onPageChangeListener

viewPager.addOnPageChangeListener(new OnPageChangeListener() {
    public void onPageScrollStateChanged(int state) {}
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    public void onPageSelected(int position) {
        // Check if this is the page you want.
    }
});

然后从那里运行动画方法。

编辑:

您可以很好地在每个Fragment上创建和实现接口。

interface PageSelectedAnimation{
    fun initAnimation()
}

public class SliderThree extends Fragment implements PageSelectedAnimation

最后:

public void onPageSelected(int position) {
        ((PageSelectedAnimation)getYourFragment(position)).initAnimation()
    }