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.transition.Fade; 24 import android.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.transition.AutoTransition; 31 import android.transition.ChangeBounds; 32 import android.transition.Transition; 33 import android.transition.TransitionListenerAdapter; 34 import android.transition.TransitionManager; 35 import android.transition.TransitionSet; 36 37 import java.util.ArrayList; 38 import java.util.HashMap; 39 import java.util.List; 40 41 public class ListViewAddRemove extends Activity { 42 43 final ArrayList<String> numList = new ArrayList<String>(); 44 45 @Override 46 public void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 setContentView(R.layout.list_view_add_remove); 49 50 final LinearLayout container = findViewById(R.id.container); 51 52 final ListView listview = findViewById(R.id.listview); 53 for (int i = 0; i < 200; ++i) { 54 numList.add(Integer.toString(i)); 55 } 56 final StableArrayAdapter adapter = new StableArrayAdapter(this, 57 android.R.layout.simple_list_item_1, numList); 58 listview.setAdapter(adapter); 59 60 final ViewTreeObserver observer = container.getViewTreeObserver(); 61 observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 62 public void onGlobalLayout() { 63 System.out.println("-------------------------------------"); 64 System.out.println("onLayoutListener: listview view tops: "); 65 for (int i = 0; i < listview.getChildCount(); ++i) { 66 TextView view = (TextView) listview.getChildAt(i); 67 System.out.println(" " + view.getText() + ": " + view.getTop()); 68 } 69 } 70 }); 71 72 final Scene mySceneChanger = new Scene(listview); 73 74 mySceneChanger.setEnterAction(new Runnable() { 75 @Override 76 public void run() { 77 numList.remove(mItemToDelete); 78 adapter.notifyDataSetChanged(); 79 } 80 }); 81 final Transition myTransition = new AutoTransition(); 82 final TransitionSet noFadeIn = new TransitionSet(). 83 setOrdering(TransitionSet.ORDERING_SEQUENTIAL); 84 Fade fadeIn = new Fade(Fade.IN); 85 fadeIn.setDuration(50); 86 noFadeIn.addTransition(new Fade(Fade.OUT)).addTransition(new ChangeBounds()).addTransition(fadeIn); 87 88 myTransition.addListener(new TransitionListenerAdapter() { 89 @Override 90 public void onTransitionStart(Transition transition) { 91 System.out.println("---------ListView Tops: Before--------"); 92 for (int i = 0; i < listview.getChildCount(); ++i) { 93 TextView view = (TextView) listview.getChildAt(i); 94 int position = listview.getPositionForView(view); 95 } 96 } 97 98 @Override 99 public void onTransitionEnd(Transition transition) { 100 System.out.println("---------ListView Tops: After--------"); 101 for (int i = 0; i < listview.getChildCount(); ++i) { 102 TextView view = (TextView) listview.getChildAt(i); 103 int position = listview.getPositionForView(view); 104 if (view.hasTransientState()) { 105 // view.setHasTransientState(false); 106 } 107 } 108 myTransition.removeListener(this); 109 } 110 }); 111 112 listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 113 114 @Override 115 public void onItemClick(AdapterView<?> parent, final View view, int position, long id) { 116 System.out.println("---------ListView Tops: OnClick--------"); 117 String item = (String) parent.getItemAtPosition(position); 118 for (int i = 0; i < listview.getChildCount(); ++i) { 119 TextView v = (TextView) listview.getChildAt(i); 120 if (!item.equals(v.getText())) { 121 // v.setHasTransientState(true); 122 } 123 } 124 // listview.setHasTransientState(true); 125 mItemToDelete = item; 126 // numList.remove(item); 127 TransitionManager.go(mySceneChanger, noFadeIn); 128 // view.postDelayed(new Runnable() { 129 // @Override 130 // public void run() { 131 // for (int i = 0; i < listview.getChildCount(); ++i) { 132 // TextView v = (TextView) listview.getChildAt(i); 133 // v.setHasTransientState(false); 134 // } 135 // } 136 // }, 200); 137 } 138 139 }); 140 } 141 142 String mItemToDelete = null; 143 144 private class StableArrayAdapter extends ArrayAdapter<String> { 145 146 HashMap<String, Integer> mIdMap = new HashMap<String, Integer>(); 147 148 public StableArrayAdapter(Context context, int textViewResourceId, 149 List<String> objects) { 150 super(context, textViewResourceId, objects); 151 for (int i = 0; i < objects.size(); ++i) { 152 mIdMap.put(objects.get(i), i); 153 } 154 } 155 156 @Override 157 public long getItemId(int position) { 158 String item = getItem(position); 159 return mIdMap.get(item); 160 } 161 162 @Override 163 public boolean hasStableIds() { 164 return true; 165 } 166 167 } 168 } 169