1*90c8c64dSAndroid Build Coastguard Worker /* 2*90c8c64dSAndroid Build Coastguard Worker * Copyright (C) 2013 The Android Open Source Project 3*90c8c64dSAndroid Build Coastguard Worker * 4*90c8c64dSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*90c8c64dSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*90c8c64dSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*90c8c64dSAndroid Build Coastguard Worker * 8*90c8c64dSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*90c8c64dSAndroid Build Coastguard Worker * 10*90c8c64dSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*90c8c64dSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*90c8c64dSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*90c8c64dSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*90c8c64dSAndroid Build Coastguard Worker * limitations under the License. 15*90c8c64dSAndroid Build Coastguard Worker */ 16*90c8c64dSAndroid Build Coastguard Worker 17*90c8c64dSAndroid Build Coastguard Worker package com.example.android.slidingtabsbasic; 18*90c8c64dSAndroid Build Coastguard Worker 19*90c8c64dSAndroid Build Coastguard Worker import com.example.android.common.logger.Log; 20*90c8c64dSAndroid Build Coastguard Worker import com.example.android.common.view.SlidingTabLayout; 21*90c8c64dSAndroid Build Coastguard Worker 22*90c8c64dSAndroid Build Coastguard Worker import android.os.Bundle; 23*90c8c64dSAndroid Build Coastguard Worker import android.support.v4.app.Fragment; 24*90c8c64dSAndroid Build Coastguard Worker import android.support.v4.view.PagerAdapter; 25*90c8c64dSAndroid Build Coastguard Worker import android.support.v4.view.ViewPager; 26*90c8c64dSAndroid Build Coastguard Worker import android.view.LayoutInflater; 27*90c8c64dSAndroid Build Coastguard Worker import android.view.View; 28*90c8c64dSAndroid Build Coastguard Worker import android.view.ViewGroup; 29*90c8c64dSAndroid Build Coastguard Worker import android.widget.TextView; 30*90c8c64dSAndroid Build Coastguard Worker 31*90c8c64dSAndroid Build Coastguard Worker /** 32*90c8c64dSAndroid Build Coastguard Worker * A basic sample which shows how to use {@link com.example.android.common.view.SlidingTabLayout} 33*90c8c64dSAndroid Build Coastguard Worker * to display a custom {@link ViewPager} title strip which gives continuous feedback to the user 34*90c8c64dSAndroid Build Coastguard Worker * when scrolling. 35*90c8c64dSAndroid Build Coastguard Worker */ 36*90c8c64dSAndroid Build Coastguard Worker public class SlidingTabsBasicFragment extends Fragment { 37*90c8c64dSAndroid Build Coastguard Worker 38*90c8c64dSAndroid Build Coastguard Worker static final String LOG_TAG = "SlidingTabsBasicFragment"; 39*90c8c64dSAndroid Build Coastguard Worker 40*90c8c64dSAndroid Build Coastguard Worker /** 41*90c8c64dSAndroid Build Coastguard Worker * A custom {@link ViewPager} title strip which looks much like Tabs present in Android v4.0 and 42*90c8c64dSAndroid Build Coastguard Worker * above, but is designed to give continuous feedback to the user when scrolling. 43*90c8c64dSAndroid Build Coastguard Worker */ 44*90c8c64dSAndroid Build Coastguard Worker private SlidingTabLayout mSlidingTabLayout; 45*90c8c64dSAndroid Build Coastguard Worker 46*90c8c64dSAndroid Build Coastguard Worker /** 47*90c8c64dSAndroid Build Coastguard Worker * A {@link ViewPager} which will be used in conjunction with the {@link SlidingTabLayout} above. 48*90c8c64dSAndroid Build Coastguard Worker */ 49*90c8c64dSAndroid Build Coastguard Worker private ViewPager mViewPager; 50*90c8c64dSAndroid Build Coastguard Worker 51*90c8c64dSAndroid Build Coastguard Worker /** 52*90c8c64dSAndroid Build Coastguard Worker * Inflates the {@link View} which will be displayed by this {@link Fragment}, from the app's 53*90c8c64dSAndroid Build Coastguard Worker * resources. 54*90c8c64dSAndroid Build Coastguard Worker */ 55*90c8c64dSAndroid Build Coastguard Worker @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)56*90c8c64dSAndroid Build Coastguard Worker public View onCreateView(LayoutInflater inflater, ViewGroup container, 57*90c8c64dSAndroid Build Coastguard Worker Bundle savedInstanceState) { 58*90c8c64dSAndroid Build Coastguard Worker return inflater.inflate(R.layout.fragment_sample, container, false); 59*90c8c64dSAndroid Build Coastguard Worker } 60*90c8c64dSAndroid Build Coastguard Worker 61*90c8c64dSAndroid Build Coastguard Worker // BEGIN_INCLUDE (fragment_onviewcreated) 62*90c8c64dSAndroid Build Coastguard Worker /** 63*90c8c64dSAndroid Build Coastguard Worker * This is called after the {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} has finished. 64*90c8c64dSAndroid Build Coastguard Worker * Here we can pick out the {@link View}s we need to configure from the content view. 65*90c8c64dSAndroid Build Coastguard Worker * 66*90c8c64dSAndroid Build Coastguard Worker * We set the {@link ViewPager}'s adapter to be an instance of {@link SamplePagerAdapter}. The 67*90c8c64dSAndroid Build Coastguard Worker * {@link SlidingTabLayout} is then given the {@link ViewPager} so that it can populate itself. 68*90c8c64dSAndroid Build Coastguard Worker * 69*90c8c64dSAndroid Build Coastguard Worker * @param view View created in {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} 70*90c8c64dSAndroid Build Coastguard Worker */ 71*90c8c64dSAndroid Build Coastguard Worker @Override onViewCreated(View view, Bundle savedInstanceState)72*90c8c64dSAndroid Build Coastguard Worker public void onViewCreated(View view, Bundle savedInstanceState) { 73*90c8c64dSAndroid Build Coastguard Worker // BEGIN_INCLUDE (setup_viewpager) 74*90c8c64dSAndroid Build Coastguard Worker // Get the ViewPager and set it's PagerAdapter so that it can display items 75*90c8c64dSAndroid Build Coastguard Worker mViewPager = (ViewPager) view.findViewById(R.id.viewpager); 76*90c8c64dSAndroid Build Coastguard Worker mViewPager.setAdapter(new SamplePagerAdapter()); 77*90c8c64dSAndroid Build Coastguard Worker // END_INCLUDE (setup_viewpager) 78*90c8c64dSAndroid Build Coastguard Worker 79*90c8c64dSAndroid Build Coastguard Worker // BEGIN_INCLUDE (setup_slidingtablayout) 80*90c8c64dSAndroid Build Coastguard Worker // Give the SlidingTabLayout the ViewPager, this must be done AFTER the ViewPager has had 81*90c8c64dSAndroid Build Coastguard Worker // it's PagerAdapter set. 82*90c8c64dSAndroid Build Coastguard Worker mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs); 83*90c8c64dSAndroid Build Coastguard Worker mSlidingTabLayout.setViewPager(mViewPager); 84*90c8c64dSAndroid Build Coastguard Worker // END_INCLUDE (setup_slidingtablayout) 85*90c8c64dSAndroid Build Coastguard Worker } 86*90c8c64dSAndroid Build Coastguard Worker // END_INCLUDE (fragment_onviewcreated) 87*90c8c64dSAndroid Build Coastguard Worker 88*90c8c64dSAndroid Build Coastguard Worker /** 89*90c8c64dSAndroid Build Coastguard Worker * The {@link android.support.v4.view.PagerAdapter} used to display pages in this sample. 90*90c8c64dSAndroid Build Coastguard Worker * The individual pages are simple and just display two lines of text. The important section of 91*90c8c64dSAndroid Build Coastguard Worker * this class is the {@link #getPageTitle(int)} method which controls what is displayed in the 92*90c8c64dSAndroid Build Coastguard Worker * {@link SlidingTabLayout}. 93*90c8c64dSAndroid Build Coastguard Worker */ 94*90c8c64dSAndroid Build Coastguard Worker class SamplePagerAdapter extends PagerAdapter { 95*90c8c64dSAndroid Build Coastguard Worker 96*90c8c64dSAndroid Build Coastguard Worker /** 97*90c8c64dSAndroid Build Coastguard Worker * @return the number of pages to display 98*90c8c64dSAndroid Build Coastguard Worker */ 99*90c8c64dSAndroid Build Coastguard Worker @Override getCount()100*90c8c64dSAndroid Build Coastguard Worker public int getCount() { 101*90c8c64dSAndroid Build Coastguard Worker return 10; 102*90c8c64dSAndroid Build Coastguard Worker } 103*90c8c64dSAndroid Build Coastguard Worker 104*90c8c64dSAndroid Build Coastguard Worker /** 105*90c8c64dSAndroid Build Coastguard Worker * @return true if the value returned from {@link #instantiateItem(ViewGroup, int)} is the 106*90c8c64dSAndroid Build Coastguard Worker * same object as the {@link View} added to the {@link ViewPager}. 107*90c8c64dSAndroid Build Coastguard Worker */ 108*90c8c64dSAndroid Build Coastguard Worker @Override isViewFromObject(View view, Object o)109*90c8c64dSAndroid Build Coastguard Worker public boolean isViewFromObject(View view, Object o) { 110*90c8c64dSAndroid Build Coastguard Worker return o == view; 111*90c8c64dSAndroid Build Coastguard Worker } 112*90c8c64dSAndroid Build Coastguard Worker 113*90c8c64dSAndroid Build Coastguard Worker // BEGIN_INCLUDE (pageradapter_getpagetitle) 114*90c8c64dSAndroid Build Coastguard Worker /** 115*90c8c64dSAndroid Build Coastguard Worker * Return the title of the item at {@code position}. This is important as what this method 116*90c8c64dSAndroid Build Coastguard Worker * returns is what is displayed in the {@link SlidingTabLayout}. 117*90c8c64dSAndroid Build Coastguard Worker * <p> 118*90c8c64dSAndroid Build Coastguard Worker * Here we construct one using the position value, but for real application the title should 119*90c8c64dSAndroid Build Coastguard Worker * refer to the item's contents. 120*90c8c64dSAndroid Build Coastguard Worker */ 121*90c8c64dSAndroid Build Coastguard Worker @Override getPageTitle(int position)122*90c8c64dSAndroid Build Coastguard Worker public CharSequence getPageTitle(int position) { 123*90c8c64dSAndroid Build Coastguard Worker return "Item " + (position + 1); 124*90c8c64dSAndroid Build Coastguard Worker } 125*90c8c64dSAndroid Build Coastguard Worker // END_INCLUDE (pageradapter_getpagetitle) 126*90c8c64dSAndroid Build Coastguard Worker 127*90c8c64dSAndroid Build Coastguard Worker /** 128*90c8c64dSAndroid Build Coastguard Worker * Instantiate the {@link View} which should be displayed at {@code position}. Here we 129*90c8c64dSAndroid Build Coastguard Worker * inflate a layout from the apps resources and then change the text view to signify the position. 130*90c8c64dSAndroid Build Coastguard Worker */ 131*90c8c64dSAndroid Build Coastguard Worker @Override instantiateItem(ViewGroup container, int position)132*90c8c64dSAndroid Build Coastguard Worker public Object instantiateItem(ViewGroup container, int position) { 133*90c8c64dSAndroid Build Coastguard Worker // Inflate a new layout from our resources 134*90c8c64dSAndroid Build Coastguard Worker View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item, 135*90c8c64dSAndroid Build Coastguard Worker container, false); 136*90c8c64dSAndroid Build Coastguard Worker // Add the newly created View to the ViewPager 137*90c8c64dSAndroid Build Coastguard Worker container.addView(view); 138*90c8c64dSAndroid Build Coastguard Worker 139*90c8c64dSAndroid Build Coastguard Worker // Retrieve a TextView from the inflated View, and update it's text 140*90c8c64dSAndroid Build Coastguard Worker TextView title = (TextView) view.findViewById(R.id.item_title); 141*90c8c64dSAndroid Build Coastguard Worker title.setText(String.valueOf(position + 1)); 142*90c8c64dSAndroid Build Coastguard Worker 143*90c8c64dSAndroid Build Coastguard Worker Log.i(LOG_TAG, "instantiateItem() [position: " + position + "]"); 144*90c8c64dSAndroid Build Coastguard Worker 145*90c8c64dSAndroid Build Coastguard Worker // Return the View 146*90c8c64dSAndroid Build Coastguard Worker return view; 147*90c8c64dSAndroid Build Coastguard Worker } 148*90c8c64dSAndroid Build Coastguard Worker 149*90c8c64dSAndroid Build Coastguard Worker /** 150*90c8c64dSAndroid Build Coastguard Worker * Destroy the item from the {@link ViewPager}. In our case this is simply removing the 151*90c8c64dSAndroid Build Coastguard Worker * {@link View}. 152*90c8c64dSAndroid Build Coastguard Worker */ 153*90c8c64dSAndroid Build Coastguard Worker @Override destroyItem(ViewGroup container, int position, Object object)154*90c8c64dSAndroid Build Coastguard Worker public void destroyItem(ViewGroup container, int position, Object object) { 155*90c8c64dSAndroid Build Coastguard Worker container.removeView((View) object); 156*90c8c64dSAndroid Build Coastguard Worker Log.i(LOG_TAG, "destroyItem() [position: " + position + "]"); 157*90c8c64dSAndroid Build Coastguard Worker } 158*90c8c64dSAndroid Build Coastguard Worker 159*90c8c64dSAndroid Build Coastguard Worker } 160*90c8c64dSAndroid Build Coastguard Worker } 161