Android后退按钮和片段 - 动画将无效

时间:2012-07-09 02:36:47

标签: android android-fragments android-animation back-button

我的Android应用程序中包含自定义后退按钮。单击片段时我的自定义动画是可以的,但是当我点击我自己的后退按钮甚至是android的后退按钮时,我似乎无法应用动画。我不知道如何修复它,我已将这些代码包含在我的Activity类中,但似乎无法正常工作。

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.menuitem1:
            break;
        case R.id.menuitem2:
            super.onBackPressed();
            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
            break;

        default:
            break;
        }

        return true;
    }

我也尝试覆盖onbackPressed方法,但似乎也没有工作

   @Override
    public void onBackPressed() {
        super.onBackPressed();

        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);

    }

我不知道如何实施这一个......请帮助我解决这个问题因为我们因此而耗尽时间。谢谢

等待积极回应。

My Android Application Screencast http://content.screencast.com/users/junnel1001/folders/Jing/media/2a13fd01-f467-4889-ac21-743c188a0f67/2012-07-09_1032.png

修改

好的,这就是我设置片段的方式

在我的主要活动中:

 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


    setContentView(R.layout.main);


    ActionBar actionBar = getActionBar();
    Drawable myIcon = getResources().getDrawable( R.drawable.header_bg );
    actionBar.setSplitBackgroundDrawable(myIcon);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab tabA = actionBar.newTab().setIcon(R.drawable.about);
    ActionBar.Tab tabB = actionBar.newTab().setIcon(R.drawable.sponsors);
    ActionBar.Tab tabC = actionBar.newTab().setIcon(R.drawable.map);
    ActionBar.Tab tabD = actionBar.newTab().setIcon(R.drawable.destination);
    Fragment aboutTab = new PhotosActivity();
    Fragment sponsorTab = new SongsActivity();
    Fragment mapTab = new HCCMapActivity(); 
    Fragment DestinationTab = new DestinationActivity();
    tabA.setTabListener(new MyTabsListener(aboutTab));
    tabB.setTabListener(new MyTabsListener(sponsorTab));
    tabC.setTabListener(new MyTabsListener(mapTab));
    tabD.setTabListener(new MyTabsListener(DestinationTab));
    actionBar.addTab(tabD, 0, true);
    actionBar.addTab(tabC, 1, false);
    actionBar.addTab(tabA, 2, false);
    actionBar.addTab(tabB, 3, false);

}

protected class MyTabsListener implements TabListener{

        Fragment fragment;

        public MyTabsListener(Fragment fragment){

            this.fragment = fragment;
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {           
            Log.i("Tab Position Now", Integer.toString( tab.getPosition()));
            if (myTabPosition < 0){
                ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);    
            }else{
                if (myTabPosition >  tab.getPosition()){
                    ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);   
                    Log.i("Tab Position Before",Integer.toString( myTabPosition));
                }else{
                    ft.setCustomAnimations(R.anim.slide_out_left, R.anim.slide_out_right);      
                    Log.i("Tab Position Before1",Integer.toString( myTabPosition));
                }
            }   
            myTabPosition = tab.getPosition();
            ft.replace(R.id.fragment_place, fragment, null);    
        //  ft.commit();
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
            ft.remove(fragment);

        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }





    }

在我的一个片段中:

例如destinationActivity

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
             View view = inflater.inflate(R.layout.destination_layout, container, false);                                
             ArrayList<Category> category = new ArrayList<Category>();
             ListView lv = (ListView)view.findViewById(android.R.id.list);
             category.add(new Category("Image", "Image Banner"));
             category.add(new Category("HISTORICAL TOURS", "Historical Desc"));
             category.add(new Category("CULTURAL TOURS", "Cultural Desc"));
             lv.setAdapter(new CategoryAdapter(getActivity(), R.layout.list_view, category));
             lv.setClickable(true);
             super.setMenuVisibility (false);
             super.setHasOptionsMenu(false);
             return view;
     } 


     @Override
     public void onListItemClick(ListView l, View v, int position, long id) {
         Log.i("FragmentList", "Item clicked: " + id);
         final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
         if (id == 1){
        //   ft.remove(fragment)
             ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_in_right);   
             ft.replace(R.id.fragment_place, new ListLocationActivity(), null); 
             ft.addToBackStack(null);
             ft.commit(); 
         }else if (id == 2){
             ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_in_right);   
             ft.replace(R.id.fragment_place, new ListLocationActivity(), null); 
             ft.addToBackStack(null);
             ft.commit(); 
         }

     }

1 个答案:

答案 0 :(得分:1)

你真的需要让它覆盖后退功能吗?为什么不创建一个新的事件触发器,使其具有自己的功能而不是覆盖?因此,当“按下此按钮”“滑入/滑出”时。然后,如果你想完成一个活动,你可以放入“finish()”命令或“finishActivity()”

Android: Go back to previous activity

“如果你需要使用后退按钮回到以前的活动,你将不会使用overridePendingTransition()进行动画转换。对于只发生一次然后从历史堆栈中删除的活动,例如splash活动,overridePendingTransition ()是完美的。如果你需要动画回到之前的活动,你可以考虑其他选择。“

http://www.anddev.org/novice-tutorials-f8/splash-fade-activity-animations-overridependingtransition-t9464.html