Android Open Source - Swipeable-Cards Card Stack Adapter






From Project

Back to project page Swipeable-Cards.

License

The source code is released under:

Apache License

If you think the Android project Swipeable-Cards listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.andtinder.view;
// www  . j  a va  2 s .  co m
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.andtinder.R;
import com.andtinder.model.CardModel;

import java.util.ArrayList;
import java.util.Collection;

public abstract class CardStackAdapter extends BaseCardStackAdapter {
  private final Context mContext;

  /**
   * Lock used to modify the content of {@link #mData}. Any write operation
   * performed on the deque should be synchronized on this lock.
   */
  private final Object mLock = new Object();
  private ArrayList<CardModel> mData;

  public CardStackAdapter(Context context) {
    mContext = context;
    mData = new ArrayList<CardModel>();
  }

  public CardStackAdapter(Context context, Collection<? extends CardModel> items) {
    mContext = context;
    mData = new ArrayList<CardModel>(items);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    FrameLayout wrapper = (FrameLayout) convertView;
    FrameLayout innerWrapper;
    View cardView;
    View convertedCardView;
    if (wrapper == null) {
      wrapper = new FrameLayout(mContext);
      wrapper.setBackgroundResource(R.drawable.card_bg);
      if (shouldFillCardBackground()) {
        innerWrapper = new FrameLayout(mContext);
        innerWrapper.setBackgroundColor(mContext.getResources().getColor(R.color.card_bg));
        wrapper.addView(innerWrapper);
      } else {
        innerWrapper = wrapper;
      }
      cardView = getCardView(position, getCardModel(position), null, parent);
      innerWrapper.addView(cardView);
    } else {
      if (shouldFillCardBackground()) {
        innerWrapper = (FrameLayout) wrapper.getChildAt(0);
      } else {
        innerWrapper = wrapper;
      }
      cardView = innerWrapper.getChildAt(0);
      convertedCardView = getCardView(position, getCardModel(position), cardView, parent);
      if (convertedCardView != cardView) {
        wrapper.removeView(cardView);
        wrapper.addView(convertedCardView);
      }
    }

    return wrapper;
  }

  protected abstract View getCardView(int position, CardModel model, View convertView, ViewGroup parent);

  public boolean shouldFillCardBackground() {
    return true;
  }

  public void add(CardModel item) {
    synchronized (mLock) {
      mData.add(item);
    }
    notifyDataSetChanged();
  }

  public CardModel pop() {
    CardModel model;
    synchronized (mLock) {
      model = mData.remove(mData.size() - 1);
    }
    notifyDataSetChanged();
    return model;
  }

  @Override
  public Object getItem(int position) {
    return getCardModel(position);
  }

  public CardModel getCardModel(int position) {
    synchronized (mLock) {
      return mData.get(mData.size() - 1 - position);
    }
  }

  @Override
  public int getCount() {
    return mData.size();
  }

  @Override
  public long getItemId(int position) {
    return getItem(position).hashCode();
  }

  public Context getContext() {
    return mContext;
  }
}




Java Source Code List

com.andtinder.Utils.java
com.andtinder.demo.MainActivity.java
com.andtinder.model.CardModel.java
com.andtinder.model.Likes.java
com.andtinder.model.Orientations.java
com.andtinder.view.BaseCardStackAdapter.java
com.andtinder.view.CardContainer.java
com.andtinder.view.CardStackAdapter.java
com.andtinder.view.SimpleCardStackAdapter.java