当片段被打开时,我向左做动画。 现在我需要在单击后退按钮时从左到右制作动画。我尝试了不同的解决方案,但都没有奏效。求你帮帮我。
在此视频中,您可以看到有一个从右到左的动画,但没有从左到右的动画。 https://www.youtube.com/watch?v=mNjiCwLpaqY&feature=youtu.be
我的第一个片段
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
FragmentManager fm = getActivity().getSupportFragmentManager();
final FragmentTransaction transaction = fm.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.addToBackStack(null);
transaction.setCustomAnimations(R.animator.enter_from_right, R.animator.exit_to_left);
final Fragment fragment = new ProfileFragment();
Bundle bundle = new Bundle();
User user = (User)adapterView.getAdapter().getItem(i);
bundle.putString("name",user.getName());
bundle.putInt("age",user.getAge());
bundle.putString("speak",user.getSpeakingLanguage());
bundle.putString("learn",user.getLearningLanguage());
bundle.putInt("distance",user.getDistance());
fragment.setArguments(bundle);
transaction.replace(R.id.container,fragment).commit();
}
});
我的第二个片段
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
((Main2Activity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((Main2Activity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
View v = inflater.inflate(R.layout.fragment_profile, container, false);
Bundle bundle = this.getArguments();
name = v.findViewById(R.id.user_name);
speakingLanguage = v.findViewById(R.id.speaking_language);
learningLanguage = v.findViewById(R.id.learning_language);
ratingBar = v.findViewById(R.id.user_rating);
distance = v.findViewById(R.id.location);
if (bundle != null){
name.setText(bundle.getString("name"));
speakingLanguage.setText(bundle.getString("speaking"));
learningLanguage.setText(bundle.getString("learning"));
//ratingBar.setRating(bundle.getFloat("rating"));
}else {
SharedPreferences preferences = getActivity().getSharedPreferences(SessionManager.PREF_NAME, Context.MODE_PRIVATE);
name.setText(preferences.getString("name",""));
speakingLanguage.setText(preferences.getString("speaking","Speaking: Russian"));
learningLanguage.setText(preferences.getString("learning","Learning: English"));
}
return v;
}
我的MainActivity类
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
FragmentManager fm = getSupportFragmentManager();
int count = fm.getBackStackEntryCount();
FragmentTransaction ft = fm.beginTransaction();
if (count == 0) {
super.onBackPressed();
} else {
fm.popBackStack();
}
ft.setCustomAnimations(R.animator.enter_from_left, R.animator.exit_to_right);
ft.commit();
return true;
}
return super.onOptionsItemSelected(item);
}
enter_from_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500" />
</set>
exit_to_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="-100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="500"/>
</set>
答案 0 :(得分:1)
您必须使用setCustomAnimations()
的重载版本。
setCustomAnimations(int enter,int exit,int popEnter,int popExit)
示例:
transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, android.R.anim.slide_in_right, android.R.anim.slide_out_left);
编辑: - 对于幻灯片动画,您可以按照This answer跟你提到的相同内容。