/
NativeRecyclerViewMaterialCardFragment.java
191 lines (156 loc) · 6.78 KB
/
NativeRecyclerViewMaterialCardFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* ******************************************************************************
* 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);
}
}
}