2

我有一个带有 FragmentPageAdapter 的 ViewPager。显示片段时,我希望它启动动画(淡入片段中的视图)。

但是,动画运行不一致,当我向左/向右滑动时,它每隔几页就会运行一次。我想我需要在 onPageSelected 事件上启动动画,但我不知道如何获取片段并启动动画。

我的代码如下。TourFragment 是我使用 FragmentPageAdapter 添加到 ViewPager 的片段,而 TourActivty 包含 ViewPager。

旅游片段

public class TourFragment extends Fragment  {

    Tour tour;
    ImageView ivTitle;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_tour,
            container, false);

    RelativeLayout rlImageContainer = (RelativeLayout) v.findViewById(R.id.tour_image_container);

    Resources resources = getActivity().getResources();

    ivTitle = (ImageView) v.findViewById(R.id.tour_title);
    ivTitle.setImageDrawable(resources.getDrawable(tour.getDrawableTitleId()));

    Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.fadein);
    ivTitle.startAnimation(fadeInAnimation );

    return v;
}

@Override
public void onResume() {
    super.onResume();
    Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.fadein);
    ivTitle.startAnimation(fadeInAnimation );
}



public static Fragment newInstance(Tour tour) {

    TourFragment f = new TourFragment();
    f.tour = tour;
    return f;
}
}

旅游活动

public class TourActivity extends FragmentActivity {

MyPageAdapter pageAdapter;
String tourType;
Drawable drawableSelected;
Drawable drawableNotSelected;
LinearLayout llIndicators;
int prevPagePosition = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tour);

    List<Fragment> fragments = getFragments();
    pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
    ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
    pager.setAdapter(pageAdapter);

    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i2) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
        });
    }

    private List<Fragment> getFragments(){
    List<Fragment> fList = new ArrayList<Fragment>();

    List<Tour> tours = new ArrayList<Tour>();

        messages = getResources().getStringArray(R.array.guess_tour_messages);
        Tour tour = new Tour();
        tour.setDrawableImageId(R.drawable.tu_guest_image_1);
        tour.setDrawableTitleId(R.drawable.tu_guest_text_1);
        tour.setMessage(messages[0]);
        tour.setPageIndex(0);
        tour.setTotalPages(messages.length);
        tours.add(tour);

        tour = new Tour();
        tour.setDrawableImageId(R.drawable.tu_guest_image_2);
        tour.setDrawableTitleId(R.drawable.tu_guest_text_2);
        tour.setMessage(messages[1]);
        tour.setPageIndex(1);
        tour.setTotalPages(messages.length);
        tours.add(tour);

        tour = new Tour();
        tour.setDrawableImageId(R.drawable.tu_guest_image_3);
        tour.setDrawableTitleId(R.drawable.tu_guest_text_3);
        tour.setMessage(messages[2]);
        tour.setPageIndex(2);
        tour.setTotalPages(messages.length);
        tours.add(tour);

        tour = new Tour();
        tour.setDrawableImageId(R.drawable.tu_guest_image_4);
        tour.setDrawableTitleId(R.drawable.tu_guest_text_4);
        tour.setMessage(messages[3]);
        tour.setPageIndex(3);
        tour.setTotalPages(messages.length);
        tours.add(tour);


    for (Tour tour : tours) {
        fList.add(TourFragment.newInstance(tour));
    }


    return fList;
}

}

class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;

public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

@Override
public Fragment getItem(int position) {
    return this.fragments.get(position);
}

@Override
public int getCount() {
    return this.fragments.size();
}
}
4

4 回答 4

5

You should use in your fragment

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) { 
        Log.v(TAG, "run now");
            Animation animationFadeIn = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_view);
            iv.startAnimation(animationFadeIn);
            animationFadeIn.setFillAfter(true);
    }
    else {  }
}
于 2013-11-18T02:46:29.577 回答
1

我想到了。

在 onPageSelected 上,我使用片段中的 getView 来获取我想要制作动画的视图。

    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i2) {

        }

        @Override
        public void onPageSelected(int position) {

            View v = pageAdapter.getItem(position).getView();

            ImageView ivTitle = (ImageView) v.findViewById(R.id.tour_title);

            Animation fadeInAnimation = AnimationUtils.loadAnimation(TourActivity.this, R.anim.fadein);
            ivTitle.startAnimation(fadeInAnimation );
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });
于 2013-06-27T18:36:47.360 回答
0

在您的FragmentPagerAdapter实现中,您需要覆盖public Object instantiateItem(viewGroup container, int index). 在基类中,片段仅FragmentManager在首次创建适配器时添加一个实例。每当您分页时,屏幕外的片段都会被分离但不会被删除。缓存带有对象上标签的FragmentManager实例。然后在它们被分页返回时附加它们。项目标签的默认实现在instantiateItem每个Fragment内部都FragmentAdapter带有一个硬编码标签。您需要Fragment使用唯一标签标记您使用的每个。有了这个地方,您可以FragmentManager使用public abstract Fragment findFragmentByTag (String tag).

String[] tags = {"tag1", "tag2", "tag3", "tag4", "tag5"};

@Override
public Object instantiateItem(ViewGroup container, int index) {
    FragmentTransaction ft = fm.beginTransaction();

    Fragment fragment = fm.findFragmentByTag(tags[index]);
    if (fragment == null) {
        fragment = getItem(index);
        ft.add(container.getId(), fragment, mTags[index]);
    }
    else {
        ft.attach(fragment);
    }

    ft.commit();

    return fragment;
}

public String getTag(int index) {
    return mTags[index];
}

您现在应该可以访问这些实例了onPageSelected

于 2013-06-27T18:06:31.097 回答
0

我有一个类似的问题,在这里解决了它: ViewPager 片段中嵌套片段的动画在渲染之前被触发

在您的情况下,相同的解决方案如下所示:

爪哇:

v.post(new Runnable() {
  @Override
  public void run() {
    ivTitle.startAnimation(fadeInAnimation)
  }
})

科特林:

v.post(Runnable {
  ivTitle.startAnimation(fadeInAnimation)
})

它确保视图确实被绘制

于 2017-12-22T08:51:19.040 回答