Android Open Source - Joetz-Android-V2 Home Fragment






From Project

Back to project page Joetz-Android-V2.

License

The source code is released under:

GNU General Public License

If you think the Android project Joetz-Android-V2 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.example.jens.myapplication.home;
/*from w  w  w . jav  a 2 s .  co  m*/

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.DefaultItemAnimator;
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.Toast;

import com.example.jens.myapplication.R;
import com.example.jens.myapplication.apimanager.ApiConnection;
import com.example.jens.myapplication.apimanager.SimpleRequestTask;
import com.example.jens.myapplication.apimanager.manager.CampManager;
import com.example.jens.myapplication.camps.RecyclerItemClickListener;
import com.example.jens.myapplication.domain.Camp;
import com.example.jens.myapplication.sam.JoetzApplication;
import com.example.jens.myapplication.sam.MyFragment;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends MyFragment {

    private List<Reclame> products;
    private ImageSlideAdapter mImageSlideAdapter;
    private ViewPager mViewPager;

    private List<Camp> mAanbevolen;
    private RecyclerView mRecyclerView;
    private AanbevolenAdapter mAdapter;

    private CampManager mCampManager;
    private SimpleRequestTask mListenerLoadCamps;

    private Callbacks mCallbacks = sAanbevolenCallbacks;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        mImageSlideAdapter = new ImageSlideAdapter(getActivity(), products);
        mViewPager = (ViewPager) view.findViewById(R.id.pagerSlider);
        mViewPager.setAdapter(mImageSlideAdapter);

        mRecyclerView = (RecyclerView) view.findViewById(R.id.lstAanbevolen);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        mRecyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                       Camp c = mAdapter.getItem(position);
                       mCallbacks.onAanbevolenItemSelected(c.getId());
                    }
                })
        );

        retrieveCamps();

        return view;
    }

    private void getData(){
        products = new ArrayList<>();
        products.add(new Reclame(1,"Joetz is kk leuk", "http://cache4.asset-cache.net/gc/117149527-happy-indian-girls-exploring-something-in-gettyimages.jpg?v=1&c=IWSAsset&k=2&d=JPA0V7865CIsGHPNL83Gf2k5UaLMJTcA6ug9u5Uz380%3D"));
        products.add(new Reclame(2,"Maak nieuwe kk vriendjes", "http://polishmycrown.files.wordpress.com/2011/03/tumblr_lf3j5lvwwn1qcmiu9o1_5001.jpg"));
        products.add(new Reclame(3,"Even verlost van je kk kind", "http://theferkel.files.wordpress.com/2013/05/300688_10150339844429260_820764259_8021172_208942660_n.jpg"));
    }

    private void fillAanbeloven(){
        CampManager campManager = ((JoetzApplication) getActivity().getApplication()).getCampManager();
       mAanbevolen = campManager.getCamps();
       // mAanbevolen = mAanbevolen.subList(0,3);

        mAdapter = new AanbevolenAdapter(mAanbevolen, R.layout.item_aanbevolen, getActivity());
        mRecyclerView.setAdapter(mAdapter);

    }

    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        public void run() {
            if( mViewPager.getCurrentItem() >= mImageSlideAdapter.getCount() -1){
                mViewPager.setCurrentItem(0);
            }else{
                mViewPager.setCurrentItem(
                        mViewPager.getCurrentItem() + 1, true);
            }
            mViewPager.setCurrentItem(mViewPager.getCurrentItem(), true);
            handler.postDelayed(runnable, 5000);
        }
    };

    @Override
    public void onPause() {
        super.onPause();
        if (handler!= null) {
            handler.removeCallbacks(runnable);
        }
    }

    @Override
    public void onResume() {
        super.onResume();  // Always call the superclass method first
        handler.postDelayed(runnable, 5000);
    }

    private void retrieveCamps() {

        mCampManager = ((JoetzApplication) getActivity().getApplication()).getCampManager();
        if (mCampManager.isCampsLoaded()) {
            fillAanbeloven();
            return;
        }

        mListenerLoadCamps = new SimpleRequestTask() {
            @Override
            public void doTask(int statusCode) {
                //mRetrieveCampsTask = null;
                mListenerLoadCamps = null;
                if (statusCode < 0) {
                } else if (statusCode != ApiConnection.STATUS_OK) {
                    Toast.makeText(getActivity(), R.string.error_unknown, Toast.LENGTH_SHORT).show();
                } else {
                   fillAanbeloven();
                }
            }
        };

        //mRetrieveCampsTask = mCampManager.retrieveCamps(mListenerLoadCamps);
        mCampManager.retrieveCamps(mListenerLoadCamps);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // Activities containing this fragment must implement its callbacks.
        if (!(activity instanceof Callbacks)) {
            throw new IllegalStateException("Activity must implement fragment's callbacks.");
        }

        mCallbacks = (Callbacks) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();

        // Reset the active callbacks interface to the dummy implementation.
        mCallbacks = sAanbevolenCallbacks;
    }

    public interface Callbacks {
        /**
         * Callback for when an item has been selected.
         */
        public void onAanbevolenItemSelected(Long id);
    }

    private static Callbacks sAanbevolenCallbacks = new Callbacks() {
        public void onAanbevolenItemSelected(Long id) {
        }
    };


    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mListenerLoadCamps != null){
            mCampManager.unsubscribeLoadCamps(mListenerLoadCamps);
        }
    }


}




Java Source Code List

