/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.supportv4.app;

import com.example.android.supportv4.R;
import com.example.android.supportv4.Shakespeare;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class FragmentPagerSupport extends FragmentActivity
        implements FragmentPager.Adapter {
    static final int NUM_ITEMS = 10;

    FragmentPager mPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_pager_support);

        mPager = (FragmentPager)findViewById(R.id.pager);
        mPager.setAdapter(this);

        Button button;
        
        // Watch for button clicks.
        button = (Button)findViewById(R.id.goto_first);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mPager.setCurrentItem(0);
            }
        });
        button = (Button)findViewById(R.id.goto_last);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mPager.setCurrentItem(NUM_ITEMS-1);
            }
        });
    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
        return ArrayListFragment.newInstance(position);
    }

    public static class ArrayListFragment extends ListFragment {
        int mNum;

        /**
         * Create a new instance of CountingFragment, providing "num"
         * as an argument.
         */
        static ArrayListFragment newInstance(int num) {
            ArrayListFragment f = new ArrayListFragment();

            // Supply num input as an argument.
            Bundle args = new Bundle();
            args.putInt("num", num);
            f.setArguments(args);

            return f;
        }

        /**
         * When creating, retrieve this instance's number from its arguments.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mNum = getArguments() != null ? getArguments().getInt("num") : 1;
        }

        /**
         * The Fragment's UI is just a simple text view showing its
         * instance number.
         */
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_pager_support_list, container, false);
            View tv = v.findViewById(R.id.text);
            ((TextView)tv).setText("Fragment #" + mNum);
            return v;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setListAdapter(new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, Shakespeare.TITLES));
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            Log.i("FragmentList", "Item clicked: " + id);
        }
    }
}

@@ -1459,16 +1459,33 @@
  mPager;
+%0A    int mCurPos;
 %0A%0A    @O
@@ -1639,24 +1639,16 @@
 nt_pager
-_support
 );%0A%0A    
@@ -1739,40 +1739,8 @@
 );%0A%0A
-        Button button;%0A        %0A
     
@@ -1771,32 +1771,39 @@
 clicks.%0A        
+Button 
 button = (Button
@@ -1825,17 +1825,19 @@
 .id.
-goto_firs
+new_fragmen
 t);%0A
@@ -1957,31 +1957,16 @@
    m
-Pager.setCurrentItem(0)
+CurPos++
 ;%0A  
@@ -1978,229 +1978,165 @@
     
- %7D%0A
      
-   %7D);%0A        button = (Button)findViewById(R.id.goto_last);%0A        button.setOnClickListener(new OnClickListener() %7B%0A            public void onClick(View v) %7B%0A                mPager.setCurrentItem(NUM_ITEMS-1);
+if (mCurPos %3C NUM_ITEMS) %7B%0A                    mPager.setCurrentItem(mCurPos);%0A                %7D else %7B%0A                    mCurPos--;%0A                %7D
 %0A   
@@ -3518,16 +3518,8 @@
 ger_
-support_
 list

Fix build.