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.example.android.slidingtabscolors;
18
19
import com.example.android.common.view.SlidingTabLayout;
20
21
import android.graphics.Color;
22
import android.os.Bundle;
23
import android.support.v4.app.Fragment;
24
import android.support.v4.app.FragmentManager;
25
import android.support.v4.app.FragmentPagerAdapter;
26
import android.support.v4.view.ViewPager;
27
import android.view.LayoutInflater;
28
import android.view.View;
29
import android.view.ViewGroup;
30
31
import java.util.ArrayList;
32
import java.util.List;
33
34
/**
35
* A basic sample which shows how to use {@link com.example.android.common.view.SlidingTabLayout}
36
* to display a custom {@link ViewPager} title strip which gives continuous feedback to the user
37
* when scrolling.
38
*/
39
public class SlidingTabsColorsFragment extends Fragment {
40
41
/**
42
* This class represents a tab to be displayed by {@link ViewPager} and it's associated
43
* {@link SlidingTabLayout}.
44
*/
45
static class SamplePagerItem {
46
private final CharSequence mTitle;
47
private final int mIndicatorColor;
48
private final int mDividerColor;
49
50
SamplePagerItem(CharSequence title, int indicatorColor, int dividerColor) {
51
mTitle = title;
52
mIndicatorColor = indicatorColor;
53
mDividerColor = dividerColor;
54
}
55
56
/**
57
* @return A new {@link Fragment} to be displayed by a {@link ViewPager}
58
*/
59
Fragment createFragment() {
60
return ContentFragment.newInstance(mTitle, mIndicatorColor, mDividerColor);
61
}
62
63
/**
64
* @return the title which represents this tab. In this sample this is used directly by
65
* {@link android.support.v4.view.PagerAdapter#getPageTitle(int)}
66
*/
67
CharSequence getTitle() {
68
return mTitle;
69
}
70
71
/**
72
* @return the color to be used for indicator on the {@link SlidingTabLayout}
73
*/
74
int getIndicatorColor() {
75
return mIndicatorColor;
76
}
77
78
/**
79
* @return the color to be used for right divider on the {@link SlidingTabLayout}
80
*/
81
int getDividerColor() {
82
return mDividerColor;
83
}
84
}
85
86
static final String LOG_TAG = "SlidingTabsColorsFragment";
87
88
/**
89
* A custom {@link ViewPager} title strip which looks much like Tabs present in Android v4.0 and
90
* above, but is designed to give continuous feedback to the user when scrolling.
91
*/
92
private SlidingTabLayout mSlidingTabLayout;
93
94
/**
95
* A {@link ViewPager} which will be used in conjunction with the {@link SlidingTabLayout} above.
96
*/
97
private ViewPager mViewPager;
98
99
/**
100
* List of {@link SamplePagerItem} which represent this sample's tabs.
101
*/
102
private List<SamplePagerItem> mTabs = new ArrayList<SamplePagerItem>();
103
104
@Override
105
public void onCreate(Bundle savedInstanceState) {
106
super.onCreate(savedInstanceState);
107
109
/**
110
* Populate our tab list with tabs. Each item contains a title, indicator color and divider
111
* color, which are used by {@link SlidingTabLayout}.
112
*/
113
mTabs.add(new SamplePagerItem(
114
getString(R.string.tab_stream), // Title
115
Color.BLUE, // Indicator color
116
Color.GRAY // Divider color
117
));
118
119
mTabs.add(new SamplePagerItem(
120
getString(R.string.tab_messages), // Title
121
Color.RED, // Indicator color
122
Color.GRAY // Divider color
123
));
124
125
mTabs.add(new SamplePagerItem(
126
getString(R.string.tab_photos), // Title
127
Color.YELLOW, // Indicator color
128
Color.GRAY // Divider color
129
));
130
131
mTabs.add(new SamplePagerItem(
132
getString(R.string.tab_notifications), // Title
133
Color.GREEN, // Indicator color
134
Color.GRAY // Divider color
135
));
137
}
138
139
/**
140
* Inflates the {@link View} which will be displayed by this {@link Fragment}, from the app's
141
* resources.
142
*/
143
@Override
144
public View onCreateView(LayoutInflater inflater, ViewGroup container,
145
Bundle savedInstanceState) {
146
return inflater.inflate(R.layout.fragment_sample, container, false);
147
}
148
150
/**
151
* This is called after the {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} has finished.
152
* Here we can pick out the {@link View}s we need to configure from the content view.
153
*
154
* We set the {@link ViewPager}'s adapter to be an instance of
155
* {@link SampleFragmentPagerAdapter}. The {@link SlidingTabLayout} is then given the
156
* {@link ViewPager} so that it can populate itself.
157
*
158
* @param view View created in {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)}
159
*/
160
@Override
161
public void onViewCreated(View view, Bundle savedInstanceState) {
163
// Get the ViewPager and set it's PagerAdapter so that it can display items
164
mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
165
mViewPager.setAdapter(new SampleFragmentPagerAdapter(getChildFragmentManager()));
167
169
// Give the SlidingTabLayout the ViewPager, this must be done AFTER the ViewPager has had
170
// it's PagerAdapter set.
171
mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
172
mSlidingTabLayout.setViewPager(mViewPager);
173
175
// Set a TabColorizer to customize the indicator and divider colors. Here we just retrieve
176
// the tab at the position, and return it's set color
177
mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
178
179
@Override
180
public int getIndicatorColor(int position) {
181
return mTabs.get(position).getIndicatorColor();
182
}
183
184
@Override
185
public int getDividerColor(int position) {
186
return mTabs.get(position).getDividerColor();
187
}
188
189
});
192
}
194
195
/**
196
* The {@link FragmentPagerAdapter} used to display pages in this sample. The individual pages
197
* are instances of {@link ContentFragment} which just display three lines of text. Each page is
198
* created by the relevant {@link SamplePagerItem} for the requested position.
199
* <p>
200
* The important section of this class is the {@link #getPageTitle(int)} method which controls
201
* what is displayed in the {@link SlidingTabLayout}.
202
*/
203
class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
204
205
SampleFragmentPagerAdapter(FragmentManager fm) {
206
super(fm);
207
}
208
209
/**
210
* Return the {@link android.support.v4.app.Fragment} to be displayed at {@code position}.
211
* <p>
212
* Here we return the value returned from {@link SamplePagerItem#createFragment()}.
213
*/
214
@Override
215
public Fragment getItem(int i) {
216
return mTabs.get(i).createFragment();
217
}
218
219
@Override
220
public int getCount() {
221
return mTabs.size();
222
}
223
225
/**
226
* Return the title of the item at {@code position}. This is important as what this method
227
* returns is what is displayed in the {@link SlidingTabLayout}.
228
* <p>
229
* Here we return the value returned from {@link SamplePagerItem#getTitle()}.
230
*/
231
@Override
232
public CharSequence getPageTitle(int position) {
233
return mTabs.get(position).getTitle();
234
}
236
237
}
238
239
}