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