Java tutorial
/********************************************************************** * Copyright (c) 2015 Luka Kunic, "ItemListFragment.java" * * 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. * * Author: lkunic * Last modified: 16/01/2015 **********************************************************************/ package com.lkunic.lib.activityaddonlib.twopane.fragments; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import com.lkunic.lib.activityaddonlib.R; /** * A list fragment representing a list of Items. * Activities containing this fragment MUST implement the {@link Callbacks} interface. */ public abstract class ItemListFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_item_list, container, false); // Setup the item list ListView list = (ListView) view.findViewById(R.id.item_list); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mCallbacks.onItemSelected(id); } }); // Call to the abstract method that is used by a derived class to add content to the list setupListContent(list); return view; } @Override public void onAttach(Activity activity) { super.onAttach(activity); // Activities containing this fragment must implement its callback if (!(activity instanceof Callbacks)) { throw new IllegalStateException( String.format("%s must implement fragment's callbacks.", activity.getClass().getName())); } mCallbacks = (Callbacks) activity; } @Override public void onDetach() { super.onDetach(); // Reset the active callback interface to the dummy implementation mCallbacks = sDummyCallbacks; } /**************************************************************************************************/ /**** Abstract methods ****/ /**************************************************************************************************/ /** * Implement to set up list adapter and content. You can extend the ItemListCursorAdapter and use * it for populating your list using a cursor. */ protected abstract void setupListContent(View list); /**************************************************************************************************/ /**** Callback implementation ****/ /**************************************************************************************************/ // The fragment's current callback object, which is notified of list item clicks private Callbacks mCallbacks = sDummyCallbacks; /** * A callback interface that all activities containing this fragment must implement. This mechanism allows * activities to be notified of item selections. */ public interface Callbacks { /** * Callback for when an item has been selected. */ public void onItemSelected(long id); } /** * Returns the callback object. * @return */ protected Callbacks getCallbacks() { return mCallbacks; } /** * A dummy implementation of the {@link Callbacks} interface that does * nothing. Used only when this fragment is not attached to an activity. */ private static Callbacks sDummyCallbacks = new Callbacks() { @Override public void onItemSelected(long id) { Log.e("ItemListFragment", "Dummy callback method called. Something is wrong with the implementation"); } }; }