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.Fragment; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.webkit.WebView; 25 26 /** 27 * Fragment that displays a news article. 28 */ 29 public class ArticleFragment extends Fragment { 30 // The webview where we display the article (our only view) 31 WebView mWebView; 32 33 // The article we are to display 34 NewsArticle mNewsArticle = null; 35 36 // Parameterless constructor is needed by framework ArticleFragment()37 public ArticleFragment() { 38 super(); 39 } 40 41 /** 42 * Sets up the UI. It consists if a single WebView. 43 */ 44 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)45 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 46 mWebView = new WebView(getActivity()); 47 loadWebView(); 48 return mWebView; 49 } 50 51 /** 52 * Displays a particular article. 53 * 54 * @param article the article to display 55 */ displayArticle(NewsArticle article)56 public void displayArticle(NewsArticle article) { 57 mNewsArticle = article; 58 loadWebView(); 59 } 60 61 /** 62 * Loads article data into the webview. 63 * 64 * This method is called internally to update the webview's contents to the appropriate 65 * article's text. 66 */ loadWebView()67 void loadWebView() { 68 if (mWebView != null) { 69 mWebView.loadData(mNewsArticle == null ? "" : mNewsArticle.getBody(), "text/html", 70 "utf-8"); 71 } 72 } 73 } 74