If you think the Android project arcgis-runtime-samples-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/* Copyright 2014 ESRI
*/*www.java2s.com*/
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
*
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
*
* See the Sample code usage restrictions document for further information.
*
*/package com.arcgis.android.samples.maps.fragmentmanagement;
import android.app.Activity;
import android.os.Bundle;
import android.app.ListFragment;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
* A fragment representing a list of basemaps. This fragment also supports a two-pane view by
* allowing list items to be given an 'activated' state upon selection. This helps indicate
* which item is currently being viewed in a {@link MapFragment}.
* Activities containing this fragment MUST implement the {@code BasemapListListener} interface.
*/publicclass BasemapListFragment extends ListFragment {
/** Fragment argument representing currently selected position in the list */publicstaticfinal String ARG_ACTIVATED_POSITION = "ActivatedPosition";
privatestaticfinal String KEY_ACTIVATED_POSITION = "ActivatedPosition";
private BasemapListListener mBasemapListListener = sDummyListener;
privateint mActivatedPosition = AdapterView.INVALID_POSITION;
/**
* A callback interface that all activities containing this fragment must implement.
* This mechanism allows activities to be notified of basemap selections.
*/publicinterface BasemapListListener {
/**
* Callback for when a basemap has been selected.
*
* @param position Position of selected basemap in list.
* @param id String identifier of selected basemap.
*/publicvoid onBasemapSelected(int position, String id);
}
/**
* A dummy implementation of the {@link BasemapListListener} interface that does nothing.
* Used only when this fragment is not attached to an activity.
*/privatestatic BasemapListListener sDummyListener = new BasemapListListener() {
@Override
publicvoid onBasemapSelected(int position, String id) {
}
};
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/public BasemapListFragment() {
}
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
// Retrieve arguments
if (mActivatedPosition == AdapterView.INVALID_POSITION && getArguments().containsKey(ARG_ACTIVATED_POSITION)) {
mActivatedPosition = getArguments().getInt(ARG_ACTIVATED_POSITION);
}
// Setup list adapter
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.map_types,
android.R.layout.simple_list_item_activated_1);
setListAdapter(adapter);
}
@Override
publicvoid onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Reinstate saved instance state (if any)
if (savedInstanceState != null && savedInstanceState.containsKey(KEY_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState.getInt(KEY_ACTIVATED_POSITION));
}
}
@Override
publicvoid onAttach(Activity activity) {
super.onAttach(activity);
// Check the host activity implements the mandatory callback listener.
if (!(activity instanceof BasemapListListener)) {
thrownew IllegalStateException("Activity must implement BasemapListListener");
}
mBasemapListListener = (BasemapListListener) activity;
}
@Override
publicvoid onDetach() {
super.onDetach();
// Reset the active listener interface to the dummy implementation
mBasemapListListener = sDummyListener;
}
@Override
publicvoid onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
// Notify the active listener interface (the activity, if the fragment is attached to one)
// that an item has been selected
CharSequence text = ((TextView) view).getText();
mBasemapListListener.onBasemapSelected(position, text.toString());
mActivatedPosition = position;
}
@Override
publicvoid onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mActivatedPosition != AdapterView.INVALID_POSITION) {
outState.putInt(KEY_ACTIVATED_POSITION, mActivatedPosition);
}
}
/**
* Turns on activate-on-click mode. When this mode is on, list items will be given the
* 'activated' state when touched.
*/publicvoid setActivateOnItemClick(boolean activateOnItemClick) {
getListView().setChoiceMode(activateOnItemClick ? AbsListView.CHOICE_MODE_SINGLE : AbsListView.CHOICE_MODE_NONE);
if (activateOnItemClick && mActivatedPosition != AdapterView.INVALID_POSITION) {
getListView().setItemChecked(mActivatedPosition, true);
}
}
/**
* Sets the activated position and highlights it in the list.
*
* @param position The activated position.
*/privatevoid setActivatedPosition(int position) {
if (position == AdapterView.INVALID_POSITION) {
getListView().setItemChecked(mActivatedPosition, false);
} else {
getListView().setItemChecked(position, true);
}
mActivatedPosition = position;
}
}