Java tutorial
/* * Copyright 2015 Aditya Amirullah. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package inforuh.eventfinder.ui.fragment; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.support.v4.content.LocalBroadcastManager; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import inforuh.eventfinder.R; import inforuh.eventfinder.io.Event; import inforuh.eventfinder.provider.Contract; import inforuh.eventfinder.sync.SyncAdapter; import inforuh.eventfinder.ui.DetailActivity; /** * Created by tioammar * on 8/11/15. */ public class PagerFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, EventAdapter.Listener, SwipeRefreshLayout.OnRefreshListener { private EventAdapter listAdapter; private String category; private SwipeRefreshLayout swipeLayout; private BroadcastReceiver receiver; public PagerFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_main, container, false); category = getArguments().getString("category"); TextView emptyView = (TextView) view.findViewById(R.id.empty_view); listAdapter = new EventAdapter(getActivity(), this, emptyView); RecyclerView rv = (RecyclerView) view.findViewById(R.id.event_list); rv.setLayoutManager(new LinearLayoutManager(rv.getContext())); rv.setAdapter(listAdapter); receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { swipeLayout.setRefreshing(false); } }; swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_layout); swipeLayout.setOnRefreshListener(this); return view; } @Override public void onRefresh() { swipeLayout.setRefreshing(true); SyncAdapter.startSync(getActivity()); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); getLoaderManager().initLoader(0, null, this); } @Override public void onResume() { super.onResume(); LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, new IntentFilter(SyncAdapter.ACTION_GET_TIMELINE_FINISH)); } @Override public void onPause() { super.onPause(); if (swipeLayout.isRefreshing()) swipeLayout.setRefreshing(false); LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { String selection = Contract.EventColumn.CATEGORY + " = ? "; String sortOrder = Contract.EventColumn.START_DATE + " DESC"; return new CursorLoader(getActivity(), Contract.EventColumn.CONTENT_URI, Event.PROJECTION, selection, new String[] { category }, sortOrder); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { if (data == null) return; listAdapter.swapCursor(data); } @Override public void onLoaderReset(Loader<Cursor> loader) { } @Override public void onClickListener(Uri uri) { Intent intent = new Intent(getActivity(), DetailActivity.class); intent.setData(uri); startActivity(intent); } }