1 /* 2 * Copyright (C) 2017 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.autofill.app; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 import android.support.annotation.Nullable; 22 import android.support.design.widget.TabLayout; 23 import android.support.v4.app.Fragment; 24 import android.support.v4.app.FragmentManager; 25 import android.support.v4.app.FragmentPagerAdapter; 26 import android.support.v4.view.PagerAdapter; 27 import android.support.v4.view.ViewPager; 28 import android.support.v7.app.AppCompatActivity; 29 30 import com.example.android.autofill.app.commoncases.CommonCasesFragment; 31 import com.example.android.autofill.app.edgecases.EdgeCasesFragment; 32 33 /** 34 * This is used to launch sample activities that showcase autofill. 35 */ 36 public class MainActivity extends AppCompatActivity { 37 38 @Override 39 protected void onCreate(@Nullable Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.activity_main); 42 ViewPager viewPager = findViewById(R.id.pager); 43 PagerAdapter pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), this); 44 viewPager.setAdapter(pagerAdapter); 45 TabLayout tabLayout = findViewById(R.id.sliding_tabs); 46 tabLayout.setupWithViewPager(viewPager); 47 } 48 49 /** 50 * A simple pager adapter that holds 2 Fragments. 51 */ 52 private static class ScreenSlidePagerAdapter extends FragmentPagerAdapter { 53 private BaseMainFragment[] fragments = new BaseMainFragment[]{new CommonCasesFragment(), 54 new EdgeCasesFragment()}; 55 56 private Context mContext; 57 58 public ScreenSlidePagerAdapter(FragmentManager fm, Context context) { 59 super(fm); 60 mContext = context; 61 } 62 63 @Override 64 public Fragment getItem(int position) { 65 return fragments[position]; 66 } 67 68 @Override 69 public int getCount() { 70 return fragments.length; 71 } 72 73 @Override 74 public CharSequence getPageTitle(int position) { 75 return mContext.getString(fragments[position].getPageTitleResId()); 76 } 77 } 78 }