Giter Site home page Giter Site logo

Buttons and Images about viewpagercards HOT 1 OPEN

rubensousa avatar rubensousa commented on July 30, 2024
Buttons and Images

from viewpagercards.

Comments (1)

ab5593 avatar ab5593 commented on July 30, 2024

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

from viewpagercards.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.