to top
SlidingTabsColors / src / com.example.android.slidingtabscolors /

ContentFragment.java

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 android.os.Bundle;
20
import android.support.v4.app.Fragment;
21
import android.view.LayoutInflater;
22
import android.view.View;
23
import android.view.ViewGroup;
24
import android.widget.TextView;
25
 
26
/**
27
 * Simple Fragment used to display some meaningful content for each page in the sample's
28
 * {@link android.support.v4.view.ViewPager}.
29
 */
30
public class ContentFragment extends Fragment {
31
 
32
    private static final String KEY_TITLE = "title";
33
    private static final String KEY_INDICATOR_COLOR = "indicator_color";
34
    private static final String KEY_DIVIDER_COLOR = "divider_color";
35
 
36
    /**
37
     * @return a new instance of {@link ContentFragment}, adding the parameters into a bundle and
38
     * setting them as arguments.
39
     */
40
    public static ContentFragment newInstance(CharSequence title, int indicatorColor,
41
            int dividerColor) {
42
        Bundle bundle = new Bundle();
43
        bundle.putCharSequence(KEY_TITLE, title);
44
        bundle.putInt(KEY_INDICATOR_COLOR, indicatorColor);
45
        bundle.putInt(KEY_DIVIDER_COLOR, dividerColor);
46
 
47
        ContentFragment fragment = new ContentFragment();
48
        fragment.setArguments(bundle);
49
 
50
        return fragment;
51
    }
52
 
53
    @Override
54
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
55
            Bundle savedInstanceState) {
56
        return inflater.inflate(R.layout.pager_item, container, false);
57
    }
58
 
59
    @Override
60
    public void onViewCreated(View view, Bundle savedInstanceState) {
61
        super.onViewCreated(view, savedInstanceState);
62
 
63
        Bundle args = getArguments();
64
 
65
        if (args != null) {
66
            TextView title = (TextView) view.findViewById(R.id.item_title);
67
            title.setText("Title: " + args.getCharSequence(KEY_TITLE));
68
 
69
            int indicatorColor = args.getInt(KEY_INDICATOR_COLOR);
70
            TextView indicatorColorView = (TextView) view.findViewById(R.id.item_indicator_color);
71
            indicatorColorView.setText("Indicator: #" + Integer.toHexString(indicatorColor));
72
            indicatorColorView.setTextColor(indicatorColor);
73
 
74
            int dividerColor = args.getInt(KEY_DIVIDER_COLOR);
75
            TextView dividerColorView = (TextView) view.findViewById(R.id.item_divider_color);
76
            dividerColorView.setText("Divider: #" + Integer.toHexString(dividerColor));
77
            dividerColorView.setTextColor(dividerColor);
78
        }
79
    }
80
}