com.example.jens.myapplication.ApplicationTest.java
com.example.jens.myapplication.account.FragmentAccount.java
com.example.jens.myapplication.account.LoginDialogActivity.java
com.example.jens.myapplication.account.LoginFragment.java
com.example.jens.myapplication.account.PersonsFragment.java
com.example.jens.myapplication.account.ProfileFragment.java
com.example.jens.myapplication.account.RegisterDialogActivity.java
com.example.jens.myapplication.account.RegisterFragment.java
com.example.jens.myapplication.account.TabsPagerAdapter.java
com.example.jens.myapplication.activities.ActivityDetailActivity.java
com.example.jens.myapplication.activities.ActivityDetailFragment.java
com.example.jens.myapplication.activities.FragmentListActivities.java
com.example.jens.myapplication.activities.JoetzActivitiesAdapter.java
com.example.jens.myapplication.activities.JoetzActivity.java
com.example.jens.myapplication.activities.StartActivity.java
com.example.jens.myapplication.apimanager.ApiConnection.java
com.example.jens.myapplication.apimanager.CancellableTask.java
com.example.jens.myapplication.apimanager.JSONParsingTask.java
com.example.jens.myapplication.apimanager.PostRequestTask.java
com.example.jens.myapplication.apimanager.RequestParams.java
com.example.jens.myapplication.apimanager.SimpleRequestTask.java
com.example.jens.myapplication.apimanager.filter.PersonFilter.java
com.example.jens.myapplication.apimanager.manager.ActivityManager.java
com.example.jens.myapplication.apimanager.manager.AuthToken.java
com.example.jens.myapplication.apimanager.manager.BookingManager.java
com.example.jens.myapplication.apimanager.manager.CampManager.java
com.example.jens.myapplication.apimanager.manager.ImageManager.java
com.example.jens.myapplication.apimanager.manager.LoginManager.java
com.example.jens.myapplication.apimanager.manager.ProfileManager.java
com.example.jens.myapplication.apimanager.manager.RegisterManager.java
com.example.jens.myapplication.booking.AddPersonActivity.java
com.example.jens.myapplication.booking.BookingActivity.java
com.example.jens.myapplication.booking.BookingBondFragment.java
com.example.jens.myapplication.booking.BookingContactFragment.java
com.example.jens.myapplication.booking.BookingExtraFragment.java
com.example.jens.myapplication.booking.BookingParticipantsFragment.java
com.example.jens.myapplication.bookingHistory.BookingHistoryDetailActivity.java
com.example.jens.myapplication.bookingHistory.BookingHistoryDetailFragment.java
com.example.jens.myapplication.bookingHistory.BookingHistoryOverviewFragment.java
com.example.jens.myapplication.bookingHistory.BookingsAdapter.java
com.example.jens.myapplication.calendar.FragmentCalendar.java
com.example.jens.myapplication.calendar.KalenderAdapter.java
com.example.jens.myapplication.calendar.KalenderItem.java
com.example.jens.myapplication.camps.CampAdapter.java
com.example.jens.myapplication.camps.CampDetailActivity.java
com.example.jens.myapplication.camps.CampFilterActivity.java
com.example.jens.myapplication.camps.FilterFragment.java
com.example.jens.myapplication.camps.FragmentListCamps.java
com.example.jens.myapplication.camps.ImageActivity.java
com.example.jens.myapplication.camps.ImagePagerFragment.java
com.example.jens.myapplication.camps.InfoFragment.java
com.example.jens.myapplication.camps.PhotoFragment.java
com.example.jens.myapplication.camps.RecyclerItemClickListener.java
com.example.jens.myapplication.camps.SquareImageView.java
com.example.jens.myapplication.camps.TabsPagerAdapter.java
com.example.jens.myapplication.domain.BookingStatus.java
com.example.jens.myapplication.domain.Booking.java
com.example.jens.myapplication.domain.Camp.java
com.example.jens.myapplication.domain.PeriodCategory.java
com.example.jens.myapplication.domain.Person.java
com.example.jens.myapplication.domain.UserRole.java
com.example.jens.myapplication.domain.User.java
com.example.jens.myapplication.domain.binding.PersonValidatorBinding.java
com.example.jens.myapplication.domain.binding.UserValidatorBinding.java
com.example.jens.myapplication.domain.binding.ValidatorBinding.java
com.example.jens.myapplication.domain.converter.ErrorConverter.java
com.example.jens.myapplication.domain.converter.MyStringParser.java
com.example.jens.myapplication.domain.converter.UnixDateTimeConverter.java
com.example.jens.myapplication.domain.validator.PersonValidator.java
com.example.jens.myapplication.domain.validator.UserValidator.java
com.example.jens.myapplication.domain.validator.Validator.java
com.example.jens.myapplication.home.AanbevolenAdapter.java
com.example.jens.myapplication.home.HomeFragment.java
com.example.jens.myapplication.home.ImageSlideAdapter.java
com.example.jens.myapplication.home.Reclame.java
com.example.jens.myapplication.lib.RangeSeekBar.java
com.example.jens.myapplication.main.MainActivity.java
com.example.jens.myapplication.main.NavDrawerAdapter.java
com.example.jens.myapplication.main.NavDrawerItem.java
com.example.jens.myapplication.main.NavMenuItem.java
com.example.jens.myapplication.main.NavMenuSection.java
com.example.jens.myapplication.sam.JoetzApplication.java
com.example.jens.myapplication.sam.MyFragment.java
com.example.jens.myapplication.sam.MySaveableFragment.java
com.example.jens.myapplication.util.AESEncryption.java
com.example.jens.myapplication.util.ActivityUtils.java
com.example.jens.myapplication.util.BindingUtils.java
com.example.jens.myapplication.util.DateTimeStringConverter.java
com.example.jens.myapplication.util.JsonUtils.java
com.example.jens.myapplication.util.MyMonths.java
com.example.jens.myapplication.util.StringUtils.java
com.example.jens.myapplication.view.MyLinearLayout.java