Home | History | Annotate | Download | only in fragments
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.example.android.fragments;
     17 
     18 import android.app.Activity;
     19 import android.os.Bundle;
     20 import android.support.v4.app.ListFragment;
     21 import android.view.View;
     22 import android.widget.ArrayAdapter;
     23 import android.widget.ListView;
     24 
     25 public class HeadlinesFragment extends ListFragment {
     26     OnHeadlineSelectedListener mCallback;
     27 
     28     // The container Activity must implement this interface so the frag can deliver messages
     29     public interface OnHeadlineSelectedListener {
     30         /** Called by HeadlinesFragment when a list item is selected */
     31         public void onArticleSelected(int position);
     32     }
     33 
     34     @Override
     35     public void onCreate(Bundle savedInstanceState) {
     36         super.onCreate(savedInstanceState);
     37 
     38         // We need to use a different list item layout for devices older than Honeycomb
     39         int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
     40                 android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
     41 
     42         // Create an array adapter for the list view, using the Ipsum headlines array
     43         setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));
     44     }
     45 
     46     @Override
     47     public void onStart() {
     48         super.onStart();
     49 
     50         // When in two-pane layout, set the listview to highlight the selected list item
     51         // (We do this during onStart because at the point the listview is available.)
     52         if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) {
     53             getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
     54         }
     55     }
     56 
     57     @Override
     58     public void onAttach(Activity activity) {
     59         super.onAttach(activity);
     60 
     61         // This makes sure that the container activity has implemented
     62         // the callback interface. If not, it throws an exception.
     63         try {
     64             mCallback = (OnHeadlineSelectedListener) activity;
     65         } catch (ClassCastException e) {
     66             throw new ClassCastException(activity.toString()
     67                     + " must implement OnHeadlineSelectedListener");
     68         }
     69     }
     70 
     71     @Override
     72     public void onListItemClick(ListView l, View v, int position, long id) {
     73         // Notify the parent activity of selected item
     74         mCallback.onArticleSelected(position);
     75 
     76         // Set the item as checked to be highlighted when in two-pane layout
     77         getListView().setItemChecked(position, true);
     78     }
     79 }