1
1
package com .slidingmenu .example ;
2
2
3
+ import java .util .ArrayList ;
3
4
import java .util .List ;
4
- import java .util .Vector ;
5
5
6
6
import android .os .Bundle ;
7
7
import android .support .v4 .app .Fragment ;
8
8
import android .support .v4 .app .FragmentManager ;
9
9
import android .support .v4 .app .FragmentPagerAdapter ;
10
10
import android .support .v4 .app .FragmentTransaction ;
11
11
import android .support .v4 .view .ViewPager ;
12
- import android .view .View ;
13
12
14
13
import com .actionbarsherlock .app .ActionBar ;
15
14
import com .actionbarsherlock .app .ActionBar .Tab ;
18
17
import com .actionbarsherlock .view .MenuItem ;
19
18
import com .slidingmenu .lib .app .SlidingFragmentActivity ;
20
19
21
- public class ExampleActivity extends SlidingFragmentActivity implements TabListener {
20
+ public class ExampleActivity extends SlidingFragmentActivity {
22
21
23
22
public void onCreate (Bundle savedInstanceState ) {
24
23
super .onCreate (savedInstanceState );
24
+ // set the Above View
25
25
setContentView (R .layout .pager );
26
26
ViewPager vp = (ViewPager ) findViewById (R .id .pager );
27
- List <Fragment > fragments = new Vector <Fragment >();
28
- fragments .add (new SampleListFragment ());
29
- fragments .add (new SampleListFragment ());
30
- fragments .add (new SampleListFragment ());
31
- vp .setAdapter (new PagerAdapter (super .getSupportFragmentManager (), fragments ));
27
+ PagerAdapter adapter = new PagerAdapter (getSupportFragmentManager (),
28
+ vp , getSupportActionBar ());
29
+ for (int i = 0 ; i < 3 ; i ++) {
30
+ adapter .addTab (new SampleListFragment ());
31
+ }
32
+
33
+ // set the Behind View
32
34
setBehindContentView (R .layout .frame );
33
-
34
35
FragmentTransaction t = this .getSupportFragmentManager ().beginTransaction ();
35
36
t .add (R .id .frame , new SampleListFragment ());
36
37
t .commit ();
37
38
39
+ // customize the SlidingMenu
40
+ getSlidingMenu ().setShadowWidthRes (R .dimen .shadow_width );
41
+ getSlidingMenu ().setShadowDrawable (R .drawable .shadow );
38
42
getSlidingMenu ().setBehindOffsetRes (R .dimen .actionbar_home_width );
39
43
getSlidingMenu ().setBehindScrollScale (0.5f );
40
- // getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
44
+
45
+ // customize the ActionBar
41
46
ActionBar actionBar = getSupportActionBar ();
42
47
actionBar .setNavigationMode (ActionBar .NAVIGATION_MODE_TABS );
43
48
actionBar .setDisplayHomeAsUpEnabled (true );
44
- for (int i = 0 ; i < 3 ; i ++) {
45
- Tab tab = actionBar .newTab ();
46
- tab .setText ("Tab " + i );
47
- tab .setTabListener (this );
48
- actionBar .addTab (tab );
49
- }
50
- View v = this .getWindow ().getDecorView ();
51
- View a = findViewById (android .R .id .home );
52
49
}
53
- public class PagerAdapter extends FragmentPagerAdapter {
54
-
55
- private List <Fragment > fragments ;
56
- /**
57
- * @param fm
58
- * @param fragments
59
- */
60
- public PagerAdapter (FragmentManager fm , List <Fragment > fragments ) {
50
+
51
+ public class PagerAdapter extends FragmentPagerAdapter implements
52
+ ViewPager .OnPageChangeListener , TabListener {
53
+
54
+ private List <Fragment > mFragments = new ArrayList <Fragment >();
55
+ private ViewPager mPager ;
56
+ private ActionBar mActionBar ;
57
+
58
+ public PagerAdapter (FragmentManager fm , ViewPager vp , ActionBar ab ) {
61
59
super (fm );
62
- this .fragments = fragments ;
60
+ mPager = vp ;
61
+ mPager .setAdapter (this );
62
+ mPager .setOnPageChangeListener (this );
63
+ mActionBar = ab ;
64
+ }
65
+
66
+ public void addTab (Fragment frag ) {
67
+ mFragments .add (frag );
68
+ mActionBar .addTab (mActionBar .newTab ().setTabListener (this ).
69
+ setText ("Tab " +mFragments .size ()));
63
70
}
64
- /* (non-Javadoc)
65
- * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
66
- */
71
+
67
72
@ Override
68
73
public Fragment getItem (int position ) {
69
- return this . fragments .get (position );
74
+ return mFragments .get (position );
70
75
}
71
76
72
- /* (non-Javadoc)
73
- * @see android.support.v4.view.PagerAdapter#getCount()
74
- */
75
77
@ Override
76
78
public int getCount () {
77
- return this .fragments .size ();
79
+ return mFragments .size ();
80
+ }
81
+
82
+ @ Override
83
+ public void onTabSelected (Tab tab , FragmentTransaction ft ) {
84
+ mPager .setCurrentItem (tab .getPosition ());
85
+ }
86
+
87
+ public void onTabUnselected (Tab tab , FragmentTransaction ft ) { }
88
+ public void onTabReselected (Tab tab , FragmentTransaction ft ) { }
89
+ public void onPageScrollStateChanged (int arg0 ) { }
90
+ public void onPageScrolled (int arg0 , float arg1 , int arg2 ) { }
91
+
92
+ @ Override
93
+ public void onPageSelected (int position ) {
94
+ mActionBar .setSelectedNavigationItem (position );
78
95
}
79
96
}
80
97
@@ -93,30 +110,4 @@ public boolean onCreateOptionsMenu(Menu menu) {
93
110
return true ;
94
111
}
95
112
96
- public void onTabSelected (Tab tab , FragmentTransaction ft ) {
97
- // switch (tab.getPosition()) {
98
- // case 0:
99
- // findViewById(R.id.main).setBackgroundResource(android.R.color.white);
100
- // break;
101
- // case 1:
102
- // findViewById(R.id.main).setBackgroundResource(android.R.color.black);
103
- // break;
104
- // case 2:
105
- // findViewById(R.id.main).setBackgroundResource(android.R.color.darker_gray);
106
- // break;
107
- // }
108
- }
109
-
110
- public void onTabUnselected (Tab tab , FragmentTransaction ft ) {
111
- // TODO Auto-generated method stub
112
-
113
- }
114
-
115
- public void onTabReselected (Tab tab , FragmentTransaction ft ) {
116
- // TODO Auto-generated method stub
117
-
118
- }
119
-
120
-
121
-
122
113
}
0 commit comments