Back to project page vocabulary-list.
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.
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); } } }