gpt4 book ai didi

android - 在 ViewPager 上,在 onPageSelected 上的 Fragment 上启动动画

转载 作者:太空狗 更新时间:2023-10-29 16:20:08 27 4
gpt4 key购买 nike

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

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

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

旅游 fragment

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;
}
}

旅游 Activity

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();
}
}

最佳答案

你应该在你的 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 { }
}

关于android - 在 ViewPager 上,在 onPageSelected 上的 Fragment 上启动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17348227/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com