我有一个带有FragmentPageAdapter的ViewPager。当显示片段时,我希望它开始动画(淡入片段中的视图)。
然而,动画运行不一致,当我向左/向右滑动时,它会在每几页上运行。我想我需要在onPageSelected事件上启动动画,但我无法弄清楚如何获取片段并启动动画。
我的代码如下。 TourFragment是我使用FragmentPageAdapter添加到ViewPager的片段,而TourActivty包含ViewPager。
TourFragment
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;
}
}
TourActivity
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();
}
}
答案 0 :(得分:4)
你应该在片段中使用
@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 { }
}
答案 1 :(得分: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) {
}
});
答案 2 :(得分: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
中的实例。
答案 3 :(得分:0)
我有一个类似的问题,在这里解决了它: Animation of nested fragment within ViewPager fragment is triggered before render
在您的情况下,相同的解决方案将如下所示:
的java:
v.post(new Runnable() {
@Override
public void run() {
ivTitle.startAnimation(fadeInAnimation)
}
})
kotlin:
v.post(Runnable {
ivTitle.startAnimation(fadeInAnimation)
})
确保视图确实被绘制