Added transition while swiping left/right.

master
Tomasz Półgrabia 2016-09-17 12:47:24 +02:00
parent 1f022a048e
commit 5347f67d74
5 changed files with 67 additions and 2 deletions

View File

@ -61,6 +61,7 @@ public class MainActivity extends ActionBarActivity {
private GestureDetector.OnGestureListener swipeHandler; private GestureDetector.OnGestureListener swipeHandler;
private PanoramioImageInfo photoInfo; private PanoramioImageInfo photoInfo;
private ProgressDialog progressDlg; private ProgressDialog progressDlg;
private int oldFragmentId = 0;
public StandardLocationListener getLocationCallback() { public StandardLocationListener getLocationCallback() {
return locationCallback; return locationCallback;
@ -216,6 +217,27 @@ public class MainActivity extends ActionBarActivity {
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ctx = fragmentManager.beginTransaction(); FragmentTransaction ctx = fragmentManager.beginTransaction();
Log.v(CLASS_TAG, "old fragment id: " + oldFragmentId + ", current fragment id: " + currentFragmentId);
if (oldFragmentId != currentFragmentId) {
if (currentFragmentId < oldFragmentId) {
// slide left animation
Log.v(CLASS_TAG, "sliding left animation");
ctx.setCustomAnimations(
R.anim.slide_in_left,
R.anim.slide_out_left,
R.anim.slide_in_right,
R.anim.slide_out_right);
} else {
// slide right animation
Log.v(CLASS_TAG, "sliding right animation");
ctx.setCustomAnimations(
R.anim.slide_in_right,
R.anim.slide_out_right,
R.anim.slide_in_left,
R.anim.slide_out_left);
}
}
ctx.replace(R.id.fragments, fragment, tag); ctx.replace(R.id.fragments, fragment, tag);
ctx.addToBackStack(null); ctx.addToBackStack(null);
ctx.commit(); ctx.commit();
@ -235,12 +257,17 @@ public class MainActivity extends ActionBarActivity {
} }
public void swipeLeft() { public void swipeLeft() {
currentFragmentId = (int)Math.max(MIN_FRAGMENT_ID, currentFragmentId-1); changeCurrentFragId((int)Math.max(MIN_FRAGMENT_ID, currentFragmentId-1));
switchFragment(); switchFragment();
} }
private void changeCurrentFragId(int nextFragmentId) {
oldFragmentId = currentFragmentId;
currentFragmentId = nextFragmentId;
}
public void swipeRight() { public void swipeRight() {
currentFragmentId = (int)Math.min(MAX_FRAGMENT_ID, currentFragmentId+1); changeCurrentFragId((int)Math.min(MAX_FRAGMENT_ID, currentFragmentId+1));
switchFragment(); switchFragment();
} }
@ -350,6 +377,12 @@ public class MainActivity extends ActionBarActivity {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putSerializable(FRAG_ID, currentFragmentId); outState.putSerializable(FRAG_ID, currentFragmentId);
outState.putSerializable(PHOTO_INFO, photoInfo); outState.putSerializable(PHOTO_INFO, photoInfo);
// SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// SharedPreferences.Editor editor = sharedPrefs.edit();
// editor.putInt(FRAG_ID, currentFragmentId);
// editor.commit();
Log.v(CLASS_TAG, "2 Saving current fragment id: " + currentFragmentId); Log.v(CLASS_TAG, "2 Saving current fragment id: " + currentFragmentId);
} }

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%"
android:toXDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="1000" />
</set>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%"
android:toXDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="1000" />
</set>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%"
android:toXDelta="-100%"
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="1000" />
</set>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="1000" />
</set>