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