背景
我试图让任何视图显示下拉列表(不是对话框,而是显示在视图下方/上方的列表),如微调器所示(从 API 11 开始,如此处所示)。
为此,我使用来自 actionBarSherlock 库的 IcsListPopupWindow.java。
问题
我无法让它很好地显示项目列表:
当列表太大而无法显示时,列表不会显示(滚动条的)拇指。
选择项目时,有时会出现正常的选择效果,有时会出现很多灰色背景,如下图:
问题
我究竟做错了什么?我查看了 IcsListPopupWindow.java 的代码,很难理解和查看需要什么和不需要什么。
这是我的示例代码:
public class MainActivity extends Activity {
private PopupMenuAdapter mAdapter;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//prepare popup menu:
final PopupMenuItem[] items = new PopupMenuItem[10];
for (int i = 0; i < items.length; ++i)
items[i] = new PopupMenuItem(android.R.drawable.sym_def_app_icon, R.string.hello_world);
mAdapter = new PopupMenuAdapter(this, android.R.layout.simple_list_item_1, items);
final IcsListPopupWindow popupWindow = new IcsListPopupWindow(this, null, R.attr.popupMenuStyle); popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.abs__menu_dropdown_panel_holo_light));
popupWindow.setAdapter(mAdapter);
popupWindow.setModal(true);
popupWindow.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> arg0, final View arg1, final int arg2, final long arg3) {
popupWindow.dismiss();
}
});
//upon clicking on a button, show the popup menu :
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
// show popup menu:
popupWindow.setContentWidth(MainActivity.this.getWindowManager().getDefaultDisplay().getWidth() / 2);
mAdapter.notifyDataSetChanged(); // if you change anything
popupWindow.setAnchorView(v);
popupWindow.show();
}
});
}
// //////////////////////////////////////////////////////////
// PopupMenuAdapter //
// ///////////////////
private class PopupMenuAdapter extends ArrayAdapter<PopupMenuItem> {
Context context;
int layoutResourceId;
PopupMenuItem data[] = null;
public PopupMenuAdapter(final Context context, final int layoutResourceId, final PopupMenuItem[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View view = convertView;
if (view == null) {
final LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(layoutResourceId, parent, false);
}
final PopupMenuItem pItem = data[position];
final TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText(pItem.textResId);
text.setCompoundDrawablesWithIntrinsicBounds(pItem.iconResId, 0, 0, 0);
return view;
}
}
private static class PopupMenuItem {
public int iconResId;
public int textResId;
public PopupMenuItem(final int iconResId, final int textResId) {
this.iconResId = iconResId;
this.textResId = textResId;
}
}
}
编辑:我现在已经尝试了 HoloEverywhere 库中的 ListPopupWindow.java 文件(并且还只导入了它需要的文件),它也有完全相同的问题。
编辑:对于第二个问题(选择效果),我使用了我自己定制的 listView 项,它的根视图有一个 next 属性:
android:background="@android:drawable/list_selector_background"
仍然不知道如何显示滚动条。如果我使用:
mPopup.getListView().setVerticalScrollBarEnabled(true);
mPopup.getListView().setScrollbarFadingEnabled(false);
比 actionBarSherlock 什么都没有发生,而对于 holoEverywhere 我得到一个 NPE:
07-03 14:28:09.359: E/AndroidRuntime(1869): java.lang.NullPointerException
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.View.onDrawScrollBars(View.java:5976)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.View.draw(View.java:6939)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.widget.AbsListView.draw(AbsListView.java:3030)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.View.draw(View.java:6936)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.ViewRoot.draw(ViewRoot.java:1529)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.ViewRoot.performTraversals(ViewRoot.java:1265)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.os.Looper.loop(Looper.java:130)
07-03 14:28:09.359: E/AndroidRuntime(1869): at android.app.ActivityThread.main(ActivityThread.java:3687)
07-03 14:28:09.359: E/AndroidRuntime(1869): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 14:28:09.359: E/AndroidRuntime(1869): at java.lang.reflect.Method.invoke(Method.java:507)
07-03 14:28:09.359: E/AndroidRuntime(1869): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-03 14:28:09.359: E/AndroidRuntime(1869): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-03 14:28:09.359: E/AndroidRuntime(1869): at dalvik.system.NativeStart.main(Native Method)