com.adkdevelopment.earthquakesurvival.ui.adapters.PagerAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.adkdevelopment.earthquakesurvival.ui.adapters.PagerAdapter.java

Source

/*
 * MIT License
 *
 * Copyright (c) 2016. Dmytro Karataiev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.adkdevelopment.earthquakesurvival.ui.adapters;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

import com.adkdevelopment.earthquakesurvival.ui.MapviewFragment;
import com.adkdevelopment.earthquakesurvival.ui.NewsFragment;
import com.adkdevelopment.earthquakesurvival.ui.SurvivalFragment;
import com.adkdevelopment.earthquakesurvival.R;
import com.adkdevelopment.earthquakesurvival.ui.RecentFragment;

import java.lang.ref.WeakReference;

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

    private Context mContext;
    SparseArray<WeakReference<Fragment>> registeredFragments = new SparseArray<>();

    public PagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        mContext = context;
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a SurvivalFragment (defined as a static inner class below).
        switch (position) {
        case 0:
            return RecentFragment.newInstance(position + 1);
        case 1:
            return MapviewFragment.newInstance(position + 1);
        case 2:
            return NewsFragment.newInstance(position + 1);
        case 3:
            return SurvivalFragment.newInstance(position + 1);
        default:
            return null;
        }
    }

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

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position) {
        case 0:
            return mContext.getString(R.string.title_tab_recent);
        case 1:
            return mContext.getString(R.string.title_tab_maps);
        case 2:
            return mContext.getString(R.string.title_tab_news);
        case 3:
            return mContext.getString(R.string.title_tab_info);
        default:
            return "";
        }
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, new WeakReference<>(fragment));
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    public Fragment getRegisteredFragment(int position) {
        if (position == 1) {
            return getItem(position);
        } else {
            if (registeredFragments.valueAt(position) != null) {
                return registeredFragments.valueAt(position).get();
            } else {
                return getItem(position);
            }
        }
    }

}