com.ontheroad.www.googlefragmentsimple.HeadlinesFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.ontheroad.www.googlefragmentsimple.HeadlinesFragment.java

Source

/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * 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 com.ontheroad.www.googlefragmentsimple;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // The container Activity must implement this interface so the frag can deliver messages
    //ActivityFragment?
    public interface OnHeadlineSelectedListener {
        /** Called by HeadlinesFragment when a list item is selected */
        public void onArticleSelected(int position);
    }

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

        // We need to use a different list item layout for devices older than Honeycomb
        int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
                ? android.R.layout.simple_list_item_activated_1
                : android.R.layout.simple_list_item_1;
        // Create an array adapter for the list view, using the Ipsum headlines array
        //ArrayAdapter(Context context, int resource, int textViewResourceId)
        setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));
        Log.i("TAG", "HeadlinesFragment onCreate");
    }

    @Override
    public void onStart() {
        super.onStart();
        //the choice mode has been set to CHOICE_MODE_SINGLE
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

    //Actifity onCreate?? Fragment
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.i("TAG", "HeadlinesFragment onAttach:" + activity.getComponentName().toString());
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            //MainActivity
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Notify the parent activity of selected item
        mCallback.onArticleSelected(position);

        // Set the item as checked to be highlighted when in two-pane layout
        //Sets the checked state of the specified position. The is only valid if the choice mode has been set to CHOICE_MODE_SINGLE or CHOICE_MODE_MULTIPLE.
        getListView().setItemChecked(position, true);
    }
}