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.widget.AdapterView; |
| 23 | import android.widget.ArrayAdapter; |
| 24 | import android.widget.LinearLayout; |
| 25 | import android.widget.ListView; |
| 26 | import android.widget.TextView; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 27 | |
| 28 | |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.HashMap; |
| 31 | import java.util.List; |
| 32 | |
| 33 | public class ListViewAddRemoveNoTransition extends Activity { |
| 34 | |
| 35 | final ArrayList<String> numList = new ArrayList<String>(); |
| 36 | |
| 37 | @Override |
| 38 | public void onCreate(Bundle savedInstanceState) { |
| 39 | super.onCreate(savedInstanceState); |
| 40 | setContentView(R.layout.list_view_add_remove); |
| 41 | |
| 42 | final LinearLayout container = (LinearLayout) findViewById(R.id.container); |
| 43 | |
| 44 | final ListView listview = (ListView) findViewById(R.id.listview); |
| 45 | for (int i = 0; i < 200; ++i) { |
| 46 | numList.add(Integer.toString(i)); |
| 47 | } |
| 48 | final StableArrayAdapter adapter = new StableArrayAdapter(this, |
| 49 | android.R.layout.simple_list_item_1, numList); |
| 50 | listview.setAdapter(adapter); |
| 51 | |
| 52 | listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 53 | |
| 54 | @Override |
| 55 | public void onItemClick(AdapterView<?> parent, final View view, int position, long id) { |
| 56 | String item = (String) parent.getItemAtPosition(position); |
| 57 | for (int i = 0; i < listview.getChildCount(); ++i) { |
| 58 | TextView v = (TextView) listview.getChildAt(i); |
| 59 | if (!item.equals(v.getText())) { |
| 60 | v.setHasTransientState(true); |
| 61 | } |
| 62 | } |
| 63 | numList.remove(item); |
| 64 | adapter.notifyDataSetChanged(); |
| 65 | view.postDelayed(new Runnable() { |
| 66 | @Override |
| 67 | public void run() { |
| 68 | for (int i = 0; i < listview.getChildCount(); ++i) { |
| 69 | TextView v = (TextView) listview.getChildAt(i); |
| 70 | v.setHasTransientState(false); |
| 71 | } |
| 72 | } |
| 73 | }, 200); |
| 74 | } |
| 75 | |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | private class StableArrayAdapter extends ArrayAdapter<String> { |
| 80 | |
| 81 | HashMap<String, Integer> mIdMap = new HashMap<String, Integer>(); |
| 82 | |
| 83 | public StableArrayAdapter(Context context, int textViewResourceId, |
| 84 | List<String> objects) { |
| 85 | super(context, textViewResourceId, objects); |
| 86 | for (int i = 0; i < objects.size(); ++i) { |
| 87 | mIdMap.put(objects.get(i), i); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public long getItemId(int position) { |
| 93 | String item = getItem(position); |
| 94 | return mIdMap.get(item); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public boolean hasStableIds() { |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | } |
| 103 | } |