com.jaysen.mddp.AppBarLayoutActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.jaysen.mddp.AppBarLayoutActivity.java

Source

/*
 * Copyright (c) 2015. The Android Open Project of  jaysen.lin@foxmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jaysen.mddp;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.jaysen.mddp.adapter.MyViewPagerAdapter;

import java.util.ArrayList;

public class AppBarLayoutActivity extends AbsAppCompatActivity
        implements TabFragment.OnFragmentInteractionListener {
    protected ViewPager mPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_bar_layout_activitiy);
        setUpTabLayout();
        setUpDrawerLayout();
        setUpNavMenu();
        setUpViewPager();
    }

    protected void setUpViewPager() {
        mPager = (ViewPager) findViewById(R.id.pager);
        MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager());
        adapter.setFragments(getFragments());
        mPager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mPager);
    }

    private ArrayList<Fragment> getFragments() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            Fragment fragment = TabFragment.newInstance();
            fragments.add(fragment);
        }
        return fragments;
    }

    @Override
    protected void setUpDrawerLayout() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
        setSupportActionBar(toolbar);
        //        if (getSupportActionBar() != null) {
        //            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //            getSupportActionBar().setHomeButtonEnabled(true);
        //        }
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open_drawer,
                R.string.close_drawer);
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.color_primary_dark));
    }

    @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_app_bar_layout_activitiy, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

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

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        int id = menuItem.getItemId();
        switch (id) {
        case R.id.navItem1:
            break;
        case R.id.navItem2:
            break;
        case R.id.navItem3:
            break;
        }
        menuItem.setChecked(true);
        mDrawerLayout.closeDrawer(mNavigation);
        return true;
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}