Skip to content
This repository has been archived by the owner on Dec 15, 2020. It is now read-only.

Commit

Permalink
Fix focus bugs and icon bug by using stable IDs.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dektar committed Nov 11, 2016
1 parent 5958666 commit fc5af1d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,6 +79,21 @@ private ExpandableServiceAdapter(@NonNull List<ServiceParentListItem> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit fc5af1d

Please sign in to comment.