/* * Copyright (C) 2015 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.support.design.activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.design.widget.AppBarLayout; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; import android.support.design.widget.TabLayout; import android.support.v4.content.LocalBroadcastManager; import android.support.v4.view.GravityCompat; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import com.support.design.R; import com.support.design.common.Flog; import com.support.design.fragment.CheeseListFragment; import com.support.design.fragment.CustomPagerAdapter; /** * Demo 主界面 * 持续修改中 * * @author Choices */ public class MainActivity extends AppCompatActivity { private DrawerLayout mDrawerLayout; private AppBarLayout mAppBar; public static final int DEFAULT_PAGES = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); final ActionBar ab = getSupportActionBar(); if (ab != null) { ab.setHomeAsUpIndicator(R.drawable.ic_menu); ab.setDisplayHomeAsUpEnabled(true); } mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, // toolbar, R.string.open, R.string.close); // drawerToggle.syncState(); // mDrawerLayout.setDrawerListener(drawerToggle); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); if (navigationView != null) { setupDrawerContent(navigationView); } ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); if (viewPager != null) { setupViewPager(viewPager); tabLayout.setupWithViewPager(viewPager); } FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("Choices")); } private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Flog.d("intent getAction : %s", intent.getAction()); } }; @Override protected void onDestroy() { super.onDestroy(); LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.sample_actions, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: mDrawerLayout.openDrawer(GravityCompat.START); break; case R.id.action_about: // Intent intent = new Intent(this, MessageActivity.class); // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // startActivity(intent); break; } return super.onOptionsItemSelected(item); } private void setupViewPager(ViewPager viewPager) { CustomPagerAdapter adapter = new CustomPagerAdapter(getSupportFragmentManager()); adapter.addFragment(CheeseListFragment.newInstance(CheeseListFragment.GLIDE), "Glide"); adapter.addFragment(CheeseListFragment.newInstance(CheeseListFragment.FRESCO), "Fresco"); viewPager.setAdapter(adapter); viewPager.setOffscreenPageLimit(DEFAULT_PAGES); } private void setupDrawerContent(NavigationView navigationView) { navigationView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { mDrawerLayout.closeDrawers(); mDrawerLayout.setSelected(true); return true; } }); } }