ViewPagerCards
ViewPagerCards copied to clipboard
Buttons and Images
Hi There are two problems with the source: Can't use buttons for starting activities. Can't use Images for each cards: mCardAdapter.addCardItem(new CardItem(R.string.title_1, R.string.title_1 )); I want use images for second argument.
How can I do these?
To use images for second argument..
in the adapter.xml
add an ImageView
as below:
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Next, in CardItem
class do the following :
public class CardItem {
private int mTitleResource;
// This is the change to add image in the second argument..
private int mImage;
public CardItem(int title, int image) {
mTitleResource = title;
mImage = image;
}
/*
public int getText() {
return mTextResource;
}
*/
public int getTitle() {
return mTitleResource;
}
// Add the getters for the image..
public int getImage() {
return mImage;
}
}
Then update the CardPagerAdapter
as below:
public class CardPagerAdapter extends PagerAdapter implements CardAdapter {
private List<CardView> mViews;
private List<CardItem> mData;
private List<CardItem> mImage; // this is the change here
private float mBaseElevation;
public CardPagerAdapter() {
mData = new ArrayList<>();
mViews = new ArrayList<>();
mImage = new ArrayList<>(); // note this change.. we are creating a new ArrayList for Images..
}
public void addCardItem(CardItem item) {
mViews.add(null);
mData.add(item);
mImage.add(item); // add the image in the card...
}
public float getBaseElevation() {
return mBaseElevation;
}
@Override
public CardView getCardViewAt(int position) {
return mViews.get(position);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = LayoutInflater.from(container.getContext())
.inflate(R.layout.adapter, container, false);
container.addView(view);
bind(mData.get(position), view);
CardView cardView = (CardView) view.findViewById(R.id.cardView);
if (mBaseElevation == 0) {
mBaseElevation = cardView.getCardElevation();
}
cardView.setMaxCardElevation(mBaseElevation * MAX_ELEVATION_FACTOR);
mViews.set(position, cardView);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
mViews.set(position, null);
}
private void bind(CardItem item, View view) {
TextView titleTextView = (TextView) view.findViewById(R.id.titleTextView);
// TextView contentTextView = (TextView) view.findViewById(R.id.contentTextView);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView); // bind the image to the view
titleTextView.setText(item.getTitle());
// contentTextView.setText(item.getText());
imageView.setImageResource(item.getImage()); // set the image resource
}
}
Finally in the MainActivity
class you can use Images as the second argument as below:
mCardAdapter = new CardPagerAdapter();
//change the string resource to your drawables images in the second argument..
mCardAdapter.addCardItem(new CardItem(R.string.title_1, R.drawable.image1));
Thats it!!