/* * ****************************************************************************** * Copyright (c) 2013-2014 Gabriele Mariotti. * * 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 it.gmariotti.cardslib.demo.fragment.nativeview; import android.os.AsyncTask; import android.os.Bundle; import android.os.SystemClock; import android.support.v7.widget.LinearLayoutManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import java.util.ArrayList; import it.gmariotti.cardslib.demo.R; import it.gmariotti.cardslib.library.cards.actions.BaseSupplementalAction; import it.gmariotti.cardslib.library.cards.actions.TextSupplementalAction; import it.gmariotti.cardslib.library.cards.material.MaterialLargeImageCard; import it.gmariotti.cardslib.library.internal.Card; import it.gmariotti.cardslib.library.recyclerview.internal.CardArrayRecyclerViewAdapter; import it.gmariotti.cardslib.library.recyclerview.view.CardRecyclerView; /** * @author Gabriele Mariotti (gabri.mariotti@gmail.com) */ public class NativeRecyclerViewMaterialCardFragment extends BaseNativeListFragment { CardArrayRecyclerViewAdapter mCardArrayAdapter; @Override protected int getSubTitleHeaderResourceId() { return R.string.header_title_subtitle_recyclerview; } @Override protected int getTitleHeaderResourceId() { return R.string.header_title_group3; } @Override protected String getDocUrl() { return "https://github.com/gabrielemariotti/cardslib/blob/master/doc/RECYCLERVIEW.md"; } @Override protected String getSourceUrl() { return "https://github.com/gabrielemariotti/cardslib/blob/master/demo/stock/src/main/java/it/gmariotti/cardslib/demo/fragment/nativeview/NativeRecyclerViewFragment.java"; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.demo_fragment_native_recyclerview_materialcard, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); hideList(false); //Set the arrayAdapter ArrayList<Card> cards = new ArrayList<Card>(); mCardArrayAdapter = new CardArrayRecyclerViewAdapter(getActivity(), cards); //Staggered grid view CardRecyclerView mRecyclerView = (CardRecyclerView) getActivity().findViewById(R.id.carddemo_recyclerview2); mRecyclerView.setHasFixedSize(false); mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); //Set the empty view if (mRecyclerView != null) { mRecyclerView.setAdapter(mCardArrayAdapter); } //Load cards new LoaderAsyncTask().execute(); } //------------------------------------------------------------------------------------------------------------- // Images loader //------------------------------------------------------------------------------------------------------------- /** * Async Task to elaborate images */ class LoaderAsyncTask extends AsyncTask<Void, Void, ArrayList<Card>> { LoaderAsyncTask() { } @Override protected ArrayList<Card> doInBackground(Void... params) { //elaborate images SystemClock.sleep(1000); //delay to simulate download, don't use it in a real app if (isAdded()) { ArrayList<Card> cards = initCard(); return cards; }else return null; } @Override protected void onPostExecute(ArrayList<Card> cards) { //Update the adapter updateAdapter(cards); displayList(); } } /** * This method builds a simple list of cards */ private ArrayList<Card> initCard() { //Init an array of Cards ArrayList<Card> cards = new ArrayList<Card>(); for (int i = 0; i < 200; i++) { ArrayList<BaseSupplementalAction> actions = new ArrayList<BaseSupplementalAction>(); // Set supplemental actions TextSupplementalAction t1 = new TextSupplementalAction(getActivity(), R.id.text1); t1.setOnActionClickListener(new BaseSupplementalAction.OnActionClickListener() { @Override public void onClick(Card card, View view) { Toast.makeText(getActivity()," Click on Text SHARE "+card.getTitle(),Toast.LENGTH_SHORT).show(); } }); actions.add(t1); TextSupplementalAction t2 = new TextSupplementalAction(getActivity(), R.id.text2); t2.setOnActionClickListener(new BaseSupplementalAction.OnActionClickListener() { @Override public void onClick(Card card, View view) { Toast.makeText(getActivity()," Click on Text LEARN "+card.getTitle(),Toast.LENGTH_SHORT).show(); } }); actions.add(t2); //Create a Card, set the title over the image and set the thumbnail MaterialLargeImageCard card = MaterialLargeImageCard.with(getActivity()) .setTextOverImage("Italian Beaches "+i) .setTitle("This is my favorite local beach "+i) .setSubTitle("A wonderful place") .useDrawableId(R.drawable.sea) .setupSupplementalActions(R.layout.carddemo_native_material_supplemental_actions_large, actions) .build(); card.setOnClickListener(new Card.OnCardClickListener() { @Override public void onClick(Card card, View view) { Toast.makeText(getActivity()," Click on ActionArea ",Toast.LENGTH_SHORT).show(); } }); cards.add(card); } return cards; } /** * Update the adapter */ private void updateAdapter(ArrayList<Card> cards) { if (cards != null) { mCardArrayAdapter.addAll(cards); } } }