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