Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.transitiontests; |
| 17 | |
| 18 | import android.app.Activity; |
| 19 | import android.content.Context; |
| 20 | import android.os.Bundle; |
| 21 | import android.view.View; |
| 22 | import android.view.ViewTreeObserver; |
| 23 | import android.view.transition.Fade; |
| 24 | import android.view.transition.Scene; |
| 25 | import android.widget.AdapterView; |
| 26 | import android.widget.ArrayAdapter; |
| 27 | import android.widget.LinearLayout; |
| 28 | import android.widget.ListView; |
| 29 | import android.widget.TextView; |
| 30 | import android.view.transition.AutoTransition; |
| 31 | import android.view.transition.Move; |
| 32 | import android.view.transition.Transition; |
| 33 | import android.view.transition.TransitionGroup; |
| 34 | import android.view.transition.TransitionManager; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 35 | |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.HashMap; |
| 38 | import java.util.List; |
| 39 | |
| 40 | public class ListViewAddRemove extends Activity { |
| 41 | |
| 42 | final ArrayList<String> numList = new ArrayList<String>(); |
| 43 | |
| 44 | @Override |
| 45 | public void onCreate(Bundle savedInstanceState) { |
| 46 | super.onCreate(savedInstanceState); |
| 47 | setContentView(R.layout.list_view_add_remove); |
| 48 | |
| 49 | final LinearLayout container = (LinearLayout) findViewById(R.id.container); |
| 50 | |
| 51 | final ListView listview = (ListView) findViewById(R.id.listview); |
| 52 | for (int i = 0; i < 200; ++i) { |
| 53 | numList.add(Integer.toString(i)); |
| 54 | } |
| 55 | final StableArrayAdapter adapter = new StableArrayAdapter(this, |
| 56 | android.R.layout.simple_list_item_1, numList); |
| 57 | listview.setAdapter(adapter); |
| 58 | |
| 59 | final ViewTreeObserver observer = container.getViewTreeObserver(); |
| 60 | observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { |
| 61 | public void onGlobalLayout() { |
| 62 | System.out.println("-------------------------------------"); |
| 63 | System.out.println("onLayoutListener: listview view tops: "); |
| 64 | for (int i = 0; i < listview.getChildCount(); ++i) { |
| 65 | TextView view = (TextView) listview.getChildAt(i); |
| 66 | System.out.println(" " + view.getText() + ": " + view.getTop()); |
| 67 | } |
| 68 | } |
| 69 | }); |
| 70 | |
| 71 | final Scene mySceneChanger = new Scene(listview); |
| 72 | |
| 73 | mySceneChanger.setEnterAction(new Runnable() { |
| 74 | @Override |
| 75 | public void run() { |
| 76 | numList.remove(mItemToDelete); |
| 77 | adapter.notifyDataSetChanged(); |
| 78 | } |
| 79 | }); |
| 80 | final Transition myTransition = new AutoTransition(); |
| 81 | final TransitionGroup noFadeIn = new TransitionGroup(TransitionGroup.SEQUENTIALLY); |
| 82 | Fade fadeIn = new Fade(Fade.IN); |
| 83 | fadeIn.setDuration(50); |
| 84 | noFadeIn.addTransitions(new Fade(Fade.OUT), new Move(), fadeIn); |
| 85 | |
| 86 | myTransition.addListener(new Transition.TransitionListenerAdapter() { |
| 87 | @Override |
| 88 | public void onTransitionStart(Transition transition) { |
| 89 | System.out.println("---------ListView Tops: Before--------"); |
| 90 | for (int i = 0; i < listview.getChildCount(); ++i) { |
| 91 | TextView view = (TextView) listview.getChildAt(i); |
| 92 | int position = listview.getPositionForView(view); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onTransitionEnd(Transition transition) { |
| 98 | System.out.println("---------ListView Tops: After--------"); |
| 99 | for (int i = 0; i < listview.getChildCount(); ++i) { |
| 100 | TextView view = (TextView) listview.getChildAt(i); |
| 101 | int position = listview.getPositionForView(view); |
| 102 | if (view.hasTransientState()) { |
| 103 | // view.setHasTransientState(false); |
| 104 | } |
| 105 | } |
| 106 | myTransition.removeListener(this); |
| 107 | } |
| 108 | }); |
| 109 | |
| 110 | listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 111 | |
| 112 | @Override |
| 113 | public void onItemClick(AdapterView<?> parent, final View view, int position, long id) { |
| 114 | System.out.println("---------ListView Tops: OnClick--------"); |
| 115 | String item = (String) parent.getItemAtPosition(position); |
| 116 | for (int i = 0; i < listview.getChildCount(); ++i) { |
| 117 | TextView v = (TextView) listview.getChildAt(i); |
| 118 | if (!item.equals(v.getText())) { |
| 119 | // v.setHasTransientState(true); |
| 120 | } |
| 121 | } |
| 122 | // listview.setHasTransientState(true); |
| 123 | mItemToDelete = item; |
| 124 | // numList.remove(item); |
| 125 | TransitionManager.go(mySceneChanger, noFadeIn); |
| 126 | // view.postDelayed(new Runnable() { |
| 127 | // @Override |
| 128 | // public void run() { |
| 129 | // for (int i = 0; i < listview.getChildCount(); ++i) { |
| 130 | // TextView v = (TextView) listview.getChildAt(i); |
| 131 | // v.setHasTransientState(false); |
| 132 | // } |
| 133 | // } |
| 134 | // }, 200); |
| 135 | } |
| 136 | |
| 137 | }); |
| 138 | } |
| 139 | |
| 140 | String mItemToDelete = null; |
| 141 | |
| 142 | private class StableArrayAdapter extends ArrayAdapter<String> { |
| 143 | |
| 144 | HashMap<String, Integer> mIdMap = new HashMap<String, Integer>(); |
| 145 | |
| 146 | public StableArrayAdapter(Context context, int textViewResourceId, |
| 147 | List<String> objects) { |
| 148 | super(context, textViewResourceId, objects); |
| 149 | for (int i = 0; i < objects.size(); ++i) { |
| 150 | mIdMap.put(objects.get(i), i); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public long getItemId(int position) { |
| 156 | String item = getItem(position); |
| 157 | return mIdMap.get(item); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public boolean hasStableIds() { |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | } |