From b69abf9a38cdf98b6a8f34722660a8f1b7a3221f Mon Sep 17 00:00:00 2001 From: Doug Keen Date: Thu, 27 Sep 2012 08:56:27 -0700 Subject: [PATCH] Train selection now stays highlighted while action bar is showing (unusually difficult to pull off... wtf, Android SDK?) --- res/layout/departure_listing.xml | 4 +- res/values/colors.xml | 1 + .../activities/ViewDeparturesActivity.java | 39 ++++++++--------- .../controls/DepartureListItemLayout.java | 42 +++++++++++++++++++ .../bart/data/DepartureArrayAdapter.java | 8 ++-- 5 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 src/com/dougkeen/bart/controls/DepartureListItemLayout.java diff --git a/res/layout/departure_listing.xml b/res/layout/departure_listing.xml index 54f24c5..850129d 100644 --- a/res/layout/departure_listing.xml +++ b/res/layout/departure_listing.xml @@ -1,5 +1,5 @@ - @@ -60,4 +60,4 @@ android:layout_alignParentRight="true" android:layout_below="@id/topRow" /> - \ No newline at end of file + \ No newline at end of file diff --git a/res/values/colors.xml b/res/values/colors.xml index f65b2d8..ec67915 100644 --- a/res/values/colors.xml +++ b/res/values/colors.xml @@ -2,5 +2,6 @@ #FF000000 + #FF2A7998 \ No newline at end of file diff --git a/src/com/dougkeen/bart/activities/ViewDeparturesActivity.java b/src/com/dougkeen/bart/activities/ViewDeparturesActivity.java index 3a48ed0..0cea643 100644 --- a/src/com/dougkeen/bart/activities/ViewDeparturesActivity.java +++ b/src/com/dougkeen/bart/activities/ViewDeparturesActivity.java @@ -29,7 +29,7 @@ import android.view.View; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.ImageView; -import android.widget.ListAdapter; +import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; @@ -172,20 +172,19 @@ public class ViewDeparturesActivity extends SherlockFragmentActivity implements } } setListAdapter(mDeparturesAdapter); - getListView().setEmptyView(findViewById(android.R.id.empty)); - getListView().setOnItemClickListener( - new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView adapterView, - View view, int position, long id) { - mSelectedDeparture = (Departure) getListAdapter() - .getItem(position); - if (mActionMode != null) { - mActionMode.finish(); - } - startDepartureActionMode(); - } - }); + final ListView listView = getListView(); + listView.setEmptyView(findViewById(android.R.id.empty)); + listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView adapterView, View view, + int position, long id) { + mSelectedDeparture = (Departure) getListAdapter().getItem( + position); + view.setSelected(true); + startDepartureActionMode(); + } + }); findViewById(R.id.missingDepartureText).setVisibility(View.VISIBLE); @@ -275,9 +274,8 @@ public class ViewDeparturesActivity extends SherlockFragmentActivity implements + mDestination.name); } - @SuppressWarnings("unchecked") - private AdapterView getListView() { - return (AdapterView) findViewById(android.R.id.list); + private ListView getListView() { + return (ListView) findViewById(android.R.id.list); } private final ServiceConnection mConnection = new ServiceConnection() { @@ -487,7 +485,8 @@ public class ViewDeparturesActivity extends SherlockFragmentActivity implements } private void startDepartureActionMode() { - mActionMode = startActionMode(new DepartureActionMode()); + if (mActionMode == null) + mActionMode = startActionMode(new DepartureActionMode()); mActionMode.setTitle(mSelectedDeparture.getTrainDestinationName()); mActionMode.setSubtitle(mSelectedDeparture.getTrainLengthText()); } @@ -531,6 +530,8 @@ public class ViewDeparturesActivity extends SherlockFragmentActivity implements @Override public void onDestroyActionMode(ActionMode mode) { + getListView().clearChoices(); + getListView().requestLayout(); mActionMode = null; } diff --git a/src/com/dougkeen/bart/controls/DepartureListItemLayout.java b/src/com/dougkeen/bart/controls/DepartureListItemLayout.java new file mode 100644 index 0000000..79c30ae --- /dev/null +++ b/src/com/dougkeen/bart/controls/DepartureListItemLayout.java @@ -0,0 +1,42 @@ +package com.dougkeen.bart.controls; + +import com.dougkeen.bart.R; + +import android.content.Context; +import android.view.LayoutInflater; +import android.widget.Checkable; +import android.widget.RelativeLayout; + +public class DepartureListItemLayout extends RelativeLayout implements + Checkable { + + public DepartureListItemLayout(Context context) { + super(context); + LayoutInflater.from(context).inflate(R.layout.departure_listing, this, + true); + } + + private boolean mChecked; + + @Override + public boolean isChecked() { + return mChecked; + } + + @Override + public void setChecked(boolean checked) { + mChecked = checked; + if (isChecked()) { + setBackgroundDrawable(getContext().getResources().getDrawable( + R.color.blue_selection)); + } else { + setBackgroundDrawable(getContext().getResources().getDrawable( + android.R.color.transparent)); + } + } + + @Override + public void toggle() { + setChecked(!isChecked()); + } +} diff --git a/src/com/dougkeen/bart/data/DepartureArrayAdapter.java b/src/com/dougkeen/bart/data/DepartureArrayAdapter.java index 32a2b73..0af5679 100644 --- a/src/com/dougkeen/bart/data/DepartureArrayAdapter.java +++ b/src/com/dougkeen/bart/data/DepartureArrayAdapter.java @@ -13,6 +13,7 @@ import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.widget.ArrayAdapter; import android.widget.ImageView; +import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextSwitcher; import android.widget.TextView; @@ -20,6 +21,7 @@ import android.widget.ViewSwitcher.ViewFactory; import com.dougkeen.bart.R; import com.dougkeen.bart.controls.CountdownTextView; +import com.dougkeen.bart.controls.DepartureListItemLayout; import com.dougkeen.bart.controls.TimedTextSwitcher; import com.dougkeen.bart.model.Departure; import com.dougkeen.bart.model.TextProvider; @@ -58,11 +60,11 @@ public class DepartureArrayAdapter extends ArrayAdapter { @Override public View getView(int position, View convertView, ViewGroup parent) { View view; - if (convertView != null && convertView instanceof RelativeLayout) { + if (convertView != null + && convertView instanceof DepartureListItemLayout) { view = convertView; } else { - LayoutInflater inflater = LayoutInflater.from(getContext()); - view = inflater.inflate(R.layout.departure_listing, parent, false); + view = new DepartureListItemLayout(getContext()); } final Departure departure = getItem(position);