gpt4 book ai didi

android - 将 PopupWindow 设置为具有最大高度

转载 作者:行者123 更新时间:2023-11-29 21:13:53 26 4
gpt4 key购买 nike

我在 PopupWindow 中扩充了一个 ListView,我希望弹出窗口的行为如下:

  • 当 ListView 的高度为 < x 时包裹它
  • 当 ListView 的高度 > x( ListView 可滚动)时,设置弹出窗口的高度 = x

弹出窗口由附加到 EditText 的 TextWatcher 显示,因为它旨在显示搜索建议。ListView 底层的适配器由自定义加载器管理,只要用户在 EditText 中键入内容,它就会启动。我试图覆盖 ListView 上的 onMeasure() 并将测量的高度传递给调用 PopupWindow.update() 的监听器,但这会创建一个循环,因为后者会结束调用第一个。使用以下代码,弹出窗口根据需要包装包含的 ListView,但高度不限于任何值。我需要一个解决方案来将高度限制为最大值,比方说 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(),如下所示:

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);
}
}

关于android - 将 PopupWindow 设置为具有最大高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21956221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com