This provides a robust way to keep the ListView item selection synchronized

This provides a robust way to keep the listview item

This preview shows page 25 - 31 out of 55 pages.

, updates the list to the current selected item. This provides a robust way to keep the ListView item selection synchronized with the currently displayed Fragment both when adding a new Fragment to the back stack and when removing one, such as when the user presses the Back button.
Image of page 25

Subscribe to view the full document.

Implementing a WebViewFragment Next, we create a custom WebViewFragment class called VeggieGardenWebViewFragment to host the blog entries related to each veggie. This Fragment class does little more than determine which blog entry URL to load and then load it in the WebView control. public class VeggieGardenWebViewFragment extends WebViewFragment { }
Image of page 26
Implementing a WebViewFragment (Cont’d) @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); String[] veggiesUrls = getResources() .getStringArray(R.array.veggieurls_array); int veggieUrlIndex = getShownIndex(); WebView webview = getWebView(); webview.setPadding(0, 0, 0, 0); webview.getSettings().setLoadWithOverviewMode(true); webview.getSettings().setUseWideViewPort(true); if (veggieUrlIndex != -1) { String veggieUrl = veggieUrls[veggieUrlIndex]; webview.loadUrl(veggieUrl); } else { String veggieUrl = “ -veggie- garden.” + “appspot.com/cherrytomatoes"; webview.loadUrl(veggieUrl); } } }
Image of page 27

Subscribe to view the full document.

Implementing a WebViewFragment (Cont’d) Most of the Fragment control’s initialization happens in the onActivityCreated() callback method so that we initialize the WebView only once. The default configuration of the WebView control doesn’t look so pretty, so we make some configuration changes, remove the padding around the control, and make some settings to make the browser fit nicely in the screen area provided. If we’ve received a request for a specific veggie to load, we look up the URL and load it; otherwise, we load the “default” front page of Andy’s Veggie Garden.
Image of page 28
Defining the Layout Files Now that you’ve implemented your Fragment classes, you can place them in the appropriate layout resource files. You’ll need to create two layout files: In landscape mode, you’ll want a single activity_simple_fragments.xml layout file to host both Fragment components. In portrait mode, you’ll want a comparable layout file that hosts only the ListFragment you implemented. The WebViewFragment you implemented will have a user interface generated at runtime.
Image of page 29

Subscribe to view the full document.

Defining the Layout Files (Cont’d) <LinearLayout xmlns:android="" android:orientation=“vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id=”@+id/toolbar” layout=”@layout/tool_bar” /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false"> <fragment android:name="com.androidintro.simplefragments.VeggieGardenListFragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <FrameLayout android:id="@+id/veggieentry" android:layout_weight="4" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout>
Image of page 30
Image of page 31
  • Summer '16

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes