1 /* 2 * Copyright (C) 2007 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 17 package android.widget.listview; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.widget.ArrayAdapter; 24 import android.widget.ListView; 25 26 import com.android.frameworks.coretests.R; 27 28 public class ListViewHeight extends Activity { 29 30 private View mButton1; 31 private View mButton2; 32 private View mButton3; 33 34 private View mOuterLayout; 35 private ListView mInnerList; 36 37 ArrayAdapter<String> mAdapter; 38 private String[] mStrings = { 39 "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi" }; 40 41 @Override onCreate(Bundle savedInstanceState)42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 45 setContentView(R.layout.linear_layout_listview_height); 46 47 mButton1 = findViewById(R.id.button1); 48 mButton2 = findViewById(R.id.button2); 49 mButton3 = findViewById(R.id.button3); 50 51 mOuterLayout = findViewById(R.id.layout); 52 mInnerList = (ListView)findViewById(R.id.inner_list); 53 54 mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, 55 mStrings); 56 57 // Clicking this button will show the list view and set it to a fixed height 58 // If you then hide the views, there is no problem. 59 mButton1.setOnClickListener(new View.OnClickListener() { 60 public void onClick(View v) { 61 // set listview to fixed height 62 ViewGroup.MarginLayoutParams lp; 63 lp = (ViewGroup.MarginLayoutParams) mInnerList.getLayoutParams(); 64 lp.height = 200; 65 mInnerList.setLayoutParams(lp); 66 // enable list adapter 67 mInnerList.setAdapter(mAdapter); 68 // and show it 69 mOuterLayout.setVisibility(View.VISIBLE); 70 } 71 }); 72 73 // Clicking this button will show the list view and set it match_parent height 74 // If you then hide the views, there is an NPE when calculating the ListView height. 75 mButton2.setOnClickListener(new View.OnClickListener() { 76 public void onClick(View v) { 77 // set listview to fill screen 78 ViewGroup.MarginLayoutParams lp; 79 lp = (ViewGroup.MarginLayoutParams) mInnerList.getLayoutParams(); 80 lp.height = lp.MATCH_PARENT; 81 mInnerList.setLayoutParams(lp); 82 // enable list adapter 83 mInnerList.setAdapter(mAdapter); 84 // and show it 85 mOuterLayout.setVisibility(View.VISIBLE); 86 } 87 }); 88 89 // Clicking this button will remove the list adapter and hide the outer enclosing view. 90 // We have to climb all the way to the top because the bug (not checking visibility) 91 // only occurs at the very outer loop of ViewAncestor.performTraversals and in the case of 92 // an Activity, this means you have to crawl all the way out to the root view. 93 // In the search manager, it's sufficient to simply show/hide the outer search manager 94 // view to trigger the same bug. 95 mButton3.setOnClickListener(new View.OnClickListener() { 96 public void onClick(View v) { 97 mInnerList.setAdapter(null); 98 // hide listview's owner 99 // as it turns out, the owner doesn't take us high enough 100 // because our activity includes a title bar, thus another layer 101 View parent = (View) mOuterLayout.getParent(); // FrameLayout (app container) 102 View grandpa = (View) parent.getParent(); // LinearLayout (title+app) 103 View great = (View) grandpa.getParent(); // PhoneWindow.DecorView 104 great.setVisibility(View.GONE); 105 } 106 }); 107 } 108 109 } 110