我的 Android 应用程序中包含一个自定义后退按钮。单击片段时我的自定义动画没问题,但似乎在单击我自己的后退按钮甚至android的后退按钮时我无法应用动画。我不知道如何修复它,我已经在我的活动类中包含了这些代码,但它似乎不起作用。
@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);
}
我不知道如何实现这个......请帮助我完成这个,因为我们已经没有时间了。谢谢
等待积极的回应。
编辑
好的,这就是我设置片段的方式
在我的主要活动中:
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();
}
}