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