com.jooik.tabbeddialog.fragments.FragmentDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.jooik.tabbeddialog.fragments.FragmentDialog.java

Source

package com.jooik.tabbeddialog.fragments;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.astuetz.PagerSlidingTabStrip;

import org.thezero.qrfi.R;

/**
 * Fragment dialog displaying tab host...
 */
public class FragmentDialog extends DialogFragment {
    // ------------------------------------------------------------------------
    // members
    // ------------------------------------------------------------------------

    // ------------------------------------------------------------------------
    // public usage
    // ------------------------------------------------------------------------

    @NonNull
    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        window.setLayout(LinearLayoutCompat.LayoutParams.WRAP_CONTENT,
                getResources().getDimensionPixelSize(R.dimen.dialog_height));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dialog, container);

        // tab slider
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());

        // Set up the ViewPager with the sections adapter.
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.pager);
        viewPager.setAdapter(sectionsPagerAdapter);

        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
        tabs.setIndicatorColorResource(R.color.accent);
        tabs.setViewPager(viewPager);

        return view;
    }

    // ------------------------------------------------------------------------
    // inner classes
    // ------------------------------------------------------------------------

    /**
     * Used for tab paging...
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                // find first fragment...
                return new Fragment_Tab_1();
            } else if (position == 2) {
                // find first fragment...
                return new Fragment_Tab_2();
            } else if (position == 3) {
                // find first fragment...
                return new Fragment_Tab_3();
            } else if (position == 1) {
                // find first fragment...
                return new Fragment_Tab_12();
            }

            return null;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return getString(R.string.app_name);
            case 1:
                return "";
            case 2:
                return getString(R.string.license);
            case 3:
                return getString(R.string.me);
            }
            return null;
        }
    }
}