简体   繁体   中英

Fragment Slide Down Animation

i'm working on a app that displays a fragment. Once the user clicks a button on the fragment, that fragment will slide down 80% showing another fragment below. The problem i'm having is that when i click the button the fragment slides down 80% but after the screen is just re drawn with only the second fragment showing. Any help?

I have provided the code that runs when the button is clicked. and the animation files

 public void onClick(View v) {
    // TODO Auto-generated method stub

    switch(v.getId())
    {
        case R.id.button1:

            ProfileFragment pro = new ProfileFragment();


    ((MainActivity) getActivity()).tra =((MainActivity)     ()).fragmentManager.beginTransaction();                         

             ((MainActivity) getActivity()).mFragmentStack.add(pro.toString());
   getActivity()).tra.setCustomAnimations(R.anim.slide_in_top, R.anim.slide_out_bottom);
             ((MainActivity) getActivity()).tra.replace(R.id.fragment_swap,pro);

             ((MainActivity) getActivity()).tra.addToBackStack(pro.toString());
             ((MainActivity) getActivity()).tra.commit();

            break;

    }

}

Slide_out_bottom:

     <?xml version="1.0" encoding="utf-8"?>
         <set xmlns:android="http://schemas.android.com/apk/res/android" >

            <translate
               android:duration="900"
               android:fromYDelta="0%"
               android:toYDelta="80%"
            />


      </set>

slide_in_top:

   <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
    android:duration="900"
    android:fromYDelta="0%"
    android:toYDelta="0%" />
</set>

It looks to me like your slide_in_top animation file has an error. You're going from 0% to 0%. Or, in other words, don't move at all. If you don't want an animation, you can just specify 0 on the setCustomAnimations() call like so:

setCustomAnimations(0, R.anim.slide_out_bottom);

Also, for the love of sanity, please don't keep casting the getActivity to MainActivity. Instead do it like this:

ProfileFragment pro = new ProfileFragment();

MainActivity main = (MainActivity) getActivity();

main.mFragmentStack.add(pro.toString());

main.fragmentManager.beginTransaction()
    .setCustomAnimations(R.anim.slide_in_top, R.anim.slide_out_bottom)
    .replace(R.id.fragment_swap,pro)
    .addToBackStack(pro.toString())
    .commit();

You might also want to reconsider using a public field variable on the MainActivity for storing the transaction. It isn't good encapsulation and in this case it looks like it's unnecessary. Certainly, mFragmentStack should not be directly accessible by being public. One meaning of the prefix m is that it's private.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM