Open sharad9m opened 7 years ago
To achieve this, you should create a custom adapter, which extends BaseAdapter. Something like this:
SwipeCardAdapter.java:
public class SwipeCardAdapter extends BaseAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
ArrayList mSwipeCardArrayList;
public SwipeCardAdapter(Context context, LayoutInflater layoutInflater, ArrayList<SwipeCard> swipeCardArraylist) {
this.mContext = context;
this.mLayoutInflater = layoutInflater;
this.mSwipeCardArrayList = swipeCardArraylist;
}
@Override
public int getCount() {
return mSwipeCardArrayList.size();
}
@Override
public Object getItem(int position) {
return mSwipeCardArrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null){
convertView = mLayoutInflater.inflate(R.layout.carditem, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView1 = (TextView) convertView.findViewById(R.id.cardItemText1);
viewHolder.textView2 = (TextView) convertView.findViewById(R.id.cardItemText2);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
SwipeCard sw = (SwipeCard) mSwipeCardArrayList.get(position);
viewHolder.textView1.setText(sw.getText1());
viewHolder.textView2.setText(sw.getText2());
return convertView;
}
private static class ViewHolder {
public TextView textView1, textView2;
}
}
cardItem.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="250dp"
android:layout_height="350dp"
android:layout_gravity="center"
android:background="@color/colorPrimary"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cardItemText1"
android:gravity="center"
android:textColor="@android:color/white"
android:text="text1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cardItemText2"
android:gravity="center"
android:textColor="@android:color/white"
android:text="text2"/>
</LinearLayout>
SwipeCard.java:
public class SwipeCard {
private String text1,text2;
public SwipeCard(String text1, String text2) {
this.text1 = text1;
this.text2 = text2;
}
public String getText1() {
return text1;
}
public void setText1(String text1) {
this.text1 = text1;
}
public String getText2() {
return text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
}
and use this in your onCreate:
al = new ArrayList<SwipeCard>();
al.add(new SwipeCard("card1text1", "card1text2"));
al.add(new SwipeCard("card2text1", "card2text2"));
al.add(new SwipeCard("card3text1", "card3text2"));
al.add(new SwipeCard("card4text1", "card4text2"));
al.add(new SwipeCard("card5text1", "card5text2"));
swipeCardAdapter = new SwipeCardAdapter(this, getLayoutInflater(), al);
mflingContainer.setAdapter(swipeCardAdapter);
Hello, I have followed the above implementation and am still unable to populate an imageview with the adapter. Here is the relevant SO post I made https://stackoverflow.com/questions/47143159/populate-imageview-from-baseadapter-swipecards
Thanks, Ian
This is supports text only, How to add card view, image and multiple textview