与ListView的问题一个PopupWindow内 [英] Problems with ListView inside a PopupWindow
问题描述
我有一个的ListView
在 PopupWindow
。该 PopupWindow
这样的初始化
I have a ListView
in a PopupWindow
. The PopupWindow
is initialized like this
window.setContentView(root);
window.setTouchable(true);
window.setFocusable(true);
window.setOutsideTouchable(true);
window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
然后的ListView
:
fileList = (ListView) root.findViewById(R.id.explorer_list);
fileList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
fileList.setSelector(android.R.drawable.screen_background_light_transparent);
fileList.setOnItemClickListener(this);
[...]
@Override
public void onItemClick(AdapterView<?> adapter, View v, int pos, long id) {
selected = (File) fileList.getItemAtPosition(pos);
}
就是这样,一切正常,只是选择不显示在选择直到的ListView
滚动(没有视觉上显示为选中状态,直到列表中滚动,虽然项目选择正确)。
Like this, everything works correctly except that the selector will not show up on selection until ListView
is scrolled (nothing shows visually as selected until the list is scrolled, although the item is correctly selected).
如果我设置了 PopupWindow
不可作为焦点,则可视选择正常工作(该项目在视觉选择权当点击进入),但 onItemClick( )
不会被调用,因此我不能选择的项目。
If I set the PopupWindow
not focusable, then the visual selection works correctly (the item is visually selected right when clicked into) but onItemClick()
is never called and thus I cannot get the selected item.
ListView.getSelectedItem()
总是返回空
在这两种情况下,即使有一个选择的项目。
ListView.getSelectedItem()
always returns null
in both cases, even if there's a selected item.
在如何解决这种情况你知道吗?先谢谢了。
Any idea on how to solve this situation? Thanks in advance.
推荐答案
我终于用一个自定义适配器来存储选择的值,并从那里用它来标记它:
I finally used a custom adapter to store the selected value and use it from there to mark it:
public class FileExplorerAdapter extends ArrayAdapter<File> {
/** File names */
private List<File> values = new ArrayList<File>();
/** Currently selected position */
private int selected = -1;
public FileExplorerAdapter(Context context, List<File> values) {
super(context, R.layout.explorer_row, values);
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// I know that my layout is always a TextView
TextView row = (TextView) convertView;
if (row == null) {
row = (TextView) ViewHelper.inflateViewById(getContext(),
R.layout.explorer_row);
}
// More code...
// Set up the background for selected element
if (selected == position) {
row.setBackgroundColor(Color.LTGRAY);
// Override background selector
} else {
row.setBackgroundColor(Color.TRANSPARENT);
}
// More code...
return row;
}
/** This sets the selected position */
public void setSelected(int position) {
selected = position;
}
}
和上实现了 OnItemClickListener
的关联的ListView
,我设置了当前选择的项目类适配器。
And on the class that implements the OnItemClickListener
for the associated ListView
, I set up the currently selected item in the adapter.
@Override
public void onItemClick(AdapterView<?> adapter, View v, int pos, long id) {
FileExplorerAdapter fileadapter = (FileExplorerAdapter) fileList
.getAdapter();
fileadapter.setSelected(pos);
}
这篇关于与ListView的问题一个PopupWindow内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!