将PopupWindow设置为最大高度 [英] Setting PopupWindow to have a maximum height

查看:912
本文介绍了将PopupWindow设置为最大高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PopupWindow内为ListView充气,我希望弹出窗口的行为如下:

I inflate a ListView inside a PopupWindow and I want the popup to behave like this:

  • 当高度小于<时包装列表视图.x
  • 当listiew的高度> x(列表视图可滚动)时,设置弹出窗口的高度= x

弹出窗口由附加到EditText的TextWatcher显示,因为它是为了显示搜索建议.ListView底层的适配器由自定义的Loader进行管理,只要用户在EditText中键入内容,就启动该适配器.我试图在列表视图上覆盖 onMeasure()并将所测量的高度传递给调用 PopupWindow.update()的侦听器,但这会创建一个循环,因为后者将结束打电话给第一个.使用以下代码,弹出窗口可以根据需要包装包含的ListView,但是高度不限于任何值.我需要一种将高度限制为最大值的解决方案,例如300dp.

the popup is showed by a TextWatcher attached to an EditText, as it is meant to show search suggestions. The adapter underlying the ListView is managed by a custom Loader, started whenever the user has typed something in the EditText. I tried to override onMeasure() on the listview and pass the measured height to a listener which calls PopupWindow.update(), but this creates a loop since the latter would end up calling the first. With the following code, the popup wraps the contained ListView as wanted, but the height is not restricted to any value. I need a solution to restrict the height to a maximum value, let's say 300dp.

etFiltro.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            /...
        }
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            Log.i("loader","text changed");
            filtro = String.valueOf(charSequence);
            getSupportLoaderManager().restartLoader(0,null,loaderCallbacks);
            if (popupWindow.isShowing()) popupWindow.dismiss();
        }
        @Override
        public void afterTextChanged(Editable editable) {               
            popupWindow.showAsDropDown(etFiltro);
        }
    });

private LoaderManager.LoaderCallbacks<Cursor> loaderCallbacks = new LoaderManager.LoaderCallbacks<Cursor>() {

    MyListView suggestionList;
    SimpleCursorAdapter adapter;
    int mHeight;

    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        SuggestionLoader loader = new SuggestionLoader(MainActivity.this, databaseConnector, filtro);
        loader.setUpdateThrottle(500);
        View view = ((LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.popup_window,null);
        suggestionList = (MyListView) view.findViewById(R.id.suggestionList);
        adapter = new SimpleCursorAdapter(MainActivity.this,android.R.layout.simple_list_item_1,null,
                new String[]{"note"},new int[]{android.R.id.text1},0);
        suggestionList.setAdapter(adapter);
        suggestionList.setEmptyView(view.findViewById(R.id.tvNoSuggestions));
        //ensure previous popup is dismissed
        if (popupWindow!=null) popupWindow.dismiss();
        popupWindow = new PopupWindow(view,etFiltro.getWidth(),0);
        popupWindow.setWindowLayoutMode(0,WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setAnimationStyle(0);//0 = no animation; -1 = default animation            
        Log.i("loader","onCreateLoader");
        return loader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        adapter.changeCursor(cursor);           
        Log.i("loader","onLoadFinished");

    }

    @Override
    public void onLoaderReset(Loader<Cursor> cursorLoader) {
        Log.i("loader", "onLoaderReset");
        adapter.changeCursor(null);
    }
};

推荐答案

我自己找到了解决方案.对于任何会偶然发现此问题的人,答案是像这样重写ListView的 onMeasure()方法:

I have found the solution by myself. For anyone who will ever stumble upon this problem, the answer is to override the method onMeasure() of the ListView like this:

public class MyListView extends ListView {

public MyListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public MyListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyListView(Context context) {
    super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    //set your custom height. AT_MOST means it can be as tall as needed,
    //up to the specified size.

    int height = MeasureSpec.makeMeasureSpec(300,MeasureSpec.AT_MOST);

    super.onMeasure(widthMeasureSpec,height);               
}
}

这篇关于将PopupWindow设置为最大高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