1 /* 2 * Copyright (C) 2015 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.supportv4.view; 18 19 import android.app.Activity; 20 import android.graphics.Color; 21 import android.os.Bundle; 22 import android.support.v4.view.PagerAdapter; 23 import android.support.v4.view.PagerTitleStrip; 24 import android.support.v4.view.ViewPager; 25 import android.util.Pair; 26 import android.view.View; 27 import android.view.ViewGroup; 28 29 import com.example.android.supportv4.R; 30 31 import java.util.ArrayList; 32 33 public class ViewPagerActivity extends Activity { 34 private ViewPager mPager; 35 private PagerTitleStrip mTitles; 36 private ColorPagerAdapter mAdapter; 37 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 42 setContentView(R.layout.view_pager_layout); 43 44 mAdapter = new ColorPagerAdapter(); 45 mAdapter.add("Red", Color.RED); 46 mAdapter.add("Green", Color.GREEN); 47 mAdapter.add("Blue", Color.BLUE); 48 49 mPager = findViewById(R.id.pager); 50 mPager.setAdapter(mAdapter); 51 52 mTitles = findViewById(R.id.titles); 53 } 54 55 private static class ColorPagerAdapter extends PagerAdapter { 56 private ArrayList<Pair<String, Integer>> mEntries = new ArrayList<>(); 57 58 public void add(String title, int color) { 59 mEntries.add(new Pair<>(title, color)); 60 } 61 62 @Override 63 public int getCount() { 64 return mEntries.size(); 65 } 66 67 @Override 68 public Object instantiateItem(ViewGroup container, int position) { 69 final View view = new View(container.getContext()); 70 view.setBackgroundColor(mEntries.get(position).second); 71 72 // Unlike ListView adapters, the ViewPager adapter is responsible 73 // for adding the view to the container. 74 container.addView(view); 75 76 return new ViewHolder(view, position); 77 } 78 79 @Override 80 public void destroyItem(ViewGroup container, int position, Object object) { 81 // The adapter is also responsible for removing the view. 82 container.removeView(((ViewHolder) object).view); 83 } 84 85 @Override 86 public int getItemPosition(Object object) { 87 return ((ViewHolder) object).position; 88 } 89 90 @Override 91 public boolean isViewFromObject(View view, Object object) { 92 return ((ViewHolder) object).view == view; 93 } 94 95 @Override 96 public CharSequence getPageTitle(int position) { 97 return mEntries.get(position).first; 98 } 99 100 private static class ViewHolder { 101 final View view; 102 final int position; 103 104 public ViewHolder(View view, int position) { 105 this.view = view; 106 this.position = position; 107 } 108 } 109 } 110 } 111