com.hardcopy.arduinonavi.views.FragmentAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.hardcopy.arduinonavi.views.FragmentAdapter.java

Source

/*
 * Copyright (C) 2014 Bluetooth Connection Template
 *
 * 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.hardcopy.arduinonavi.views;

import java.util.Locale;

import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.hardcopy.arduinonavi.R;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.Display;

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class FragmentAdapter extends FragmentPagerAdapter {

    public static final String TAG = "FragmentAdapter";

    // TODO: Total count
    public static final int FRAGMENT_COUNT = 2;

    // TODO: Fragment position
    public static final int FRAGMENT_POS_GMAP = 0;
    public static final int FRAGMENT_POS_SETTINGS = 1;

    // System
    private Context mContext = null;
    private Handler mHandler = null;
    private IFragmentListener mFragmentListener = null;

    private SupportMapFragment mGMapFragment = null;
    private Fragment mLLSettingsFragment = null;

    // Google map

    // Layout
    //----- Main Layout
    public static int mScreenW = 0;
    public static int mScreenH = 0;

    public FragmentAdapter(FragmentManager fm, Context c, IFragmentListener l, Handler h) {
        super(fm);
        mContext = c;
        mFragmentListener = l;
        mHandler = h;
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        Fragment fragment;

        if (position == FRAGMENT_POS_GMAP) {
            if (mGMapFragment == null) {
                mGMapFragment = new SupportMapFragment() {
                    @Override
                    public void onActivityCreated(Bundle savedInstanceState) {
                        // Only after fragment initialization you can get map instance.
                        // So do the map control after this line.
                        super.onActivityCreated(savedInstanceState);
                        if (this.getMap() != null) {
                            mFragmentListener.OnFragmentCallback(IFragmentListener.CALLBACK_MAP_CREATED,
                                    this.getView().getWidth(), this.getView().getHeight(), null, null,
                                    this.getMap());
                        }
                    }
                };
            }
            fragment = mGMapFragment;

        } else if (position == FRAGMENT_POS_SETTINGS) {
            if (mLLSettingsFragment == null) {
                mLLSettingsFragment = new LLSettingsFragment(mContext, mFragmentListener);
            }
            fragment = mLLSettingsFragment;

        } else {
            fragment = null;
        }

        // TODO: If you have something to notify to the fragment.
        /*
        if(needToSetArguments) {
           Bundle args = new Bundle();
           args.putInt(ARG_SECTION_NUMBER, position + 1);
           fragment.setArguments(args);
        }
        */

        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case FRAGMENT_POS_GMAP:
            return mContext.getString(R.string.title_map).toUpperCase(l);
        case FRAGMENT_POS_SETTINGS:
            return mContext.getString(R.string.title_ll_settings).toUpperCase(l);
        }
        return null;
    }

}