Home | History | Annotate | Download | only in app
      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 
     17 package com.example.android.supportv4.app;
     18 
     19 import com.example.android.supportv4.Cheeses;
     20 import com.example.android.supportv4.R;
     21 
     22 import android.os.Bundle;
     23 import android.support.v4.app.Fragment;
     24 import android.support.v4.app.FragmentActivity;
     25 import android.support.v4.app.FragmentManager;
     26 import android.support.v4.app.FragmentPagerAdapter;
     27 import android.support.v4.app.ListFragment;
     28 import android.support.v4.view.ViewPager;
     29 import android.util.Log;
     30 import android.view.LayoutInflater;
     31 import android.view.View;
     32 import android.view.ViewGroup;
     33 import android.view.View.OnClickListener;
     34 import android.widget.ArrayAdapter;
     35 import android.widget.Button;
     36 import android.widget.ListView;
     37 import android.widget.TextView;
     38 
     39 //BEGIN_INCLUDE(complete)
     40 public 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             public void onClick(View v) {
     61                 mPager.setCurrentItem(0);
     62             }
     63         });
     64         button = (Button)findViewById(R.id.goto_last);
     65         button.setOnClickListener(new OnClickListener() {
     66             public void onClick(View v) {
     67                 mPager.setCurrentItem(NUM_ITEMS-1);
     68             }
     69         });
     70     }
     71 
     72     public static class MyAdapter extends FragmentPagerAdapter {
     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 //END_INCLUDE(complete)
    142