gpt4 book ai didi

Android:无法选中PopupWindow中的ListView

转载 作者:行者123 更新时间:2023-11-29 14:02:35 25 4
gpt4 key购买 nike

我将具有 listview 的布局设置为 popupwindow,然后设置 popupwindow.setFocusale(false);,然后添加 "android:focusable="true"" 属性到 listview,然后我点击弹出窗口中的listview,无法选择listview中的item,谁能告诉解决方案?提前致谢!

最佳答案

我遇到了同样的问题,在我的情况下需要 PopupWindow.setFocusble(false)(并且在我的情况下使用 ListPopupWindow 不是解决方案,因为很多项目中的内容已经使用了基础 PopupWindow 的功能,包括扩展)。

如果有人处于相同情况,则有一种基于错误讨论的解决方法 here (#9 后)

主要思想是 ListView 的层次结构仍然接收触摸事件,因此我们可以手动触发 onItemClick()

但是,这种方法并不是 100% 与真正的 ListView 的触摸处理相同(就像在点击一行时没有选择发光一样)这对我来说目前做得很好。

如果有人对此问题有更精确的解决方案,请分享。

所以,这是完整的 Adapter 代码,它可以与 PopupWindow 中的 ListView 一起使用,即 setFocusable(false) :

私有(private)类 CustomAdapter 扩展 ArrayAdapter {

private LayoutInflater mInflater;
private ListView mOwningListView;

public CustomAdapter(Context context, List<String> objects, ListView listView) {
super(context, android.R.layout.simple_list_item_1, objects);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mOwningListView = listView;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.font_pick_row, null);
}
// this is the key point of workaround
convertView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
/*
* as every row is still receiving their touches
* we can use this to manually trigger onItemClick
* since it doesn't firing in popupWindow.setFocusable(false)
*/
mOwningListView.getOnItemClickListener().onItemClick(mOwningListView, v, position, getItemId(position));

}
});
//... other stuff
return convertView;
}

关于Android:无法选中PopupWindow中的ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8753291/

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