From 43e8639e4e39defa2c6e5b0743a74cdb572adb2b Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Wed, 24 Apr 2013 13:32:32 +0900 Subject: [PATCH] [PB3] Simplify storage By looping through children, there is no need to remember the last index. Bug: 7600384 Change-Id: I8b42b0aa38874ee494c6c4ecc09194a1d3c16922 --- .../dictionarypack/WordListPreference.java | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java b/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java index a9301e254..48d8b144c 100644 --- a/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java +++ b/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java @@ -43,7 +43,6 @@ public final class WordListPreference extends Preference { // What to display in the "status" field when we receive unknown data as a status from // the content provider. Empty string sounds sensible. static final private String NO_STATUS_MESSAGE = ""; - static final private int NOT_AN_INDEX = -1; /// Actions static final private int ACTION_UNKNOWN = 0; @@ -66,7 +65,6 @@ public final class WordListPreference extends Preference { static final private int ANIMATION_IN = 1; static final private int ANIMATION_OUT = 2; - private static int sLastClickedIndex = NOT_AN_INDEX; private static String sLastClickedWordlistId = null; private final OnWordListPreferenceClick mPreferenceClickHandler = new OnWordListPreferenceClick(); @@ -202,40 +200,38 @@ public final class WordListPreference extends Preference { private class OnWordListPreferenceClick implements View.OnClickListener { @Override public void onClick(final View v) { - final Button button = (Button)v.findViewById(R.id.wordlist_button); + // Note : v is the preference view final ViewParent parent = v.getParent(); // Just in case something changed in the framework, test for the concrete class if (!(parent instanceof ListView)) return; final ListView listView = (ListView)parent; - final int myIndex = listView.indexOfChild(v) + listView.getFirstVisiblePosition(); - if (NOT_AN_INDEX != sLastClickedIndex) { - // If another button is showing, hide it - animateButton(getButtonForIndex(listView, sLastClickedIndex), ANIMATION_OUT); - } + final int indexToOpen; if (sLastClickedWordlistId == mWordlistId) { // This button was being shown. Clear last state to record that there isn't a - // displayed button any more. - sLastClickedIndex = NOT_AN_INDEX; + // displayed button any more, and note that we don't want to open any button. sLastClickedWordlistId = null; + indexToOpen = -1; } else { - // This button was not being shown. Show it and mark it as the latest selected - // button. - animateButton(button, ANIMATION_IN); - sLastClickedIndex = myIndex; + // This button was not being shown. Mark it as the latest selected button, and + // remember the index of this child as the one to open in the following loop. sLastClickedWordlistId = mWordlistId; + indexToOpen = listView.indexOfChild(v); + } + final int lastDisplayedIndex = + listView.getLastVisiblePosition() - listView.getFirstVisiblePosition(); + // The "lastDisplayedIndex" is actually displayed, hence the <= + for (int i = 0; i <= lastDisplayedIndex; ++i) { + final Button button = + (Button)listView.getChildAt(i).findViewById(R.id.wordlist_button); + if (i == indexToOpen) { + animateButton(button, ANIMATION_IN); + } else { + animateButton(button, ANIMATION_OUT); + } } } } - private Button getButtonForIndex(final ListView listView, final int index) { - final int indexInChildren = index - listView.getFirstVisiblePosition(); - if (indexInChildren < 0 || index > listView.getLastVisiblePosition()) { - // The view is offscreen. - return null; - } - return (Button)listView.getChildAt(indexInChildren).findViewById(R.id.wordlist_button); - } - private void animateButton(final Button button, final int direction) { if (null == button) return; final float outerX = ((View)button.getParent()).getWidth();