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.antonioleiva.materializeyourapp; 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.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.text.Html; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.Volley; import com.antonioleiva.materializeyourapp.models.NewsBeans; import com.antonioleiva.materializeyourapp.models.NewsModel; import com.antonioleiva.materializeyourapp.picasso.CircleTransform; import com.google.gson.Gson; import com.squareup.picasso.Picasso; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter.OnItemClickListener { public static final String AVATAR_URL = "http://lorempixel.com/200/200/people/1/"; public static final String DATA_URL = "http://api.k.sohu.com/api/open/channel/newsList.go?channelId=1&page=1&num=20&showContent=1&refer=samsungCocktail"; // static { // for (int i = 1; i <= 10; i++) { // items.add(new NewsModel("Item " + i, "http://n1.itc.cn/img7/adapt/wb/focal/2016/03/30/145931004357546545_180_180.JPEG")); // } // } private DrawerLayout drawerLayout; private View content; private RecyclerView recyclerView; private NavigationView navigationView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initRecyclerView(); initFab(); 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 protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (recyclerView.getAdapter() != null) { outState.putParcelableArrayList("data", (ArrayList) ((RecyclerViewAdapter) recyclerView.getAdapter()).getItems()); } } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if (savedInstanceState.containsKey("data")) { RecyclerViewAdapter adapter = new RecyclerViewAdapter( savedInstanceState.<NewsModel>getParcelableArrayList("data")); adapter.setOnItemClickListener(MainActivity.this); recyclerView.setAdapter(adapter); } } @Override public void onEnterAnimationComplete() { super.onEnterAnimationComplete(); setRecyclerAdapter(recyclerView); recyclerView.scheduleLayoutAnimation(); } private void initRecyclerView() { recyclerView = (RecyclerView) findViewById(R.id.recycler); recyclerView.setLayoutManager(new GridLayoutManager(this, 2)); } private void setRecyclerAdapter(final RecyclerView recyclerView) { RequestQueue queue = Volley.newRequestQueue(this); queue.add(new JsonObjectRequest(DATA_URL, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Gson gson = new Gson(); NewsBeans news = gson.fromJson(response.toString(), NewsBeans.class); List<NewsModel> items = new ArrayList<>(); for (NewsBeans.ArticlesBean articlesBean : news.getArticles()) { NewsModel model = new NewsModel(articlesBean.getTitle(), articlesBean.getImages() != null ? articlesBean.getImages().get(0).getUrl() : articlesBean.getListPic(), Html.fromHtml(articlesBean.getContent())); items.add(model); } RecyclerViewAdapter adapter = new RecyclerViewAdapter(items); adapter.setOnItemClickListener(MainActivity.this); recyclerView.setAdapter(adapter); } }, null)); } private void initFab() { findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { 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() { drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); navigationView = (NavigationView) findViewById(R.id.navigation_view); navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { Snackbar.make(content, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show(); menuItem.setChecked(true); drawerLayout.closeDrawers(); 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, NewsModel newsModel) { DetailActivity.navigate(this, view.findViewById(R.id.image), newsModel); } }