org.floppp.observerpattern_activityfragments.views.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for org.floppp.observerpattern_activityfragments.views.MainActivity.java

Source

/*
 *  The MIT License (MIT)
 * Copyright (c) 2015 Fernando Lpez
 *
 * 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 org.floppp.observerpattern_activityfragments.views;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

import org.floppp.observerpattern_activityfragments.R;
import org.floppp.observerpattern_activityfragments.observer_pattern.Observer;
import org.floppp.observerpattern_activityfragments.presenter.ActivityPresenter;

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

public class MainActivity extends AppCompatActivity implements OnFragmentInteractionListener {
    private ActivityPresenter mPresenter;
    private final String TAB_TITLES[] = new String[] { "GENERAL", "GOWEX", "BANCAJA" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpTabLayout(setUpViewPager());
        mPresenter = new ActivityPresenter(this);
        mPresenter.onCreate();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private List<CustomFragment> getFragments() {
        List<CustomFragment> aux = new ArrayList<>();
        aux.add(FragmentA.newInstance());
        aux.add(FragmentB.newInstance());
        aux.add(FragmentC.newInstance());

        return aux;
    }

    @Override
    public void onFragmentInteraction(Observer observer) {
        mPresenter.registerObserver(observer);
    }

    private ViewPager setUpViewPager() {
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager_activity);
        viewPager.setOffscreenPageLimit(TAB_TITLES.length);
        viewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), getFragments()));

        return viewPager;
    }

    private void setUpTabLayout(final ViewPager viewPager) {
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText(TAB_TITLES[0]));
        tabLayout.addTab(tabLayout.newTab().setText(TAB_TITLES[1]));
        tabLayout.addTab(tabLayout.newTab().setText(TAB_TITLES[2]));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        // Listener that allows tab change on flip.
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        // Listener to allow flip on tab clicked.
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }
}