Android Open Source - vocabulary-list Card Adapter






From Project

Back to project page vocabulary-list.

License

The source code is released under:

Apache License

If you think the Android project vocabulary-list 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.rulych.vocabularylist.adapters;
/*  w  w w.  j a  v  a2 s .  c  o m*/
import com.rulych.vocabularylist.R;
import com.rulych.vocabularylist.fragments.CardFragment;
import com.rulych.vocabularylist.model.Card;
import com.rulych.vocabularylist.model.comparation.CardComparator;

import android.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.Holder> {

    private static final String TAG_EDIT_CARD_FRAGMENT = "edit-card-fragment";

    private final List<Card> mCards = new ArrayList<>();
    private FragmentManager mFragmentManager;

    @Inject public CardAdapter() {
    }

    public void setFragmentManager(FragmentManager fragmentManager) {
        mFragmentManager = fragmentManager;
    }

    @Override public Holder onCreateViewHolder(ViewGroup parent, int position) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_card, parent, false);

        return new Holder(view);
    }

    @Override public void onBindViewHolder(Holder holder, final int position) {
        Card card = mCards.get(position);
        holder.labelEntry.setText(card.entry);
        holder.labelPronunciation.setText(card.pronunciation);
        holder.labelTranslation.setText(card.translation);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View view) {
                CardFragment.newInstance(mCards.get(position))
                        .show(mFragmentManager, TAG_EDIT_CARD_FRAGMENT);
            }
        });
    }

    @Override public int getItemCount() {
        return mCards.size();
    }

    public void add(Card card) {
        mCards.add(card);
        sort();
        notifyDataSetChanged();
    }

    public void addAll(Collection<Card> cards) {
        mCards.addAll(cards);
        sort();
        notifyDataSetChanged();
    }

    public void refresh(Card card) {
        mCards.remove(card);
        mCards.add(card);
        sort();
        notifyDataSetChanged();
    }

    public void remove(Card card) {
        mCards.remove(card);
        notifyDataSetChanged();
    }

    public void sort() {
        Collections.sort(mCards, new CardComparator());
    }

    static class Holder extends RecyclerView.ViewHolder {
        @InjectView(R.id.label_entry) TextView labelEntry;
        @InjectView(R.id.label_translation) TextView labelTranslation;
        @InjectView(R.id.label_pronunciation) TextView labelPronunciation;

        public Holder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

}




Java Source Code List

com.rulych.vocabularylist.AppModule.java
com.rulych.vocabularylist.VocabularyListApplication.java
com.rulych.vocabularylist.activities.VocabularyListActivity.java
com.rulych.vocabularylist.adapters.CardAdapter.java
com.rulych.vocabularylist.fragments.BaseCardFragment.java
com.rulych.vocabularylist.fragments.CardFragment.java
com.rulych.vocabularylist.fragments.DriveApiFragment.java
com.rulych.vocabularylist.fragments.NewCardFragment.java
com.rulych.vocabularylist.fragments.VocabularyListFragment.java
com.rulych.vocabularylist.model.Card.java
com.rulych.vocabularylist.model.comparation.CardComparator.java
com.rulych.vocabularylist.model.persistence.CardDAO.java
com.rulych.vocabularylist.model.persistence.PersistenceModule.java
com.rulych.vocabularylist.model.persistence.impl.CardDAOImpl.java
com.rulych.vocabularylist.model.persistence.impl.CardListReaderImpl.java
com.rulych.vocabularylist.model.persistence.impl.CardListReader.java
com.rulych.vocabularylist.model.persistence.impl.CardListWriterImpl.java
com.rulych.vocabularylist.model.persistence.impl.CardListWriter.java
com.rulych.vocabularylist.model.persistence.impl.FileGetterImpl.java
com.rulych.vocabularylist.model.persistence.impl.FileGetter.java
com.rulych.vocabularylist.model.persistence.impl.exception.CardNotFoundException.java
com.rulych.vocabularylist.model.persistence.impl.exception.CouldNotGetFileException.java
com.rulych.vocabularylist.model.persistence.impl.exception.CouldNotReadFileException.java
com.rulych.vocabularylist.model.persistence.impl.exception.CouldNotWriteFileException.java
com.rulych.vocabularylist.util.BaseRuntimeException.java
com.rulych.vocabularylist.util.FloatingActionButtonAnimator.java
com.rulych.vocabularylist.util.ThreadLoggingTree.java
com.rulych.vocabularylist.util.UtilModule.java
com.rulych.vocabularylist.util.impl.FloatingActionButtonAnimatorImpl.java