From fc5af1dc1b205c87e44fee377318de25edc8f477 Mon Sep 17 00:00:00 2001 From: Katie Dektar Date: Fri, 11 Nov 2016 11:50:38 -0800 Subject: [PATCH] Fix focus bugs and icon bug by using stable IDs. Uses stable IDs and disables the item animator per https://code.google.com/p/android/issues/detail?id=204277. BUG=32807420,32810660,32770320 Change-Id: Ia8f90d07dc1ccda94fd17875c53ab60f5f57ab8e --- .../CompositeRecyclerAdapter.java | 6 ++++++ .../ExpandableDeviceAdapter.java | 21 +++++++++++++++---- .../ExpandableServiceAdapter.java | 16 ++++++++++++++ .../ManageDevicesRecyclerFragment.java | 8 +++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/CompositeRecyclerAdapter.java b/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/CompositeRecyclerAdapter.java index 9c9aae99..7ef8376a 100644 --- a/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/CompositeRecyclerAdapter.java +++ b/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/CompositeRecyclerAdapter.java @@ -125,6 +125,12 @@ private int translate(int subIndex, int subPosition) { return position; } + @Override + public long getItemId(int position) { + scanTo(position); + return mSubAdapters[mLastSubAdapterIndex].getItemId(mLastSubPosition); + } + @Override public int getItemViewType(int position) { scanTo(position); diff --git a/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/ExpandableDeviceAdapter.java b/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/ExpandableDeviceAdapter.java index 1091ab0c..dcc77230 100644 --- a/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/ExpandableDeviceAdapter.java +++ b/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/ExpandableDeviceAdapter.java @@ -23,6 +23,7 @@ import android.view.ViewGroup; import com.bignerdranch.expandablerecyclerview.Model.ParentListItem; +import com.bignerdranch.expandablerecyclerview.Model.ParentWrapper; import com.google.android.apps.forscience.whistlepunk.R; import com.google.android.apps.forscience.whistlepunk.SensorAppearanceProvider; import com.google.android.apps.forscience.whistlepunk.SensorRegistry; @@ -66,6 +67,21 @@ private ExpandableDeviceAdapter(final ConnectableSensorRegistry registry, mSensorRegistry = sensorRegistry; } + @Override + public long getItemId(int position) { + Object item = getListItem(position); + long result; + if (item instanceof ParentWrapper) { + DeviceParentListItem parent = (DeviceParentListItem) + ((ParentWrapper) item).getParentListItem(); + result = parent.getSpec().getGlobalDeviceAddress().hashCode(); + } else { + // The item is a SensorKey string. + result = ((String) item).hashCode(); + } + return result; + } + @Override public DeviceParentViewHolder onCreateParentViewHolder( ViewGroup parentViewGroup) { @@ -108,13 +124,10 @@ public boolean hasSensorKey(String sensorKey) { public boolean addAvailableSensor(String sensorKey, ConnectableSensor sensor) { boolean isReplacement = mSensorMap.containsKey(sensorKey); if (isReplacement) { - ConnectableSensor previousSensor = mSensorMap.get(sensorKey); mSensorMap.put(sensorKey, sensor); int parentIndex = findParentIndex(sensorKey); if (parentIndex >= 0) { - if (!previousSensor.isUnchanged(sensor)) { - notifyChildItemChanged(findParentIndex(sensorKey), findChildIndex(sensorKey)); - } + notifyChildItemChanged(findParentIndex(sensorKey), findChildIndex(sensorKey)); return true; } } diff --git a/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/ExpandableServiceAdapter.java b/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/ExpandableServiceAdapter.java index 3f3e3ac5..f71f4b71 100644 --- a/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/ExpandableServiceAdapter.java +++ b/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/ExpandableServiceAdapter.java @@ -26,6 +26,7 @@ import android.widget.TextView; import com.bignerdranch.expandablerecyclerview.Model.ParentListItem; +import com.bignerdranch.expandablerecyclerview.Model.ParentWrapper; import com.bignerdranch.expandablerecyclerview.ViewHolder.ChildViewHolder; import com.google.android.apps.forscience.whistlepunk.R; import com.google.android.apps.forscience.whistlepunk.SensorAppearanceProvider; @@ -78,6 +79,21 @@ private ExpandableServiceAdapter(@NonNull List parentItem mAppearanceProvider = appearanceProvider; } + @Override + public long getItemId(int position) { + Object item = getListItem(position); + long result; + if (item instanceof ParentWrapper) { + ServiceParentListItem parent = (ServiceParentListItem) + ((ParentWrapper) item).getParentListItem(); + result = parent.getGlobalServiceId().hashCode(); + } else { + DeviceWithSensors device = (DeviceWithSensors) item; + result = (device.getSpec().getGlobalDeviceAddress() + "device").hashCode(); + } + return result; + } + @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); diff --git a/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/ManageDevicesRecyclerFragment.java b/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/ManageDevicesRecyclerFragment.java index 2f6de2ba..f17dd200 100644 --- a/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/ManageDevicesRecyclerFragment.java +++ b/OpenScienceJournal/whistlepunk_library/src/main/java/com/google/android/apps/forscience/whistlepunk/devicemanager/ManageDevicesRecyclerFragment.java @@ -18,9 +18,12 @@ import android.app.Fragment; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.SimpleItemAnimator; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -100,9 +103,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, } CompositeRecyclerAdapter adapter = new CompositeRecyclerAdapter(myHeader, mMyDevices, availableHeader, mAvailableDevices); + adapter.setHasStableIds(true); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager( new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false)); + // Don't animate on change: https://code.google.com/p/android/issues/detail?id=204277. + SimpleItemAnimator animator = new DefaultItemAnimator(); + animator.setSupportsChangeAnimations(false); + recyclerView.setItemAnimator(animator); return view; }