org.enbyted.android.zseinfo.view.section.SectionsPagerAdapter.java Source code

Java tutorial

Introduction

Here is the source code for org.enbyted.android.zseinfo.view.section.SectionsPagerAdapter.java

Source

/*
 * This file is part of ZSE Info.
 *
 *     ZSE Info is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 3 of the License, or
 *     any later version.
 *
 *     ZSE Info is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Foobar; if not, write to the Free Software
 *     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.enbyted.android.zseinfo.view.section;

import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import org.enbyted.android.zseinfo.InfoApp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bartosz Grabias on 24.02.14.
 */
public class SectionsPagerAdapter extends PagerAdapter {
    private static final String TAG = "SectionsPagerAdapter";
    private FragmentManager fragmentManager;
    private FragmentTransaction currentTransaction;

    private List<BaseSection> sections;

    public SectionsPagerAdapter(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
        this.sections = new ArrayList<>();
    }

    @Override
    public void startUpdate(ViewGroup container) {
        if (currentTransaction == null) {
            currentTransaction = fragmentManager.beginTransaction();
        }
    }

    @Override
    public BaseSection instantiateItem(ViewGroup container, int position) {
        startUpdate(container);

        String name = makeFragmentName(container.getId(), position);
        BaseSection section = (BaseSection) fragmentManager.findFragmentByTag(name);
        if (section != null && section.isValid()) {
            Log.v(TAG, "Attaching item #" + position + ": f=" + section);
            currentTransaction.attach(section);
        } else {
            section = getSection(position);
            Log.v(TAG, "Adding item #" + position + ": f=" + section);
            currentTransaction.add(container.getId(), section, makeFragmentName(container.getId(), position));
            section.validate();
        }
        return section;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        startUpdate(container);
        if (!((BaseSection) object).isValid()) {
            Log.v(TAG, "Removing item #" + position + ": f=" + object + "v=" + ((BaseSection) object).getView());
            currentTransaction.remove(((BaseSection) object));
        } else {
            Log.v(TAG, "Detaching item #" + position + ": f=" + object + "v=" + ((BaseSection) object).getView());
            currentTransaction.detach((BaseSection) object);
        }
    }

    @Override
    public void finishUpdate(ViewGroup container) {
        if (currentTransaction != null) {
            currentTransaction.commit();
            currentTransaction = null;
            fragmentManager.executePendingTransactions();
        }
    }

    private static String makeFragmentName(int viewId, int index) {
        return "enbyted:sections:" + viewId + ":" + index;
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return ((BaseSection) o).getView() == view;
    }

    //Collection stuff

    public BaseSection getSection(int position) {
        return sections.get(position);
    }

    public void addSection(BaseSection section) {
        this.sections.add(section);
        notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public void invalidate() {
        for (BaseSection section : sections) {
            section.invalidate();
            destroyItem(null, sections.indexOf(section), section);
        }
        notifyDataSetChanged();
    }

    public void removeAllSections() {
        sections.clear();
        notifyDataSetChanged();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return InfoApp.getContext().getString(getSection(position).getTitle());
    }

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