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