Skip to content

Commit f90a45a

Browse files
committed
Added SliddingTabs implementation by Google
1 parent 3eb9b82 commit f90a45a

File tree

8 files changed

+814
-1
lines changed

8 files changed

+814
-1
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,31 @@
77
android:icon="@drawable/ic_launcher"
88
android:label=""
99
android:theme="@style/AppTheme" >
10+
1011
<activity
1112
android:name=".FirstActivity"
1213
android:label="" >
13-
1414
</activity>
15+
1516
<activity
1617
android:name=".SecondActivity"
1718
android:label="@string/title_activity_my_activity2" >
1819
</activity>
20+
1921
<activity
2022
android:name=".NavDrawerActivity"
2123
android:label="@string/title_activity_nav_drawer" >
24+
</activity>
25+
26+
<activity android:name=".slidingtabs.MainActivity">
2227

2328
<intent-filter>
2429
<action android:name="android.intent.action.MAIN" />
2530
<category android:name="android.intent.category.LAUNCHER" />
2631
</intent-filter>
32+
2733
</activity>
34+
2835
</application>
2936

3037
</manifest>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2013 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.saulmm.material.slidingtabs;
18+
19+
import android.app.Activity;
20+
import android.app.FragmentTransaction;
21+
import android.os.Bundle;
22+
import com.saulmm.material.R;
23+
24+
/**
25+
* A simple launcher activity containing a summary sample description, sample log and a custom
26+
* {@link android.support.v4.app.Fragment} which can display a view.
27+
* <p>
28+
* For devices with displays with a width of 720dp or greater, the sample log is always visible,
29+
* on other devices it's visibility is controlled by an item on the Action Bar.
30+
*/
31+
public class MainActivity extends Activity {
32+
33+
public static final String TAG = "MainActivity";
34+
35+
// Whether the Log Fragment is currently shown
36+
private boolean mLogShown;
37+
38+
@Override
39+
protected void onCreate(Bundle savedInstanceState) {
40+
super.onCreate(savedInstanceState);
41+
setContentView(R.layout.activity_main);
42+
43+
if (savedInstanceState == null) {
44+
FragmentTransaction transaction = getFragmentManager().beginTransaction();
45+
SlidingTabsBasicFragment fragment = new SlidingTabsBasicFragment();
46+
transaction.replace(R.id.sample_content_fragment, fragment);
47+
transaction.commit();
48+
}
49+
}
50+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright (C) 2013 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.saulmm.material.slidingtabs;
18+
19+
20+
import android.app.Fragment;
21+
import android.os.Bundle;
22+
import android.support.v4.view.PagerAdapter;
23+
import android.support.v4.view.ViewPager;
24+
import android.util.Log;
25+
import android.view.LayoutInflater;
26+
import android.view.View;
27+
import android.view.ViewGroup;
28+
import android.widget.TextView;
29+
30+
import com.saulmm.material.R;
31+
import com.saulmm.material.slidingtabs.views.SlidingTabLayout;
32+
33+
34+
public class SlidingTabsBasicFragment extends Fragment {
35+
36+
static final String LOG_TAG = "SlidingTabsBasicFragment";
37+
38+
/**
39+
* A custom {@link ViewPager} title strip which looks much like Tabs present in Android v4.0 and
40+
* above, but is designed to give continuous feedback to the user when scrolling.
41+
*/
42+
private SlidingTabLayout mSlidingTabLayout;
43+
44+
/**
45+
* A {@link ViewPager} which will be used in conjunction with the {@link SlidingTabLayout} above.
46+
*/
47+
private ViewPager mViewPager;
48+
49+
/**
50+
* Inflates the {@link View} which will be displayed by this {@link Fragment}, from the app's
51+
* resources.
52+
*/
53+
@Override
54+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
55+
Bundle savedInstanceState) {
56+
return inflater.inflate(R.layout.fragment_sample, container, false);
57+
}
58+
59+
// BEGIN_INCLUDE (fragment_onviewcreated)
60+
/**
61+
* This is called after the {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} has finished.
62+
* Here we can pick out the {@link View}s we need to configure from the content view.
63+
*
64+
* We set the {@link ViewPager}'s adapter to be an instance of {@link SamplePagerAdapter}. The
65+
* {@link SlidingTabLayout} is then given the {@link ViewPager} so that it can populate itself.
66+
*
67+
* @param view View created in {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}
68+
*/
69+
@Override
70+
public void onViewCreated(View view, Bundle savedInstanceState) {
71+
// BEGIN_INCLUDE (setup_viewpager)
72+
// Get the ViewPager and set it's PagerAdapter so that it can display items
73+
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
74+
mViewPager.setAdapter(new SamplePagerAdapter());
75+
// END_INCLUDE (setup_viewpager)
76+
77+
// BEGIN_INCLUDE (setup_slidingtablayout)
78+
// Give the SlidingTabLayout the ViewPager, this must be done AFTER the ViewPager has had
79+
// it's PagerAdapter set.
80+
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
81+
mSlidingTabLayout.setViewPager(mViewPager);
82+
// END_INCLUDE (setup_slidingtablayout)
83+
}
84+
// END_INCLUDE (fragment_onviewcreated)
85+
86+
/**
87+
* The {@link android.support.v4.view.PagerAdapter} used to display pages in this sample.
88+
* The individual pages are simple and just display two lines of text. The important section of
89+
* this class is the {@link #getPageTitle(int)} method which controls what is displayed in the
90+
* {@link SlidingTabLayout}.
91+
*/
92+
class SamplePagerAdapter extends PagerAdapter {
93+
94+
/**
95+
* @return the number of pages to display
96+
*/
97+
@Override
98+
public int getCount() {
99+
return 10;
100+
}
101+
102+
/**
103+
* @return true if the value returned from {@link #instantiateItem(ViewGroup, int)} is the
104+
* same object as the {@link View} added to the {@link ViewPager}.
105+
*/
106+
@Override
107+
public boolean isViewFromObject(View view, Object o) {
108+
return o == view;
109+
}
110+
111+
// BEGIN_INCLUDE (pageradapter_getpagetitle)
112+
/**
113+
* Return the title of the item at {@code position}. This is important as what this method
114+
* returns is what is displayed in the {@link SlidingTabLayout}.
115+
* <p>
116+
* Here we construct one using the position value, but for real application the title should
117+
* refer to the item's contents.
118+
*/
119+
@Override
120+
public CharSequence getPageTitle(int position) {
121+
return "Item " + (position + 1);
122+
}
123+
// END_INCLUDE (pageradapter_getpagetitle)
124+
125+
/**
126+
* Instantiate the {@link View} which should be displayed at {@code position}. Here we
127+
* inflate a layout from the apps resources and then change the text view to signify the position.
128+
*/
129+
@Override
130+
public Object instantiateItem(ViewGroup container, int position) {
131+
// Inflate a new layout from our resources
132+
View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,
133+
container, false);
134+
// Add the newly created View to the ViewPager
135+
container.addView(view);
136+
137+
// Retrieve a TextView from the inflated View, and update it's text
138+
TextView title = (TextView) view.findViewById(R.id.item_title);
139+
title.setText(String.valueOf(position + 1));
140+
141+
Log.i(LOG_TAG, "instantiateItem() [position: " + position + "]");
142+
143+
// Return the View
144+
return view;
145+
}
146+
147+
/**
148+
* Destroy the item from the {@link ViewPager}. In our case this is simply removing the
149+
* {@link View}.
150+
*/
151+
@Override
152+
public void destroyItem(ViewGroup container, int position, Object object) {
153+
container.removeView((View) object);
154+
Log.i(LOG_TAG, "destroyItem() [position: " + position + "]");
155+
}
156+
157+
}
158+
}

0 commit comments

Comments
 (0)