-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathMainActivity.java
114 lines (95 loc) · 4.44 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.alexstyl.searchtransition.mainscreen;
import android.content.Intent;
import android.os.Bundle;
import android.transition.Transition;
import android.transition.Transition.TransitionListener;
import android.transition.TransitionManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import com.alexstyl.searchtransition.boilerplate.BoilerplateActivity;
import com.alexstyl.searchtransition.R;
import com.alexstyl.searchtransition.searchscreen.SearchActivity;
import com.alexstyl.searchtransition.transition.FadeInTransition;
import com.alexstyl.searchtransition.transition.FadeOutTransition;
import com.alexstyl.searchtransition.transition.SimpleTransitionListener;
public class MainActivity extends BoilerplateActivity {
private SimpleToolbar toolbar;
private int toolbarMargin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// setup your Toolbar as you would normally would
// For simplicity, I am wrapping the styling of it into its a separate class
toolbar = (SimpleToolbar) findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
toolbarMargin = getResources().getDimensionPixelSize(R.dimen.toolbarMargin);
toolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Prepare the keyboard as soon as the user touches the Toolbar
// This will make the transition look faster
showKeyboard();
transitionToSearch();
}
});
}
private void transitionToSearch() {
// create a transition that navigates to search when complete
Transition transition = FadeOutTransition.withAction(navigateToSearchWhenDone());
// let the TransitionManager do the heavy work for us!
// all we have to do is change the attributes of the toolbar and the TransitionManager animates the changes
// in this case I am removing the bounds of the toolbar (to hide the blue padding on the screen) and
// I am hiding the contents of the Toolbar (Navigation icon, Title and Option Items)
TransitionManager.beginDelayedTransition(toolbar, transition);
FrameLayout.LayoutParams frameLP = (FrameLayout.LayoutParams) toolbar.getLayoutParams();
frameLP.setMargins(0, 0, 0, 0);
toolbar.setLayoutParams(frameLP);
toolbar.hideContent();
}
private TransitionListener navigateToSearchWhenDone() {
return new SimpleTransitionListener() {
@Override
public void onTransitionEnd(Transition transition) {
Intent intent = new Intent(MainActivity.this, SearchActivity.class);
startActivity(intent);
// we are handing the enter transitions ourselves
// this line overrides that
overridePendingTransition(0, 0);
// by this point of execution we have animated the 'expansion' of the Toolbar and hidden its contents.
// We are half way there. Continue to the SearchActivity to finish the animation
}
};
}
@Override
protected void onResume() {
super.onResume();
// when you are back from the SearchActivity animate the 'shrinking' of the Toolbar and
// fade its contents back in
fadeToolbarIn();
// in case we are not coming here from the SearchActivity the Toolbar would have been already visible
// so the above method has no effect
}
private void fadeToolbarIn() {
TransitionManager.beginDelayedTransition(toolbar, FadeInTransition.createTransition());
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) toolbar.getLayoutParams();
layoutParams.setMargins(toolbarMargin, toolbarMargin, toolbarMargin, toolbarMargin);
toolbar.showContent();
toolbar.setLayoutParams(layoutParams);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.main_action_share) {
shareDemo();
return true;
}
return super.onOptionsItemSelected(item);
}
}