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.fragments; /* w w w . j a v a 2 s .co m*/ import com.google.android.gms.common.api.GoogleApiClient; import com.rulych.vocabularylist.R; import com.rulych.vocabularylist.VocabularyListApplication; import com.rulych.vocabularylist.adapters.CardAdapter; import com.rulych.vocabularylist.model.Card; import com.rulych.vocabularylist.model.persistence.CardDAO; import com.rulych.vocabularylist.util.FloatingActionButtonAnimator; import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; import android.app.Activity; import android.app.Fragment; import android.graphics.Outline; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.Toast; import java.util.List; import javax.inject.Inject; import butterknife.ButterKnife; import butterknife.InjectView; import butterknife.OnClick; import rx.android.schedulers.AndroidSchedulers; import rx.functions.Action1; import timber.log.Timber; public class VocabularyListFragment extends Fragment implements DriveApiFragment { private static final String TAG_NEW_CARD_DIALOG = "new-card-dialog"; @InjectView(R.id.list_vocabulary) RecyclerView mList; @InjectView(R.id.add_card_btn) ImageButton mAddCard; @Inject Bus mBus; @Inject CardDAO mCardDAO; @Inject CardAdapter mAdapter; @Inject FloatingActionButtonAnimator.Factory mFabAnimatorFactory; private RecyclerView.LayoutManager mLayoutManager; @Override public void onAttach(Activity activity) { super.onAttach(activity); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); VocabularyListApplication.get(getActivity()).inject(this); mAdapter.setFragmentManager(getChildFragmentManager()); mBus.register(this); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_vocabulary_list, container, false); ButterKnife.inject(this, view); mLayoutManager = new LinearLayoutManager(getActivity()); mList.setLayoutManager(mLayoutManager); mList.setAdapter(mAdapter); setAddCardButtonOutline(); mFabAnimatorFactory.create(mAddCard).setup(); return view; } @Override public void onDestroyView() { super.onDestroyView(); ButterKnife.reset(this); } @Override public void onDestroy() { super.onDestroy(); mBus.register(this); } @OnClick(R.id.add_card_btn) public void onNewCardClicked() { NewCardFragment newCardFragment = new NewCardFragment(); newCardFragment.show(getFragmentManager(), TAG_NEW_CARD_DIALOG); } @Override public void onApiClientReady(GoogleApiClient apiClient) { mCardDAO.start(apiClient); mCardDAO.all() .observeOn(AndroidSchedulers.mainThread()) .subscribe(new OnCardsDownloadSuccessAction(), new OnCardsDownloadErrorAction()); } @Subscribe public void on(CardDAO.NewCardSavedEvent event) { mAdapter.add(event.card); } @Subscribe public void on(CardDAO.CardDeletedEvent event) { mAdapter.remove(event.card); } @Subscribe public void on(CardDAO.CardModifiedEvent event) { mAdapter.refresh(event.card); } private void setAddCardButtonOutline() { int size = getResources().getDimensionPixelSize(R.dimen.fab_size); Outline outline = new Outline(); outline.setOval(0, 0, size, size); mAddCard.setOutline(outline); } private class OnCardsDownloadSuccessAction implements Action1<List<Card>> { @Override public void call(List<Card> cards) { Timber.d("Got %d cards.", cards.size()); mAdapter.addAll(cards); } } private class OnCardsDownloadErrorAction implements Action1<Throwable> { @Override public void call(Throwable throwable) { Timber.e(throwable, "Error getting cards."); Toast.makeText(getActivity(), R.string.card_list_download_error, Toast.LENGTH_SHORT) .show(); } } }