/
ExpandPicassoFragment.java
258 lines (200 loc) · 9.31 KB
/
ExpandPicassoFragment.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* ******************************************************************************
* Copyright (c) 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.extras.fragment;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.nhaarman.listviewanimations.appearance.AnimationAdapter;
import com.nhaarman.listviewanimations.appearance.simple.AlphaInAnimationAdapter;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import it.gmariotti.cardslib.demo.extras.R;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardArrayAdapter;
import it.gmariotti.cardslib.library.internal.CardExpand;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.internal.ViewToClickToExpand;
import it.gmariotti.cardslib.library.view.CardListView;
/**
* This example uses a list card with expand area inside the Card.
* Pay attention to style="@style/card.main_layout_expandinside" in card layout.
* This style doesn't use a selector as background, but a simple color background.
*
* The adapter is also animated with ListViewAnimations library.
* Please refer to https://github.com/nhaarman/ListViewAnimations for full doc
*
* In expand card it uses a thumbnail loaded with Picasso library.
* Please refer to https://github.com/square/picasso for full doc
*
* @author Gabriele Mariotti (gabri.mariotti@gmail.com)
*/
public class ExpandPicassoFragment extends BaseFragment {
@Override
public int getTitleResourceId() {
return R.string.carddemo_extras_title_expandinside;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.demo_extras_fragment_mix, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initCard();
}
/**
* This method builds a simple cards list
*/
private void initCard() {
//Only for test scope, use images on assets folder
String[] fileName = {"file:///android_asset/images/sea.jpg",
"file:///android_asset/images/snow.jpg",
"file:///android_asset/images/water.jpg",
"file:///android_asset/images/img2.jpg",
"file:///android_asset/images/rose.jpg"};
//Only for test scope, use 5 different header titles
int[] resTitleId = {R.string.carddemo_extras_header_expand_area_inside_sea,
R.string.carddemo_extras_header_expand_area_inside_snow,
R.string.carddemo_extras_header_expand_area_inside_water,
R.string.carddemo_extras_header_expand_area_inside_img2,
R.string.carddemo_extras_header_expand_area_inside_rose};
//Remove debugging from Picasso
Picasso.with(getActivity()).setDebugging(false);
//Init an array of Cards
ArrayList<Card> cards = new ArrayList<Card>();
for (int j = 1; j < 30; j++) {
for (int i = 0; i < 10; i++) {
//Card
CardInside card = new CardInside(this.getActivity());
//Create a CardHeader
CardHeader header = new CardHeader(getActivity(),R.layout.carddemo_extras_expandinside_inner_base_header);
if (i % 2 == 0) {
//Set the header title
header.setTitle(getString(resTitleId[(int) (i / 2) % 5]));
//Add Header to card
card.addCardHeader(header);
//Add an expand area
CardExpandInside expand = new CardExpandInside(getActivity(), fileName[(int) (i / 2) % 5]);
card.addCardExpand(expand);
} else {
//Set the header title
header.setTitle(getString(R.string.carddemo_extras_header_expand_area_inside));
//Add Header to card
card.addCardHeader(header);
//Only for test scope, use a random number
int randomNumber = (int) (Math.random() * 6);
//Add an expand area
CardExpandInsideSquare expand = new CardExpandInsideSquare(getActivity(),randomNumber);
card.addCardExpand(expand);
}
//Add a viewToClickExpand to enable click on whole card
ViewToClickToExpand viewToClickToExpand =
ViewToClickToExpand.builder()
.highlightView(false)
.setupCardElement(ViewToClickToExpand.CardElementUI.CARD);
card.setViewToClickToExpand(viewToClickToExpand);
cards.add(card);
}
}
//Set the arrayAdapter
CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(getActivity(), cards);
CardListView listView = (CardListView) getActivity().findViewById(R.id.carddemo_extra_list_mixinside);
//Add an animator
AnimationAdapter animCardArrayAdapter = new AlphaInAnimationAdapter(mCardArrayAdapter);
animCardArrayAdapter.setAbsListView(listView);
//animCardArrayAdapter.setInitialDelayMillis(500);
if (listView != null) {
listView.setExternalAdapter(animCardArrayAdapter, mCardArrayAdapter);
}
}
/**
* Main Card
*/
public class CardInside extends Card {
public CardInside(Context context) {
super(context);
}
}
/**
* A CardExpand with a image loaded with Picasso from assets folder
*/
class CardExpandInside extends CardExpand {
//Image's name
String mFileName;
public CardExpandInside(Context context, String fileName) {
super(context, R.layout.carddemo_extras_list_card_expandinside_expand_inner);
mFileName = fileName;
}
@Override
public void setupInnerViewElements(ViewGroup parent, View view) {
ImageView img = (ImageView) view.findViewById(R.id.carddemo_inside_image);
//You could use a singleton instance of picasso!
if (img != null) {
Picasso.with(getContext())
.load(Uri.parse(mFileName))
.fit()
.into(img);
}
}
}
/**
* A CardExpand with Texts and other ui elements
*/
class CardExpandInsideSquare extends CardExpand {
int randomNumber;
public CardExpandInsideSquare(Context context,int randomNumber) {
super(context, R.layout.carddemo_extras_list_card_expandinside_expand_square_inner);
this.randomNumber = randomNumber;
}
@Override
public void setupInnerViewElements(ViewGroup parent, View view) {
TextView tx = (TextView) view.findViewById(R.id.carddemo_inside_text);
TextView tx1 = (TextView) view.findViewById(R.id.carddemo_inside_image_title1);
TextView tx2 = (TextView) view.findViewById(R.id.carddemo_inside_image_title2);
if (tx != null)
tx.setText(""+randomNumber);
//Rectangle Color. Only for test scope, use different colors
final GradientDrawable rectangle=(GradientDrawable)tx.getBackground();
if (rectangle!=null){
if (randomNumber<1){
rectangle.setColor(getResources().getColor(R.color.demoextra_card_background_color1));
}else if (randomNumber<2){
rectangle.setColor(getResources().getColor(R.color.demoextra_card_background_color2));
}else if (randomNumber<3){
rectangle.setColor(getResources().getColor(R.color.demoextra_card_background_color3));
}else if (randomNumber<4){
rectangle.setColor(getResources().getColor(R.color.demoextra_card_background_color4));
}else if (randomNumber<5){
rectangle.setColor(getResources().getColor(R.color.demoextra_card_background_color5));
}else {
rectangle.setColor(getResources().getColor(R.color.demoextra_card_background_color6));
}
}
if (tx1 != null)
tx1.setText("Detail............");
if (tx2 != null)
tx2.setText("xxxx xxx xxxx xxxxx");
}
}
}