Java tutorial
/* * Copyright (C) 2015 Antonio Leiva * * 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.appsummary.luoxf.myappsummary; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.design.widget.Snackbar; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import com.appsummary.luoxf.myappsummary.animation.AnimationMainActivity; import com.appsummary.luoxf.myappsummary.more.MoreMainActivity; import com.appsummary.luoxf.myappsummary.navigationtabstrip.NavigationTabStripActivity; import com.appsummary.luoxf.myappsummary.picasso.CircleTransform; import com.appsummary.luoxf.myappsummary.recyclerView.RecyclerViewMainActivity; import com.appsummary.luoxf.myappsummary.recyclerView.RecyclerViewTabActivity; import com.appsummary.luoxf.mylibrary.swipbackhelper.SwipeBackHelper; import com.squareup.picasso.Picasso; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.OnClick; public class MainActivity extends BaseActivity implements RecyclerViewAdapter.OnItemClickListener { @BindView(R.id.recycler) RecyclerView recyclerView; @BindView(R.id.drawer_layout) DrawerLayout drawerLayout; @BindView(R.id.navigation_view) NavigationView navigationView; @BindView(R.id.content) View content; public static final String AVATAR_URL = "http://lorempixel.com/200/200/people/1/"; private static List<ViewModel> items = new ArrayList<>(); static { for (int i = 1; i <= 10; i++) { items.add(new ViewModel("Item " + i, "http://lorempixel.com/500/500/animals/" + i)); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SwipeBackHelper.getCurrentPage(this).setSwipeBackEnable(false); initRecyclerView(); initToolbar(); setupDrawerLayout(); content = findViewById(R.id.content); final ImageView avatar = (ImageView) navigationView.getHeaderView(0).findViewById(R.id.avatar); Picasso.with(this).load(AVATAR_URL).transform(new CircleTransform()).into(avatar); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { setRecyclerAdapter(recyclerView); } } @Override public int getLayoutId() { return R.layout.activity_main; } @Override public void onEnterAnimationComplete() { super.onEnterAnimationComplete(); setRecyclerAdapter(recyclerView); recyclerView.scheduleLayoutAnimation(); } private void initRecyclerView() { recyclerView.setLayoutManager(new GridLayoutManager(this, 2)); } private void setRecyclerAdapter(RecyclerView recyclerView) { RecyclerViewAdapter adapter = new RecyclerViewAdapter(items); adapter.setOnItemClickListener(this); recyclerView.setAdapter(adapter); } @OnClick(R.id.fab) public void fabClick() { Snackbar.make(content, "FAB Clicked", Snackbar.LENGTH_SHORT).show(); } private void initToolbar() { final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); final ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp); actionBar.setDisplayHomeAsUpEnabled(true); } } private void setupDrawerLayout() { navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { int itemid = menuItem.getItemId(); // Snackbar.make(content, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show(); menuItem.setChecked(true); drawerLayout.closeDrawers(); switch (itemid) { case R.id.drawer_recycler_view: startActivity(new Intent(MainActivity.this, RecyclerViewMainActivity.class)); break; case R.id.drawer_animation: startActivity(new Intent(MainActivity.this, AnimationMainActivity.class)); break; case R.id.drawer_navigationTabStrip: startActivity(new Intent(MainActivity.this, NavigationTabStripActivity.class)); break; case R.id.drawer_more: startActivity(new Intent(MainActivity.this, MoreMainActivity.class)); break; default: break; } return true; } }); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: drawerLayout.openDrawer(GravityCompat.START); return true; } return super.onOptionsItemSelected(item); } @Override public void onItemClick(View view, ViewModel viewModel) { DetailActivity.navigate(this, view.findViewById(R.id.image), viewModel); } }