com.jjoseba.podemosquotes.fragment.MainActivityFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.jjoseba.podemosquotes.fragment.MainActivityFragment.java

Source

/*
This file is part of PodemosQuotes.
    
PodemosQuotes is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
PodemosQuotes is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with PodemosQuotes.  If not, see <http://www.gnu.org/licenses/>
    
 */

package com.jjoseba.podemosquotes.fragment;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.jjoseba.podemosquotes.R;
import com.jjoseba.podemosquotes.adapter.QuotesGridAdapter;
import com.jjoseba.podemosquotes.application.SoundManager;
import com.jjoseba.podemosquotes.model.JSONReader;
import com.jjoseba.podemosquotes.model.Quote;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnItemClick;
import butterknife.OnItemLongClick;

public class MainActivityFragment extends Fragment {

    public static String TAG = MainActivityFragment.class.getName();

    @Bind(R.id.quotesGrid)
    GridView quotesGrid;

    private ArrayList<Quote> quotes = new ArrayList<>();
    private QuotesGridAdapter adapter;

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        ButterKnife.bind(this, rootView);

        quotes = new JSONReader(this.getActivity()).parse();
        adapter = new QuotesGridAdapter(this.getContext(), quotes);
        quotesGrid.setAdapter(adapter);

        return rootView;
    }

    @OnItemClick(R.id.quotesGrid)
    void gridItemClick(AdapterView<?> parent, View view, int position, long id) {
        Quote quote = adapter.getItem(position);
        String soundFile = quote.getSoundPath();
        SoundManager.getInstance(getActivity()).playSound(soundFile);
        Log.d(TAG, quote.getQuote());
    }

    @OnItemLongClick(R.id.quotesGrid)
    public boolean gridItemLongCLick(AdapterView<?> parent, View view, int position, long id) {
        Quote quote = adapter.getItem(position);
        Log.d(TAG, quote.getQuote());

        Bundle args = new Bundle();
        args.putSerializable(QuoteDialog.DIALOG_QUOTE, quote);

        QuoteDialog dialog = new QuoteDialog();
        dialog.setArguments(args);
        dialog.show(this.getFragmentManager(), TAG);
        return true;
    }

}