Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buttons and Images #17

Open
ghost opened this issue Sep 11, 2017 · 1 comment
Open

Buttons and Images #17

ghost opened this issue Sep 11, 2017 · 1 comment

Comments

@ghost
Copy link

ghost commented Sep 11, 2017

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?

@ab5593
Copy link

ab5593 commented Jun 14, 2019

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!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant