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