blob: 4eeba4d6b3ab37e031edc9f5f48d18c10656a037 [file] [log] [blame]
Chet Haasefaebd8f2012-05-18 14:17:57 -07001/*
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 */
16package com.android.transitiontests;
17
18import android.app.Activity;
19import android.content.Context;
20import android.os.Bundle;
21import android.view.View;
22import android.widget.AdapterView;
23import android.widget.ArrayAdapter;
24import android.widget.LinearLayout;
25import android.widget.ListView;
26import android.widget.TextView;
Chet Haasefaebd8f2012-05-18 14:17:57 -070027
28
29import java.util.ArrayList;
30import java.util.HashMap;
31import java.util.List;
32
33public 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}