Java tutorial
/* * 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 sk.jmurin.android.hradcicva.gui; import android.content.Context; import android.content.Intent; import android.graphics.Typeface; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.CollapsingToolbarLayout; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.globallogic.training.hradcicva.R; import sk.jmurin.android.hradcicva.data.Database; import java.util.List; public class MainActivityIncludeFragment extends Fragment { static Typeface tf; public static MainActivityIncludeFragment getInstance(String name) { MainActivityIncludeFragment inst = new MainActivityIncludeFragment(); return inst; } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_main_include_fragment, container, false); CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) view .findViewById(R.id.collapsing_toolbar); collapsingToolbar.setTitle(getString(R.string.toolbar_title)); tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Constantia.ttf"); collapsingToolbar.setCollapsedTitleTypeface(tf); collapsingToolbar.setExpandedTitleTypeface(tf); final ImageView imageView = (ImageView) view.findViewById(R.id.backdrop); Glide.with(this).load(Database.getHeaderDrawable()).centerCrop().into(imageView); RecyclerView rv = (RecyclerView) view.findViewById(R.id.recyclerview); setupRecyclerView(rv); return view; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } private void setupRecyclerView(RecyclerView recyclerView) { recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext())); recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity(), Database.getButtonNames())); } public static class SimpleStringRecyclerViewAdapter extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> { private final TypedValue mTypedValue = new TypedValue(); private int mBackground; private List<String> mValues; public static class ViewHolder extends RecyclerView.ViewHolder { public String mBoundString; public final View mView; public final ImageView mImageView; public final TextView mTextView; public ViewHolder(View view) { super(view); mView = view; mImageView = (ImageView) view.findViewById(R.id.avatar); mTextView = (TextView) view.findViewById(android.R.id.text1); } @Override public String toString() { return super.toString() + " '" + mTextView.getText(); } } public String getValueAt(int position) { return mValues.get(position); } public SimpleStringRecyclerViewAdapter(Context context, List<String> items) { context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true); mBackground = mTypedValue.resourceId; mValues = items; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false); view.setBackgroundResource(mBackground); return new ViewHolder(view); } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { holder.mBoundString = mValues.get(position); holder.mTextView.setText(mValues.get(position)); holder.mTextView.setTypeface(tf); holder.mView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Context context = v.getContext(); Intent intent = new Intent(context, PagerActivity2.class); intent.putExtra(PagerActivity2.MENU_ID, position); context.startActivity(intent); } }); Glide.with(holder.mImageView.getContext()).load(Database.getButtonDrawable(position)).fitCenter() .into(holder.mImageView); } @Override public int getItemCount() { return mValues.size(); } } }