com.uoftdev.photoaday.activities.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.uoftdev.photoaday.activities.MainActivity.java

Source

/*
 * Copyright 2013 The Android Open Source Project
 *
 * 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.uoftdev.photoaday.activities;

import com.uoftdev.photoaday.R;
import com.uoftdev.photoaday.fragments.HotPostListFragment;
import com.uoftdev.photoaday.fragments.NewPostListFragment;
import com.uoftdev.photoaday.fragments.RisingPostListFragment;
import com.uoftdev.photoaday.fragments.SettingsFragment;
import com.uoftdev.photoaday.fragments.TopPostListFragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * This activity displays the currently selected view from navigation drawer.
 */
public class MainActivity extends FragmentActivity {

    /** navigation drawer */
    private DrawerLayout drawerLayout;

    /** navigation drawer option list */
    private ListView drawerList;

    /** list of option titles to display in navigation drawer */
    private String[] drawerListTitles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        drawerListTitles = getResources().getStringArray(R.array.drawer_array);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerList = (ListView) findViewById(R.id.left_drawer);

        // Set up navigation drawer's option list.
        drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawerListTitles));
        drawerList.setOnItemClickListener(new DrawerItemClickListener());

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

    /** 
     * This click listener handles option selection in navigation drawer. 
     */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    /**
     * Checks which navigation drawer item was selected and launches that view.
     * 
     * @param position
     *            position of option selected in navigation drawer.           
     */
    private void selectItem(int position) {

        // Determine which view to show.
        Fragment fragment = new Fragment();

        switch (position) {

        // Top.
        case 0:
            fragment = new TopPostListFragment();
            break;

        // Hot.
        case 1:
            fragment = new HotPostListFragment();
            break;

        // New.
        case 2:
            fragment = new NewPostListFragment();
            break;

        // Rising.
        case 3:
            fragment = new RisingPostListFragment();
            break;

        // Settings.
        case 4:
            fragment = new SettingsFragment();
            break;

        default:
            break;
        }

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        // Update selected item and close drawer.
        drawerList.setItemChecked(position, true);
        drawerLayout.closeDrawer(drawerList);
    }
}