FragmentStatePagerSupport.java revision e0ecf5974e4bd81f07e38f11648896f9301c7401
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.v4.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 40//BEGIN_INCLUDE(complete) 41public class FragmentStatePagerSupport extends Activity { 42 static final int NUM_ITEMS = 10; 43 44 MyAdapter mAdapter; 45 46 ViewPager mPager; 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.goto_first); 60 button.setOnClickListener(new OnClickListener() { 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 public void onClick(View v) { 68 mPager.setCurrentItem(NUM_ITEMS-1); 69 } 70 }); 71 } 72 73 public static class MyAdapter extends FragmentStatePagerAdapter { 74 public MyAdapter(FragmentManager fm) { 75 super(fm); 76 } 77 78 @Override 79 public int getCount() { 80 return NUM_ITEMS; 81 } 82 83 @Override 84 public Fragment getItem(int position) { 85 return ArrayListFragment.newInstance(position); 86 } 87 } 88 89 public static class ArrayListFragment extends ListFragment { 90 int mNum; 91 92 /** 93 * Create a new instance of CountingFragment, providing "num" 94 * as an argument. 95 */ 96 static ArrayListFragment newInstance(int num) { 97 ArrayListFragment f = new ArrayListFragment(); 98 99 // Supply num input as an argument. 100 Bundle args = new Bundle(); 101 args.putInt("num", num); 102 f.setArguments(args); 103 104 return f; 105 } 106 107 /** 108 * When creating, retrieve this instance's number from its arguments. 109 */ 110 @Override 111 public void onCreate(Bundle savedInstanceState) { 112 super.onCreate(savedInstanceState); 113 mNum = getArguments() != null ? getArguments().getInt("num") : 1; 114 } 115 116 /** 117 * The Fragment's UI is just a simple text view showing its 118 * instance number. 119 */ 120 @Override 121 public View onCreateView(LayoutInflater inflater, ViewGroup container, 122 Bundle savedInstanceState) { 123 View v = inflater.inflate(R.layout.fragment_pager_list, container, false); 124 View tv = v.findViewById(R.id.text); 125 ((TextView)tv).setText("Fragment #" + mNum); 126 return v; 127 } 128 129 @Override 130 public void onActivityCreated(Bundle savedInstanceState) { 131 super.onActivityCreated(savedInstanceState); 132 setListAdapter(new ArrayAdapter<String>(getActivity(), 133 android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings)); 134 } 135 136 @Override 137 public void onListItemClick(ListView l, View v, int position, long id) { 138 Log.i("FragmentList", "Item clicked: " + id); 139 } 140 } 141} 142//END_INCLUDE(complete) 143