Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2011 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.app;
     18 
     19 import android.os.Bundle;
     20 import android.util.Log;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.view.View.OnClickListener;
     24 import android.view.ViewGroup;
     25 import android.widget.ArrayAdapter;
     26 import android.widget.Button;
     27 import android.widget.ListView;
     28 import android.widget.TextView;
     29 
     30 import androidx.fragment.app.Fragment;
     31 import androidx.fragment.app.FragmentActivity;
     32 import androidx.fragment.app.FragmentManager;
     33 import androidx.fragment.app.FragmentStatePagerAdapter;
     34 import androidx.fragment.app.ListFragment;
     35 import androidx.viewpager.widget.ViewPager;
     36 
     37 import com.example.android.supportv4.Cheeses;
     38 import com.example.android.supportv4.R;
     39 
     40 //BEGIN_INCLUDE(complete)
     41 public class FragmentStatePagerSupport extends FragmentActivity {
     42     static final int NUM_ITEMS = 10;
     43 
     44     MyAdapter mAdapter;
     45 
     46     ViewPager mPager;
     47 
     48     @Override
     49     protected void onCreate(Bundle savedInstanceState) {
     50         super.onCreate(savedInstanceState);
     51         setContentView(R.layout.fragment_pager);
     52 
     53         mAdapter = new MyAdapter(getSupportFragmentManager());
     54 
     55         mPager = (ViewPager)findViewById(R.id.pager);
     56         mPager.setAdapter(mAdapter);
     57 
     58         // Watch for button clicks.
     59         Button button = (Button)findViewById(R.id.goto_first);
     60         button.setOnClickListener(new OnClickListener() {
     61             @Override
     62             public void onClick(View v) {
     63                 mPager.setCurrentItem(0);
     64             }
     65         });
     66         button = (Button)findViewById(R.id.goto_last);
     67         button.setOnClickListener(new OnClickListener() {
     68             @Override
     69             public void onClick(View v) {
     70                 mPager.setCurrentItem(NUM_ITEMS-1);
     71             }
     72         });
     73     }
     74 
     75     public static class MyAdapter extends FragmentStatePagerAdapter {
     76         public MyAdapter(FragmentManager fm) {
     77             super(fm);
     78         }
     79 
     80         @Override
     81         public int getCount() {
     82             return NUM_ITEMS;
     83         }
     84 
     85         @Override
     86         public Fragment getItem(int position) {
     87             return ArrayListFragment.newInstance(position);
     88         }
     89     }
     90 
     91     public static class ArrayListFragment extends ListFragment {
     92         int mNum;
     93 
     94         /**
     95          * Create a new instance of CountingFragment, providing "num"
     96          * as an argument.
     97          */
     98         static ArrayListFragment newInstance(int num) {
     99             ArrayListFragment f = new ArrayListFragment();
    100 
    101             // Supply num input as an argument.
    102             Bundle args = new Bundle();
    103             args.putInt("num", num);
    104             f.setArguments(args);
    105 
    106             return f;
    107         }
    108 
    109         /**
    110          * When creating, retrieve this instance's number from its arguments.
    111          */
    112         @Override
    113         public void onCreate(Bundle savedInstanceState) {
    114             super.onCreate(savedInstanceState);
    115             mNum = getArguments() != null ? getArguments().getInt("num") : 1;
    116         }
    117 
    118         /**
    119          * The Fragment's UI is just a simple text view showing its
    120          * instance number.
    121          */
    122         @Override
    123         public View onCreateView(LayoutInflater inflater, ViewGroup container,
    124                 Bundle savedInstanceState) {
    125             View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
    126             View tv = v.findViewById(R.id.text);
    127             ((TextView)tv).setText("Fragment #" + mNum);
    128             return v;
    129         }
    130 
    131         @Override
    132         public void onActivityCreated(Bundle savedInstanceState) {
    133             super.onActivityCreated(savedInstanceState);
    134             setListAdapter(new ArrayAdapter<String>(getActivity(),
    135                     android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings));
    136         }
    137 
    138         @Override
    139         public void onListItemClick(ListView l, View v, int position, long id) {
    140             Log.i("FragmentList", "Item clicked: " + id);
    141         }
    142     }
    143 }
    144 //END_INCLUDE(complete)
    145