2016-03-27 4 views
0

Я хочу начать исчезать в анимации, когда пользователь перебирает следующий экран, но я не могу заставить его работать правильно. Проблема в том, что анимация начинается в то же время на первом и втором экране, на третьем и четвертом и т. Д.Android: Как установить текстовую анимацию в ViewPager

Вот мой PageFragment;

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

    Animation in; 
    Animation out; 

    in = new AlphaAnimation(0.0f, 1.0f); 
    in.setDuration(2000); 

    out = new AlphaAnimation(1.0f, 0.0f); 
    out.setDuration(2000); 

    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.page_fragment_layout,container,false); 
    textView = (TextView)view.findViewById(R.id.textView); 
    Bundle bundle = getArguments(); 
    String message = bundle.getString("count"); 
    textView.setText(message); 
    textView.setAnimation(in); 
    return view; 
}} 

SwipeAdapter

public class SwipeAdapter extends FragmentStatePagerAdapter{ 
public SwipeAdapter(FragmentManager fm) { 
    super(fm); 
} 

@Override 
public Fragment getItem(int i) { 
    String[] historia = {"1", "2", "3", "4", "5"}; 
    Fragment fragment = new PageFragment(); 
    Bundle bundle = new Bundle(); 
    bundle.putString("count", historia[i]); 
    fragment.setArguments(bundle); 
    return fragment; 
} 

@Override 
public int getCount() { 
    return 5; 
}} 

MainActivity

public class MainActivity extends FragmentActivity { 
ViewPager viewPager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    viewPager = (ViewPager) findViewById(R.id.view_pager); 
    SwipeAdapter swipeAdapter = new SwipeAdapter(getSupportFragmentManager()); 
    viewPager.setAdapter(swipeAdapter); 

}} 

Спасибо за любые полезные ответы! :)

ответ

0

Регистрация ViewPager.OnPageChangeListener с вашим swipeAdapter (как в swipeAdapter.addOnPageChangeListener()). Затем вы можете переместить свой анимационный материал в public void onPageSelected(int position). Не забывайте называть их добавить/удалить слушателей в onResume/onPause)

Смежные вопросы