From c919a3c99166c812a1aa91e7df5023b22265b2b1 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Tue, 27 Apr 2010 03:13:53 +0200 Subject: Clean up saving of view history. --- .../android/unicodeMap/UnicodeMapActivity.java | 85 ++++++++++++++-------- 1 file changed, 53 insertions(+), 32 deletions(-) (limited to 'src/org') diff --git a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java index 74dca91..a13f73a 100644 --- a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java +++ b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java @@ -27,15 +27,20 @@ public final class UnicodeMapActivity extends ListActivity { protected UnicodeDatabase db; - protected final List rootList - = new ArrayList(200); + protected class ViewHistory { + public final UnicodeArrayAdapter adapter; + public final int selPosition; + public final int yOffset; + public ViewHistory(UnicodeArrayAdapter adapter, + int selPosition, int yOffset) { + this.adapter = adapter; + this.selPosition = selPosition; + this.yOffset = yOffset; + } + } - protected final List stack - = new ArrayList(10); - protected final List positionStack - = new ArrayList(10); - protected final List positionYStack - = new ArrayList(10); + protected final List historyStack + = new ArrayList(10); EditText textForm; @@ -67,6 +72,37 @@ public final class UnicodeMapActivity extends ListActivity { thr.start(); } + protected void saveView(View selView, int selPosition) { + final ListView lv = getListView(); + lv.clearTextFilter(); + final UnicodeArrayAdapter adapter + = (UnicodeArrayAdapter) lv.getAdapter(); + final int yOffset; + if ( selView != null ) + yOffset = selView.getTop(); + else + yOffset = 0; + historyStack.add(new ViewHistory(adapter, selPosition, yOffset)); + } + + protected void saveView() { + final ListView lv = getListView(); + lv.clearTextFilter(); + final UnicodeArrayAdapter adapter + = (UnicodeArrayAdapter) lv.getAdapter(); + int position = lv.getSelectedItemPosition(); + int position0 = lv.getFirstVisiblePosition(); + if ( position == AdapterView.INVALID_POSITION ) + position = position0; + View view = lv.getChildAt(position-position0); + final int yOffset; + if ( view != null ) + yOffset = view.getTop(); + else + yOffset = 0; + historyStack.add(new ViewHistory(adapter, position, yOffset)); + } + protected class MapItemClickListener implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener { @@ -86,12 +122,7 @@ public final class UnicodeMapActivity extends ListActivity { UnicodeArrayAdapter adapter = new UnicodeArrayAdapter(UnicodeMapActivity.this, list); - stack.add(adapter); - positionStack.add(position); - if ( view != null ) - positionYStack.add(view.getTop()); - else - positionYStack.add(0); + saveView(view, position); setListAdapter(adapter); } else throw new AssertionError("unknown UnicodeDisplayable"); @@ -159,11 +190,12 @@ public final class UnicodeMapActivity extends ListActivity { final boolean needPopulate = db.needPopulate(); if ( needPopulate ) launchPopulation(); + List rootList + = new ArrayList(UnicodeCharacter.Range.values().length); for ( UnicodeCharacter.Range rng : UnicodeCharacter.Range.values() ) rootList.add(rng); UnicodeArrayAdapter rootAdapter = new UnicodeArrayAdapter(this, rootList); - stack.add(rootAdapter); setListAdapter(rootAdapter); final ClipboardManager cmgr = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); @@ -197,18 +229,7 @@ public final class UnicodeMapActivity extends ListActivity { UnicodeArrayAdapter adapter = new UnicodeArrayAdapter(UnicodeMapActivity.this, list); - stack.add(adapter); - final ListView lv = getListView(); - int position = lv.getSelectedItemPosition(); - int position0 = lv.getFirstVisiblePosition(); - if ( position == AdapterView.INVALID_POSITION ) - position = position0; - positionStack.add(position); - View view = lv.getChildAt(position-position0); - if ( view != null ) - positionYStack.add(view.getTop()); - else - positionYStack.add(0); + saveView(); setListAdapter(adapter); if ( overflowed ) { String str = getResources().getString(R.string.list_too_long); @@ -258,12 +279,12 @@ public final class UnicodeMapActivity extends ListActivity { @Override public boolean onKeyDown(int keyCode, KeyEvent event) { - if ( ( keyCode == KeyEvent.KEYCODE_BACK ) && stack.size() > 1 ) { + if ( ( keyCode == KeyEvent.KEYCODE_BACK ) + && historyStack.size() > 0 ) { final ListView lv = getListView(); - stack.remove(stack.size()-1); - setListAdapter(stack.get(stack.size()-1)); - lv.setSelectionFromTop(positionStack.remove(positionStack.size()-1), - positionYStack.remove(positionYStack.size()-1)); + ViewHistory saved = historyStack.remove(historyStack.size()-1); + setListAdapter(saved.adapter); + lv.setSelectionFromTop(saved.selPosition, saved.yOffset); return true; } else if ( keyCode == KeyEvent.KEYCODE_SEARCH ) { querySearch(); -- cgit v1.2.3