Comments (1)
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)
- ViewPager card distance HOT 5
- 你好,请问这个可以支持无限循环吗? HOT 2
- Could the item be click? and show a toast? HOT 2
- enableScaling ui bug
- Card view scale
- how to let it cycle HOT 1
- Rtl ViewPager
- how set item width?Disable the item width
- How do you Know the swiped card details
- How do add button click for each card view ? HOT 7
- How to make multiple CardView on page ? HOT 1
- it occurs a problem when data.Size = 1 HOT 1
- How to add library in my project
- How to add library in my project
- 如何进行数据适配 像ViewHolder一样
- Listeners when move from one Card to other
- Added AndroidX Support. Do Check!
- 为什么使用了两种方式实现?
- How to add new fragment
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from viewpagercards.