1/* 2 * Copyright (C) 2014 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.google.android.apps.common.testing.ui.testapp; 18 19import android.graphics.Color; 20import android.support.v4.view.PagerAdapter; 21import android.view.LayoutInflater; 22import android.view.View; 23import android.view.ViewGroup; 24import android.widget.TextView; 25 26class SimplePagerAdapter extends PagerAdapter { 27 28 private static final int[] COLORS = { 29 Color.BLUE, 30 Color.RED, 31 Color.YELLOW, 32 }; 33 34 private static final int NUM_PAGES = COLORS.length; 35 36 @Override 37 public int getCount() { 38 return NUM_PAGES; 39 } 40 41 @Override 42 public boolean isViewFromObject(View view, Object object) { 43 return view == object; 44 } 45 46 @Override 47 public int getItemPosition(Object object) { 48 return ((ViewGroup) ((View) object).getParent()).indexOfChild((View) object); 49 } 50 51 @Override 52 public Object instantiateItem(ViewGroup container, int position) { 53 LayoutInflater inflater = LayoutInflater.from(container.getContext()); 54 View view = inflater.inflate(R.layout.pager_view, null); 55 ((TextView) view.findViewById(R.id.pager_content)).setText("Position #" + position); 56 view.setBackgroundColor(COLORS[position]); 57 container.addView(view); 58 return view; 59 } 60 61 @Override 62 public void destroyItem(ViewGroup container, int position, Object object) { 63 container.removeView((View) object); 64 } 65} 66