FileDocCategorySizeDatePackage
ListViewHeight.javaAPI DocAndroid 5.1 API4557Thu Mar 12 22:22:12 GMT 2015android.widget.listview

ListViewHeight

public class ListViewHeight extends android.app.Activity

Fields Summary
private android.view.View
mButton1
private android.view.View
mButton2
private android.view.View
mButton3
private android.view.View
mOuterLayout
private android.widget.ListView
mInnerList
android.widget.ArrayAdapter
mAdapter
private String[]
mStrings
Constructors Summary
Methods Summary
protected voidonCreate(android.os.Bundle savedInstanceState)


    
        
        super.onCreate(savedInstanceState);

        setContentView(R.layout.linear_layout_listview_height);

        mButton1 = findViewById(R.id.button1);
        mButton2 = findViewById(R.id.button2);
        mButton3 = findViewById(R.id.button3);
        
        mOuterLayout = findViewById(R.id.layout);
        mInnerList = (ListView)findViewById(R.id.inner_list);
        
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, 
                                            mStrings);

        // Clicking this button will show the list view and set it to a fixed height
        // If you then hide the views, there is no problem.
        mButton1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // set listview to fixed height 
                ViewGroup.MarginLayoutParams lp;
                lp = (ViewGroup.MarginLayoutParams) mInnerList.getLayoutParams();
                lp.height = 200;
                mInnerList.setLayoutParams(lp);
                // enable list adapter
                mInnerList.setAdapter(mAdapter);
                // and show it
                mOuterLayout.setVisibility(View.VISIBLE);
            }
        });

        // Clicking this button will show the list view and set it match_parent height
        // If you then hide the views, there is an NPE when calculating the ListView height.
        mButton2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // set listview to fill screen
                ViewGroup.MarginLayoutParams lp;
                lp = (ViewGroup.MarginLayoutParams) mInnerList.getLayoutParams();
                lp.height = lp.MATCH_PARENT;
                mInnerList.setLayoutParams(lp);
                // enable list adapter
                mInnerList.setAdapter(mAdapter);
                // and show it
                mOuterLayout.setVisibility(View.VISIBLE);
            }
        });

        // Clicking this button will remove the list adapter and hide the outer enclosing view.
        // We have to climb all the way to the top because the bug (not checking visibility)
        // only occurs at the very outer loop of ViewAncestor.performTraversals and in the case of
        // an Activity, this means you have to crawl all the way out to the root view.
        // In the search manager, it's sufficient to simply show/hide the outer search manager
        // view to trigger the same bug.
        mButton3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mInnerList.setAdapter(null);
                // hide listview's owner
                // as it turns out, the owner doesn't take us high enough
                // because our activity includes a title bar, thus another layer
                View parent = (View) mOuterLayout.getParent();      // FrameLayout (app container)
                View grandpa = (View) parent.getParent();           // LinearLayout (title+app)
                View great = (View) grandpa.getParent();            // PhoneWindow.DecorView
                great.setVisibility(View.GONE);
            }
        });