Android Application Development: A Beginner's Tutorial by Kurniawan Budi

Android Application Development: A Beginner's Tutorial by Kurniawan Budi

Author:Kurniawan, Budi [Kurniawan, Budi]
Language: eng
Format: azw3, mobi, epub
Publisher: Brainy Software
Published: 2015-01-07T05:00:00+00:00


Extending ListFragment and Using FragmentManager

FragmentDemo1 showed how you could add a fragment to an activity using the fragment element in the activity’s layout file. In the second sample application, FragmentDemo2, you will learn how to add a fragment to an activity programmatically.

FragmentDemo2 is similar in functionality to its predecessor with a few differences. The first difference pertains to how the name and the picture of a selected city are updated. In FragmentDemo1, the containing activity calls the showDetails method in the second fragment, passing the city name. In FragmentDemo2, when a city is selected, the activity creates a new instance of DetailsFragment and uses it to replace the old instance.

The second difference is the fact that the first fragment extends ListFragment instead of Fragment. ListFragment is a subclass of Fragment and contains a ListView that fills its entire view. When subclassing ListFragment, you should override its onCreate method and call its setListAdapter method. This is demonstrated in the NamesListFragment class in Listing 13.8.

Listing 13.8: The NamesListFragment class

package com.example.fragmentdemo2; import android.app.Activity; import android.app.ListFragment; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; /* we don't need fragment_names-xml anymore */ public class NamesListFragment extends ListFragment { final String[] names = {"Amsterdam", "Brussels", "Paris"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayAdapter<String> adapter = new ArrayAdapter<String>( getActivity(), android.R.layout.simple_list_item_activated_1, names); setListAdapter(adapter); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { // ListView can only be accessed here, not in onCreate() super.onViewCreated(view, savedInstanceState); ListView listView = getListView(); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, final View view, int position, long id) { if (callback != null) { callback.onItemSelected(names[position]); } } }); } public interface Callback { public void onItemSelected(String id); } private Callback callback; @Override public void onAttach(Activity activity) { super.onAttach(activity); if (activity instanceof Callback) { callback = (Callback) activity; } } @Override public void onDetach() { super.onDetach(); callback = null; } }

Like the NamesFragment class in FragmentDemo1, the NamesListFragment class in FragmentDemo2 also defines a Callback interface that a containing activity must implement to listen to the ListView’s OnItemClick event.

The second fragment, DetailsFragment in Listing 13.9, expects its activity to pass two arguments, a name and an image ID. In its onCreate method, the fragment retrieves these arguments and store them in class level variables, name and imageId. The values of the variables are then used in its onCreateView method to populate its TextView and ImageView.

Listing 13.9: The DetailsFragment class

package com.example.fragmentdemo2; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import android.widget.TextView; public class DetailsFragment extends Fragment { int imageId; String name; public DetailsFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments().containsKey("name")) { name = getArguments().getString("name"); } if (getArguments().containsKey("imageId")) { imageId = getArguments().getInt("imageId"); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate( R.layout.fragment_details, container, false); TextView textView = (TextView) rootView.findViewById(R.id.text1); textView.setText(name); ImageView imageView = (ImageView) rootView.findViewById( R.id.imageView1); imageView.setScaleType(ScaleType.FIT_XY); //stretch image imageView.setImageResource(imageId); return rootView; } }

Now that you have looked at the fragments, take a close look at the activity.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.