1 /* 2 * Copyright (C) 2011 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 17 package com.example.android.newsreader; 18 19 import android.os.Bundle; 20 import android.support.v4.app.ListFragment; 21 import android.view.View; 22 import android.widget.AdapterView; 23 import android.widget.AdapterView.OnItemClickListener; 24 import android.widget.ArrayAdapter; 25 import android.widget.ListView; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * Fragment that displays the news headlines for a particular news category. 32 * 33 * This Fragment displays a list with the news headlines for a particular news category. 34 * When an item is selected, it notifies the configured listener that a headlines was selected. 35 */ 36 public class HeadlinesFragment extends ListFragment implements OnItemClickListener { 37 // The list of headlines that we are displaying 38 List<String> mHeadlinesList = new ArrayList<String>(); 39 40 // The list adapter for the list we are displaying 41 ArrayAdapter<String> mListAdapter; 42 43 // The listener we are to notify when a headline is selected 44 OnHeadlineSelectedListener mHeadlineSelectedListener = null; 45 46 /** 47 * Represents a listener that will be notified of headline selections. 48 */ 49 public interface OnHeadlineSelectedListener { 50 /** 51 * Called when a given headline is selected. 52 * @param index the index of the selected headline. 53 */ onHeadlineSelected(int index)54 public void onHeadlineSelected(int index); 55 } 56 57 /** 58 * Default constructor required by framework. 59 */ HeadlinesFragment()60 public HeadlinesFragment() { 61 super(); 62 } 63 64 @Override onStart()65 public void onStart() { 66 super.onStart(); 67 setListAdapter(mListAdapter); 68 getListView().setOnItemClickListener(this); 69 loadCategory(0); 70 } 71 72 @Override onCreate(Bundle savedInstanceState)73 public void onCreate(Bundle savedInstanceState) { 74 super.onCreate(savedInstanceState); 75 mListAdapter = new ArrayAdapter<String>(getActivity(), R.layout.headline_item, 76 mHeadlinesList); 77 } 78 79 /** 80 * Sets the listener that should be notified of headline selection events. 81 * @param listener the listener to notify. 82 */ setOnHeadlineSelectedListener(OnHeadlineSelectedListener listener)83 public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener listener) { 84 mHeadlineSelectedListener = listener; 85 } 86 87 /** 88 * Load and display the headlines for the given news category. 89 * @param categoryIndex the index of the news category to display. 90 */ loadCategory(int categoryIndex)91 public void loadCategory(int categoryIndex) { 92 mHeadlinesList.clear(); 93 int i; 94 NewsCategory cat = NewsSource.getInstance().getCategory(categoryIndex); 95 for (i = 0; i < cat.getArticleCount(); i++) { 96 mHeadlinesList.add(cat.getArticle(i).getHeadline()); 97 } 98 mListAdapter.notifyDataSetChanged(); 99 } 100 101 /** 102 * Handles a click on a headline. 103 * 104 * This causes the configured listener to be notified that a headline was selected. 105 */ 106 @Override onItemClick(AdapterView<?> parent, View view, int position, long id)107 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 108 if (null != mHeadlineSelectedListener) { 109 mHeadlineSelectedListener.onHeadlineSelected(position); 110 } 111 } 112 113 /** Sets choice mode for the list 114 * 115 * @param selectable whether list is to be selectable. 116 */ setSelectable(boolean selectable)117 public void setSelectable(boolean selectable) { 118 if (selectable) { 119 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 120 } 121 else { 122 getListView().setChoiceMode(ListView.CHOICE_MODE_NONE); 123 } 124 } 125 } 126