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