androidanimationandroid-fragmentsswipeview

Using animations in fragments of PagerAdapter


I am trying to do SwipeViews with some animations in each fragment. For example I would like to change alpha of button from 0 to 1.

I wrote animation

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
    android:duration="2000"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/linear_interpolator"
    android:toAlpha="1.0"/>
</set> 

And I have working PagerAdapter with fragmets like this

public class FragmentOne extends Fragment {
private Button bw;
private Animation anim;
private Context CON;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    CON=container.getContext();
    View vw = inflater.inflate(R.layout.fragemnt1,container,false);
    bw=(Button)vw.findViewById(R.id.button);
    anim= AnimationUtils.loadAnimation(CON,R.anim.animacia);
    bw.startAnimation(anim);
    return inflater.inflate(R.layout.fragemnt1,container,false);
}
}

with layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F20C36" >


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="60dp"
    android:visibility="invisible"/>


</RelativeLayout>

but those animations does not work. Nothing happens. Could somebody help me?

EDIT: Implementation of my PagerAdapter

public class PagerAdapter extends FragmentPagerAdapter {

Context CON;
public PagerAdapter(FragmentManager fm,Context con) {
    super(fm);
    CON=con;
    // TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    switch (arg0) {
        case 0:

            return new FragmentOne();
        case 1:
            return new FragmentTwo();
        case 2:
            return new FragmentThree();

        default:
            break;
    }
    return null;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 3;
}


}

Solution

  • Your button is invisible. Do you set the visibility tag to visible somewhere?

    Also the fragment might start the onCreateView method before the fragment is shown to the user, because the PagerAdapter could keep ready to be displayed, depending on your app. So you might need a setOnTabSelectedListener

    If you use an TabLayout with your PagerAdapter, you could do something like this:

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    
        tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                super.onTabSelected(tab);
                //start animation here
            }
        });