/* * Copyright (c) 2018 Yrom Wang * * 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 net.yrom.shrinker.test; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import net.yrom.shrinker.test.dummy.DummyContent; import java.util.List; /** * An activity representing a list of Items. This activity * has different presentations for handset and tablet-size devices. On * handsets, the activity presents a list of items, which when touched, * lead to a {@link ItemDetailActivity} representing * item details. On tablets, the activity presents the list of items and * item details side-by-side using two vertical panes. */ public class ItemListActivity extends AppCompatActivity { /** * Whether or not the activity is in two-pane mode, i.e. running on a tablet * device. */ private boolean mTwoPane; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_item_list); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); toolbar.setTitle(getTitle()); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); if (findViewById(R.id.item_detail_container) != null) { // The detail container view will be present only in the // large-screen layouts (res/values-w900dp). // If this view is present, then the // activity should be in two-pane mode. mTwoPane = true; } View recyclerView = findViewById(R.id.item_list); assert recyclerView != null; setupRecyclerView((RecyclerView) recyclerView); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_item_list, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.settings) { startActivity(new Intent(this, SettingsActivity.class)); return true; } return super.onOptionsItemSelected(item); } private void setupRecyclerView(@NonNull RecyclerView recyclerView) { recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(this, DummyContent.ITEMS, mTwoPane)); } public static class SimpleItemRecyclerViewAdapter extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> { private final ItemListActivity mParentActivity; private final List<DummyContent.DummyItem> mValues; private final boolean mTwoPane; private final View.OnClickListener mOnClickListener = new View.OnClickListener() { @Override public void onClick(View view) { DummyContent.DummyItem item = (DummyContent.DummyItem) view.getTag(); if (mTwoPane) { Bundle arguments = new Bundle(); arguments.putString(ItemDetailFragment.ARG_ITEM_ID, item.id); ItemDetailFragment fragment = new ItemDetailFragment(); fragment.setArguments(arguments); mParentActivity.getSupportFragmentManager().beginTransaction() .replace(R.id.item_detail_container, fragment) .commit(); } else { Context context = view.getContext(); Intent intent = new Intent(context, ItemDetailActivity.class); intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, item.id); context.startActivity(intent); } } }; SimpleItemRecyclerViewAdapter(ItemListActivity parent, List<DummyContent.DummyItem> items, boolean twoPane) { mValues = items; mParentActivity = parent; mTwoPane = twoPane; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_list_content, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(final ViewHolder holder, int position) { holder.mIdView.setText(mValues.get(position).id); Context context = holder.mIdView.getContext(); int colorId = context.getResources().getIdentifier("color" + Integer.toHexString(position % 16).toUpperCase(), "color", context.getPackageName()); int color = ContextCompat.getColor(context, colorId); holder.mIdView.setBackgroundColor(color); holder.mContentView.setText(mValues.get(position).content); holder.itemView.setTag(mValues.get(position)); holder.itemView.setOnClickListener(mOnClickListener); } @Override public int getItemCount() { return mValues.size(); } class ViewHolder extends RecyclerView.ViewHolder { final TextView mIdView; final TextView mContentView; ViewHolder(View view) { super(view); mIdView = (TextView) view.findViewById(R.id.id_text); mContentView = (TextView) view.findViewById(R.id.content); } } } }