com.licenta.android.licenseapp.adapter.SectionsPagerAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.licenta.android.licenseapp.adapter.SectionsPagerAdapter.java

Source

package com.licenta.android.licenseapp.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.licenta.android.licenseapp.alarm.AlarmFragment;
import com.licenta.android.licenseapp.fragment.TabFragment;
import com.licenta.android.licenseapp.fragment.UpdatableFragment;

import java.util.ArrayList;

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

    private ArrayList<Fragment> pageFragments;

    public SectionsPagerAdapter(FragmentManager fm, ArrayList<Fragment> pageFragments) {
        super(fm);

        this.pageFragments = pageFragments;
    }

    /**
     * Instantiate the fragment for the given page
     * @param position
     * @return
     */
    @Override
    public Fragment getItem(int position) {
        return pageFragments.get(position);
    }

    @Override
    public int getItemPosition(Object object) {
        if (object != null && object instanceof UpdatableFragment) {
            ((UpdatableFragment) object).update();
        }
        return super.getItemPosition(object);
    }

    @Override
    public int getCount() {
        return pageFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        try {
            return ((TabFragment) pageFragments.get(position)).getTitle();

        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return "";
    }
}