From 5baae79db1c86ece0ced74698d6e4d1df7307766 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Mon, 21 Apr 2014 23:01:31 +0200 Subject: [PATCH] WeatherInformation: new design with tabs --- AndroidManifest.xml | 18 +- project.properties | 2 +- res/layout/fragment_pager.xml | 14 ++ res/layout/fragment_pager_list.xml | 38 +++ res/menu/weather_main_menu.xml | 12 - .../exampletdd/WeatherInformationActivity.java | 19 +- src/de/example/exampletdd/WeatherTabsActivity.java | 264 +++++++++++++++++++++ .../WeatherInformationCurrentDataFragment.java | 35 ++- .../WeatherInformationOverviewFragment.java | 56 ++--- 9 files changed, 384 insertions(+), 74 deletions(-) create mode 100644 res/layout/fragment_pager.xml create mode 100644 res/layout/fragment_pager_list.xml create mode 100644 src/de/example/exampletdd/WeatherTabsActivity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index f8e2e96..21c1fc1 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -31,6 +31,7 @@ android:label="@string/app_name" android:logo="@drawable/ic_launcher" android:theme="@style/AppTheme" > + + + + + + + + + android:parentActivityName="de.example.exampletdd.WeatherTabsActivity" > @@ -52,7 +64,7 @@ + android:parentActivityName="de.example.exampletdd.WeatherTabsActivity" > @@ -61,7 +73,7 @@ + android:parentActivityName="de.example.exampletdd.WeatherTabsActivity" > diff --git a/project.properties b/project.properties index 7612c3e..770d261 100644 --- a/project.properties +++ b/project.properties @@ -12,5 +12,5 @@ # Project target. target=android-18 -android.library.reference.1=../../../../android/android-sdk-linux/extras/google/google_play_services/libproject/google-play-services_lib +android.library.reference.1=../../../../../ALLGEMEINS/ALLGEMEIN/Android/android-sdk-linux/extras/google/google_play_services/libproject/google-play-services_lib android.library=false diff --git a/res/layout/fragment_pager.xml b/res/layout/fragment_pager.xml new file mode 100644 index 0000000..07cf7a6 --- /dev/null +++ b/res/layout/fragment_pager.xml @@ -0,0 +1,14 @@ + + + + + + + diff --git a/res/layout/fragment_pager_list.xml b/res/layout/fragment_pager_list.xml new file mode 100644 index 0000000..e4a1c55 --- /dev/null +++ b/res/layout/fragment_pager_list.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + diff --git a/res/menu/weather_main_menu.xml b/res/menu/weather_main_menu.xml index 26ff96f..9796222 100644 --- a/res/menu/weather_main_menu.xml +++ b/res/menu/weather_main_menu.xml @@ -37,18 +37,6 @@ android:title="@string/action_map" android:titleCondensed="@string/action_map"> - - diff --git a/src/de/example/exampletdd/WeatherInformationActivity.java b/src/de/example/exampletdd/WeatherInformationActivity.java index de41348..b8491d3 100644 --- a/src/de/example/exampletdd/WeatherInformationActivity.java +++ b/src/de/example/exampletdd/WeatherInformationActivity.java @@ -10,7 +10,6 @@ import android.preference.PreferenceManager; import android.view.Menu; import android.view.MenuItem; import de.example.exampletdd.activityinterface.GetWeather; -import de.example.exampletdd.fragment.overview.WeatherInformationOverviewFragment; import de.example.exampletdd.model.GeocodingData; import de.example.exampletdd.service.WeatherServicePersistenceFile; @@ -37,10 +36,10 @@ public class WeatherInformationActivity extends Activity { // this.getFragmentManager().beginTransaction() // .add(R.id.container, weatherDataFragment).commit(); // } - final WeatherInformationOverviewFragment weatherOverviewFragment = (WeatherInformationOverviewFragment) this - .getFragmentManager().findFragmentById(R.id.weather_overview_fragment); + // final WeatherInformationOverviewFragment weatherOverviewFragment = (WeatherInformationOverviewFragment) this + // .getFragmentManager().findFragmentById(R.id.weather_overview_fragment); - this.mGetWeather = weatherOverviewFragment; + // this.mGetWeather = weatherOverviewFragment; } @@ -76,12 +75,12 @@ public class WeatherInformationActivity extends Activity { "de.example.exampletdd.WeatherInformationMapActivity")); this.startActivity(intent); return true; - } else if (itemId == R.id.weather_menu_current) { - intent = new Intent("de.example.exampletdd.WEATHERINFO") - .setComponent(new ComponentName("de.example.exampletdd", - "de.example.exampletdd.WeatherInformationCurrentDataActivity")); - this.startActivity(intent); - return true; + // } else if (itemId == R.id.weather_menu_current) { + // intent = new Intent("de.example.exampletdd.WEATHERINFO") + // .setComponent(new ComponentName("de.example.exampletdd", + // "de.example.exampletdd.WeatherInformationCurrentDataActivity")); + // this.startActivity(intent); + // return true; } else { } diff --git a/src/de/example/exampletdd/WeatherTabsActivity.java b/src/de/example/exampletdd/WeatherTabsActivity.java new file mode 100644 index 0000000..11f54d9 --- /dev/null +++ b/src/de/example/exampletdd/WeatherTabsActivity.java @@ -0,0 +1,264 @@ +package de.example.exampletdd; + +import android.app.ActionBar; +import android.app.ActionBar.Tab; +import android.app.FragmentTransaction; +import android.content.ComponentName; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.app.ListFragment; +import android.support.v4.view.ViewPager; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.TextView; +import de.example.exampletdd.activityinterface.GetWeather; +import de.example.exampletdd.fragment.current.WeatherInformationCurrentDataFragment; +import de.example.exampletdd.fragment.overview.WeatherInformationOverviewFragment; +import de.example.exampletdd.model.GeocodingData; +import de.example.exampletdd.service.WeatherServicePersistenceFile; + +public class WeatherTabsActivity extends FragmentActivity { + static final int NUM_ITEMS = 2; + + MyAdapter mAdapter; + + ViewPager mPager; + + private GetWeather mGetWeather; + + @Override + protected void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.fragment_pager); + + mAdapter = new MyAdapter(getSupportFragmentManager()); + + mPager = (ViewPager)findViewById(R.id.pager); + mPager.setAdapter(mAdapter); + + + mPager.setOnPageChangeListener( + new ViewPager.SimpleOnPageChangeListener() { + @Override + public void onPageSelected(final int position) { + // When swiping between pages, select the + // corresponding tab. + getActionBar().setSelectedNavigationItem(position); + } + }); + + + final ActionBar actionBar = getActionBar(); + + PreferenceManager.setDefaultValues(this, R.xml.weather_preferences, false); + + // Specify that tabs should be displayed in the action bar. + actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); + actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE); + actionBar.setDisplayHomeAsUpEnabled(true); + + // Create a tab listener that is called when the user changes tabs. + final ActionBar.TabListener tabListener = new ActionBar.TabListener() { + + @Override + public void onTabSelected(final Tab tab, final FragmentTransaction ft) { + // When the tab is selected, switch to the + // corresponding page in the ViewPager. + mPager.setCurrentItem(tab.getPosition()); + + } + + @Override + public void onTabUnselected(final Tab tab, final FragmentTransaction ft) { + // TODO Auto-generated method stub + + } + + @Override + public void onTabReselected(final Tab tab, final FragmentTransaction ft) { + // TODO Auto-generated method stub + + } + + }; + + actionBar.addTab(actionBar.newTab().setText("CURRENTLY").setTabListener(tabListener)); + actionBar.addTab(actionBar.newTab().setText("FORECAST").setTabListener(tabListener)); + + } + + @Override + public boolean onCreateOptionsMenu(final Menu menu) { + + this.getMenuInflater().inflate(R.menu.weather_main_menu, menu); + + return super.onCreateOptionsMenu(menu); + } + + @Override + public boolean onOptionsItemSelected(final MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + super.onOptionsItemSelected(item); + + Intent intent; + final int itemId = item.getItemId(); + if (itemId == R.id.weather_menu_settings) { + intent = new Intent("de.example.exampletdd.WEATHERINFO") + .setComponent(new ComponentName("de.example.exampletdd", + "de.example.exampletdd.WeatherInformationPreferencesActivity")); + this.startActivity(intent); + return true; + } else if (itemId == R.id.weather_menu_get) { + this.getWeather(); + return true; + } else if (itemId == R.id.weather_menu_map) { + intent = new Intent("de.example.exampletdd.WEATHERINFO") + .setComponent(new ComponentName("de.example.exampletdd", + "de.example.exampletdd.WeatherInformationMapActivity")); + this.startActivity(intent); + return true; + } else { + } + + return super.onOptionsItemSelected(item); + } + + + @Override + public void onResume() { + super.onResume(); + + final ActionBar actionBar = this.getActionBar(); + + final WeatherServicePersistenceFile weatherServicePersistenceFile = new WeatherServicePersistenceFile(this); + final GeocodingData geocodingData = weatherServicePersistenceFile.getGeocodingData(); + + if (geocodingData != null) { + final String city = (geocodingData.getCity() == null) ? this.getString(R.string.city_not_found) + : geocodingData.getCity(); + final String country = (geocodingData.getCountry() == null) ? this.getString(R.string.country_not_found) + : geocodingData.getCountry(); + actionBar.setTitle(city + "," + country); + } + + + final SharedPreferences sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(this); + final String keyPreference = this.getResources().getString( + R.string.weather_preferences_day_forecast_key); + final String value = sharedPreferences.getString(keyPreference, ""); + String humanValue = ""; + if (value.equals("5")) { + humanValue = "5-Day Forecast"; + } else if (value.equals("10")) { + humanValue = "10-Day Forecast"; + } else if (value.equals("14")) { + humanValue = "14-Day Forecast"; + } + actionBar.setSubtitle(humanValue); + + } + + + public void getWeather() { + this.mGetWeather.getRemoteWeatherInformation(); + } + + public class MyAdapter extends FragmentPagerAdapter { + public MyAdapter(final FragmentManager fm) { + super(fm); + } + + @Override + public int getCount() { + return NUM_ITEMS; + } + + @Override + public Fragment getItem(final int position) { + if (position == 0) { + return new WeatherInformationCurrentDataFragment(); + } else { + final WeatherInformationOverviewFragment fragment = new WeatherInformationOverviewFragment(); + WeatherTabsActivity.this.mGetWeather = fragment; + return fragment; + } + + } + } + + public static class ArrayListFragment extends ListFragment { + int mNum; + + /** + * Create a new instance of CountingFragment, providing "num" + * as an argument. + */ + static ArrayListFragment newInstance(final int num) { + final ArrayListFragment f = new ArrayListFragment(); + + // Supply num input as an argument. + final Bundle args = new Bundle(); + args.putInt("num", num); + f.setArguments(args); + + return f; + } + + /** + * When creating, retrieve this instance's number from its arguments. + */ + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mNum = getArguments() != null ? getArguments().getInt("num") : 1; + } + + /** + * The Fragment's UI is just a simple text view showing its + * instance number. + */ + @Override + public View onCreateView(final LayoutInflater inflater, final ViewGroup container, + final Bundle savedInstanceState) { + final View v = inflater.inflate(R.layout.fragment_pager_list, container, false); + final View tv = v.findViewById(R.id.text); + ((TextView)tv).setText("Fragment #" + mNum); + return v; + } + + @Override + public void onActivityCreated(final Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + final String[] chesses = new String[5]; + chesses[0] = "cheese 1"; + chesses[1] = "cheese 2"; + chesses[2] = "cheese 3"; + chesses[3] = "cheese 4"; + chesses[4] = "cheese 5"; + setListAdapter(new ArrayAdapter(getActivity(), + android.R.layout.simple_list_item_1, chesses)); + } + + @Override + public void onListItemClick(final ListView l, final View v, final int position, final long id) { + Log.i("FragmentList", "Item clicked: " + id); + } + } + + +} diff --git a/src/de/example/exampletdd/fragment/current/WeatherInformationCurrentDataFragment.java b/src/de/example/exampletdd/fragment/current/WeatherInformationCurrentDataFragment.java index b60121a..00a03a1 100644 --- a/src/de/example/exampletdd/fragment/current/WeatherInformationCurrentDataFragment.java +++ b/src/de/example/exampletdd/fragment/current/WeatherInformationCurrentDataFragment.java @@ -15,7 +15,6 @@ import java.util.Locale; import org.apache.http.client.ClientProtocolException; import android.app.DialogFragment; -import android.app.ListFragment; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -23,13 +22,13 @@ import android.net.http.AndroidHttpClient; import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; +import android.support.v4.app.ListFragment; import android.util.Log; import android.widget.ListView; import com.fasterxml.jackson.core.JsonParseException; import de.example.exampletdd.R; -import de.example.exampletdd.fragment.ErrorDialogFragment; import de.example.exampletdd.fragment.ProgressDialogFragment; import de.example.exampletdd.fragment.overview.IconsList; import de.example.exampletdd.httpclient.CustomHTTPClient; @@ -68,9 +67,9 @@ public class WeatherInformationCurrentDataFragment extends ListFragment { try { this.mWeatherServicePersistenceFile.storeCurrentWeatherData(currentWeatherData); } catch (final IOException e) { - final DialogFragment newFragment = ErrorDialogFragment - .newInstance(R.string.error_dialog_generic_error); - newFragment.show(this.getFragmentManager(), "errorDialog"); + // final DialogFragment newFragment = ErrorDialogFragment + // .newInstance(R.string.error_dialog_generic_error); + // newFragment.show(this.getFragmentManager(), "errorDialog"); } } } @@ -299,18 +298,18 @@ public class WeatherInformationCurrentDataFragment extends ListFragment { } catch (final IOException e) { WeatherInformationCurrentDataFragment.this.setListShown(true); Log.e(TAG, "WeatherTask onPostExecute exception: ", e); - final DialogFragment newFragment = ErrorDialogFragment - .newInstance(R.string.error_dialog_generic_error); - newFragment.show( - WeatherInformationCurrentDataFragment.this.getFragmentManager(), - "errorDialog"); + // final DialogFragment newFragment = ErrorDialogFragment + // .newInstance(R.string.error_dialog_generic_error); + // newFragment.show( + // WeatherInformationCurrentDataFragment.this.getFragmentManager(), + // "errorDialog"); } } else { WeatherInformationCurrentDataFragment.this.setListShown(true); - final DialogFragment newFragment = ErrorDialogFragment - .newInstance(R.string.error_dialog_generic_error); - newFragment.show(WeatherInformationCurrentDataFragment.this.getFragmentManager(), - "errorDialog"); + // final DialogFragment newFragment = ErrorDialogFragment + // .newInstance(R.string.error_dialog_generic_error); + // newFragment.show(WeatherInformationCurrentDataFragment.this.getFragmentManager(), + // "errorDialog"); } } @@ -318,10 +317,10 @@ public class WeatherInformationCurrentDataFragment extends ListFragment { protected void onCancelled(final CurrentWeatherData currentWeatherData) { this.weatherHTTPClient.close(); - final DialogFragment newFragment = ErrorDialogFragment - .newInstance(R.string.error_dialog_connection_tiemout); - newFragment.show(WeatherInformationCurrentDataFragment.this.getFragmentManager(), - "errorDialog"); + // final DialogFragment newFragment = ErrorDialogFragment + // .newInstance(R.string.error_dialog_connection_tiemout); + // newFragment.show(WeatherInformationCurrentDataFragment.this.getFragmentManager(), + // "errorDialog"); } private CurrentWeatherData doInBackgroundThrowable(final Object... params) diff --git a/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java b/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java index 088b684..8168e23 100644 --- a/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java +++ b/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java @@ -17,9 +17,6 @@ import java.util.Locale; import org.apache.http.client.ClientProtocolException; import android.app.DialogFragment; -import android.app.ListFragment; -import android.content.ComponentName; -import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -28,6 +25,7 @@ import android.os.AsyncTask; import android.os.Bundle; import android.os.Parcelable; import android.preference.PreferenceManager; +import android.support.v4.app.ListFragment; import android.util.Log; import android.view.View; import android.widget.ListView; @@ -36,9 +34,7 @@ import com.fasterxml.jackson.core.JsonParseException; import de.example.exampletdd.R; import de.example.exampletdd.activityinterface.GetWeather; -import de.example.exampletdd.fragment.ErrorDialogFragment; import de.example.exampletdd.fragment.ProgressDialogFragment; -import de.example.exampletdd.fragment.specific.WeatherInformationSpecificDataFragment; import de.example.exampletdd.httpclient.CustomHTTPClient; import de.example.exampletdd.model.GeocodingData; import de.example.exampletdd.model.forecastweather.ForecastWeatherData; @@ -84,9 +80,9 @@ public class WeatherInformationOverviewFragment extends ListFragment implements this.mWeatherServicePersistenceFile .storeForecastWeatherData(forecastWeatherData); } catch (final IOException e) { - final DialogFragment newFragment = ErrorDialogFragment - .newInstance(R.string.error_dialog_generic_error); - newFragment.show(this.getFragmentManager(), "errorDialog"); + // final DialogFragment newFragment = ErrorDialogFragment + // .newInstance(R.string.error_dialog_generic_error); + // newFragment.show(this.getFragmentManager(), "errorDialog"); } } @@ -108,19 +104,19 @@ public class WeatherInformationOverviewFragment extends ListFragment implements @Override public void onListItemClick(final ListView l, final View v, final int position, final long id) { - final WeatherInformationSpecificDataFragment fragment = (WeatherInformationSpecificDataFragment) this.getFragmentManager() - .findFragmentById(R.id.weather_specific_data__fragment); - if (fragment == null) { - // handset layout - final Intent intent = new Intent("de.example.exampletdd.WEATHERINFO"). - setComponent(new ComponentName("de.example.exampletdd", - "de.example.exampletdd.WeatherInformationSpecificDataActivity")); - intent.putExtra("CHOSEN_DAY", (int) id); - WeatherInformationOverviewFragment.this.getActivity().startActivity(intent); - } else { - // tablet layout - fragment.getWeatherByDay((int) id); - } + // final WeatherInformationSpecificDataFragment fragment = (WeatherInformationSpecificDataFragment) this.getFragmentManager() + // .findFragmentById(R.id.weather_specific_data__fragment); + // if (fragment == null) { + // // handset layout + // final Intent intent = new Intent("de.example.exampletdd.WEATHERINFO"). + // setComponent(new ComponentName("de.example.exampletdd", + // "de.example.exampletdd.WeatherInformationSpecificDataActivity")); + // intent.putExtra("CHOSEN_DAY", (int) id); + // WeatherInformationOverviewFragment.this.getActivity().startActivity(intent); + // } else { + // // tablet layout + // fragment.getWeatherByDay((int) id); + // } } @Override @@ -322,14 +318,14 @@ public class WeatherInformationOverviewFragment extends ListFragment implements this.onPostExecuteThrowable(weatherData); } catch (final IOException e) { Log.e(TAG, "WeatherTask onPostExecute exception: ", e); - final DialogFragment newFragment = ErrorDialogFragment - .newInstance(R.string.error_dialog_generic_error); - newFragment.show(WeatherInformationOverviewFragment.this.getFragmentManager(), "errorDialog"); + // final DialogFragment newFragment = ErrorDialogFragment + // .newInstance(R.string.error_dialog_generic_error); + // newFragment.show(WeatherInformationOverviewFragment.this.getFragmentManager(), "errorDialog"); } } else { - final DialogFragment newFragment = ErrorDialogFragment - .newInstance(R.string.error_dialog_generic_error); - newFragment.show(WeatherInformationOverviewFragment.this.getFragmentManager(), "errorDialog"); + // final DialogFragment newFragment = ErrorDialogFragment + // .newInstance(R.string.error_dialog_generic_error); + // newFragment.show(WeatherInformationOverviewFragment.this.getFragmentManager(), "errorDialog"); } } @@ -337,9 +333,9 @@ public class WeatherInformationOverviewFragment extends ListFragment implements protected void onCancelled(final ForecastWeatherData weatherData) { this.weatherHTTPClient.close(); - final DialogFragment newFragment = ErrorDialogFragment - .newInstance(R.string.error_dialog_connection_tiemout); - newFragment.show(WeatherInformationOverviewFragment.this.getFragmentManager(), "errorDialog"); + // final DialogFragment newFragment = ErrorDialogFragment + // .newInstance(R.string.error_dialog_connection_tiemout); + // newFragment.show(WeatherInformationOverviewFragment.this.getFragmentManager(), "errorDialog"); } private ForecastWeatherData doInBackgroundThrowable(final Object... params) -- 2.1.4