Home | History | Annotate | Download | only in transitiontests
      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.TransitionSet;
     34 import android.transition.TransitionManager;
     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 TransitionSet noFadeIn = new TransitionSet().
     82                 setOrdering(TransitionSet.ORDERING_SEQUENTIAL);
     83         Fade fadeIn = new Fade(Fade.IN);
     84         fadeIn.setDuration(50);
     85         noFadeIn.addTransition(new Fade(Fade.OUT)).addTransition(new ChangeBounds()).addTransition(fadeIn);
     86 
     87         myTransition.addListener(new Transition.TransitionListenerAdapter() {
     88             @Override
     89             public void onTransitionStart(Transition transition) {
     90                 System.out.println("---------ListView Tops: Before--------");
     91                 for (int i = 0; i < listview.getChildCount(); ++i) {
     92                     TextView view = (TextView) listview.getChildAt(i);
     93                     int position = listview.getPositionForView(view);
     94                 }
     95             }
     96 
     97             @Override
     98             public void onTransitionEnd(Transition transition) {
     99                 System.out.println("---------ListView Tops: After--------");
    100                 for (int i = 0; i < listview.getChildCount(); ++i) {
    101                     TextView view = (TextView) listview.getChildAt(i);
    102                     int position = listview.getPositionForView(view);
    103                     if (view.hasTransientState()) {
    104 //                        view.setHasTransientState(false);
    105                     }
    106                 }
    107                 myTransition.removeListener(this);
    108             }
    109         });
    110 
    111         listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    112 
    113             @Override
    114             public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
    115                 System.out.println("---------ListView Tops: OnClick--------");
    116                 String item = (String) parent.getItemAtPosition(position);
    117                 for (int i = 0; i < listview.getChildCount(); ++i) {
    118                     TextView v = (TextView) listview.getChildAt(i);
    119                     if (!item.equals(v.getText())) {
    120 //                        v.setHasTransientState(true);
    121                     }
    122                 }
    123 //                listview.setHasTransientState(true);
    124                 mItemToDelete = item;
    125 //                numList.remove(item);
    126                 TransitionManager.go(mySceneChanger, noFadeIn);
    127 //                view.postDelayed(new Runnable() {
    128 //                    @Override
    129 //                    public void run() {
    130 //                        for (int i = 0; i < listview.getChildCount(); ++i) {
    131 //                            TextView v = (TextView) listview.getChildAt(i);
    132 //                            v.setHasTransientState(false);
    133 //                        }
    134 //                    }
    135 //                }, 200);
    136             }
    137 
    138         });
    139     }
    140 
    141     String mItemToDelete = null;
    142 
    143     private class StableArrayAdapter extends ArrayAdapter<String> {
    144 
    145         HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
    146 
    147         public StableArrayAdapter(Context context, int textViewResourceId,
    148                 List<String> objects) {
    149             super(context, textViewResourceId, objects);
    150             for (int i = 0; i < objects.size(); ++i) {
    151                 mIdMap.put(objects.get(i), i);
    152             }
    153         }
    154 
    155         @Override
    156         public long getItemId(int position) {
    157             String item = getItem(position);
    158             return mIdMap.get(item);
    159         }
    160 
    161         @Override
    162         public boolean hasStableIds() {
    163             return true;
    164         }
    165 
    166     }
    167 }
    168