/*
* ******************************************************************************
* 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.extras.cards;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import it.gmariotti.cardslib.demo.extras.R;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardThumbnail;
/**
* This class provides a simple card with Thumbnail loaded with built-in method and Universal-Image-Loader library
* https://github.com/nostra13/Android-Universal-Image-Loader
*
* @author Gabriele Mariotti (gabri.mariotti@gmail.com)
*/
public class UniversalImageLoaderCard extends Card {
protected String mTitle;
protected String mSecondaryTitle;
protected int count;
DisplayImageOptions options;
public UniversalImageLoaderCard(Context context,DisplayImageOptions options) {
this(context, R.layout.carddemo_extra_picasso_inner_content);
this.options=options;
}
public UniversalImageLoaderCard(Context context, int innerLayout) {
super(context, innerLayout);
init();
}
private void init() {
//Add thumbnail
UniversalCardThumbnail cardThumbnail = new UniversalCardThumbnail(mContext);
cardThumbnail.setExternalUsage(true);
addCardThumbnail(cardThumbnail);
//Add ClickListener
setOnClickListener(new OnCardClickListener() {
@Override
public void onClick(Card card, View view) {
Toast.makeText(getContext(), "Click Listener card=", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void setupInnerViewElements(ViewGroup parent, View view) {
//Retrieve elements
TextView title = (TextView) parent.findViewById(R.id.carddemo_extra_picasso_main_inner_title);
TextView secondaryTitle = (TextView) parent.findViewById(R.id.carddemo_extra_picasso_main_inner_secondaryTitle);
if (title != null)
title.setText(mTitle);
if (secondaryTitle != null)
secondaryTitle.setText(mSecondaryTitle);
}
/**
* CardThumbnail which uses Universal-Image-Loader Library.
* If you use an external library you have to provide your login inside #setupInnerViewElements.
*
* This method is called before built-in method.
* If {@link it.gmariotti.cardslib.library.internal.CardThumbnail#isExternalUsage()} is false it uses the built-in method.
*/
class UniversalCardThumbnail extends CardThumbnail {
public UniversalCardThumbnail(Context context) {
super(context);
}
@Override
public void setupInnerViewElements(ViewGroup parent, View viewImage) {
/*
* If your cardthumbnail uses external library you have to provide how to load the image.
* If your cardthumbnail doesn't use an external library it will use a built-in method
*/
//It is just an example.
//In real case you should config better the imageLoader
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
//Here you have to set your image with an external library
//Only for test, use a Resource Id and a Url
if (((UniversalImageLoaderCard) getParentCard()).getCount() % 2 == 0) {
imageLoader.displayImage("https://lh5.googleusercontent.com/-squZd7FxR8Q/UyN5UrsfkqI/AAAAAAAAbAo/VoDHSYAhC_E/s96/new%2520profile%2520%25282%2529.jpg", (ImageView) viewImage,options);
} else {
imageLoader.displayImage("drawable://" + R.drawable.ic_tris, (ImageView) viewImage,options);
}
/*
viewImage.getLayoutParams().width = 96;
viewImage.getLayoutParams().height = 96;
*/
}
}
public String getTitle() {
return mTitle;
}
public void setTitle(String title) {
mTitle = title;
}
public String getSecondaryTitle() {
return mSecondaryTitle;
}
public void setSecondaryTitle(String secondaryTitle) {
mSecondaryTitle = secondaryTitle;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}