FragmentStatePagerSupport.java revision 4557342eeab7018e2edece1d3265819737d078fc
1/*
2 * Copyright (C) 2011 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
17package com.example.android.supportv13.app;
18
19import com.example.android.supportv13.Cheeses;
20import com.example.android.supportv13.R;
21
22import android.support.v13.app.FragmentStatePagerAdapter;
23import android.support.v13.view.ViewPager;
24
25import android.os.Bundle;
26import android.app.Activity;
27import android.app.Fragment;
28import android.app.FragmentManager;
29import android.app.ListFragment;
30import android.util.Log;
31import android.view.LayoutInflater;
32import android.view.View;
33import android.view.ViewGroup;
34import android.view.View.OnClickListener;
35import android.widget.ArrayAdapter;
36import android.widget.Button;
37import android.widget.ListView;
38import android.widget.TextView;
39
40public class FragmentStatePagerSupport extends Activity {
41    static final int NUM_ITEMS = 10;
42
43    MyAdapter mAdapter;
44
45    ViewPager mPager;
46    int mCurPos;
47
48    @Override
49    protected void onCreate(Bundle savedInstanceState) {
50        super.onCreate(savedInstanceState);
51        setContentView(R.layout.fragment_pager);
52
53        mAdapter = new MyAdapter(getFragmentManager());
54
55        mPager = (ViewPager)findViewById(R.id.pager);
56        mPager.setAdapter(mAdapter);
57
58        // Watch for button clicks.
59        Button button = (Button)findViewById(R.id.new_fragment);
60        button.setOnClickListener(new OnClickListener() {
61            public void onClick(View v) {
62                mCurPos++;
63                if (mCurPos < NUM_ITEMS) {
64                    mPager.setCurrentItem(mCurPos);
65                } else {
66                    mCurPos--;
67                }
68            }
69        });
70    }
71
72    public static class MyAdapter extends FragmentStatePagerAdapter {
73        public MyAdapter(FragmentManager fm) {
74            super(fm);
75        }
76
77        @Override
78        public int getCount() {
79            return NUM_ITEMS;
80        }
81
82        @Override
83        public Fragment getItem(int position) {
84            return ArrayListFragment.newInstance(position);
85        }
86    }
87
88    public static class ArrayListFragment extends ListFragment {
89        int mNum;
90
91        /**
92         * Create a new instance of CountingFragment, providing "num"
93         * as an argument.
94         */
95        static ArrayListFragment newInstance(int num) {
96            ArrayListFragment f = new ArrayListFragment();
97
98            // Supply num input as an argument.
99            Bundle args = new Bundle();
100            args.putInt("num", num);
101            f.setArguments(args);
102
103            return f;
104        }
105
106        /**
107         * When creating, retrieve this instance's number from its arguments.
108         */
109        @Override
110        public void onCreate(Bundle savedInstanceState) {
111            super.onCreate(savedInstanceState);
112            mNum = getArguments() != null ? getArguments().getInt("num") : 1;
113        }
114
115        /**
116         * The Fragment's UI is just a simple text view showing its
117         * instance number.
118         */
119        @Override
120        public View onCreateView(LayoutInflater inflater, ViewGroup container,
121                Bundle savedInstanceState) {
122            View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
123            View tv = v.findViewById(R.id.text);
124            ((TextView)tv).setText("Fragment #" + mNum);
125            return v;
126        }
127
128        @Override
129        public void onActivityCreated(Bundle savedInstanceState) {
130            super.onActivityCreated(savedInstanceState);
131            setListAdapter(new ArrayAdapter<String>(getActivity(),
132                    android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings));
133        }
134
135        @Override
136        public void onListItemClick(ListView l, View v, int position, long id) {
137            Log.i("FragmentList", "Item clicked: " + id);
138        }
139    }
140}
141