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