在 PopupWindow 中正确创建片段
我是 Android 开发新手,对如何完成我想要做的事情感到困惑。我已经阅读和学习了一些有关片段的内容,因此我可以在各种屏幕尺寸设计之间共享布局和代码。我已经创建了几个片段并已成功使用它们。但我遇到了一种情况,我想在手机上的正常活动中显示片段,但想在平板电脑上的 PopupWindow(或类似的东西,如果有更好的选择)中显示片段。
我已经设法弄清楚如何膨胀片段并在单击按钮时将其显示在 PopupWindow 中。我的代码如下所示:
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) BrowsingActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupLayout = inflater.inflate(R.layout.serverconnection_fragment, null, false);
connectionListPopup = new PopupWindow(popupLayout, 300, 470, true);
connectionListPopup.showAtLocation(BrowsingActivity.this.findViewById(R.id.connectionListImage), Gravity.CENTER, 0, 0);
}
弹出窗口出现并包含 serverconnection_fragment.xml 中描述的 UI。问题是,通过这种方式创建它,Fragment 类 ServerConnectionFragment.java 永远不会实例化,因此我的 UI 中的列表中没有项目,按钮上没有侦听器,等等。似乎应该有一种方法可以让我实例化 java 类,让它正常膨胀片段并附加事件侦听器,然后将其中创建的视图传递到 PopupWindow 构造函数中,但我不知道如何实现。有人可以帮我吗?
仅供参考,我正在使用 Fragment 类的 Android-support-v4.jar 文件为 Android 2.1 构建此程序。
I’m new to Android development and am confused about how to accomplish what I’m trying to do. I’ve done some reading and learning about fragments so I can share layout and code between various screen size designs. I’ve got a couple of fragments created and have used them successfully. But I’ve got a situation where I want to show a fragment in a normal activity on the phone, but want to show the fragment in a PopupWindow (or something similar if there’s a better choice) on a tablet.
I’ve managed to figure out how to inflate the fragment and display it in a PopupWindow when a button is clicked. My code looks like this:
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) BrowsingActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupLayout = inflater.inflate(R.layout.serverconnection_fragment, null, false);
connectionListPopup = new PopupWindow(popupLayout, 300, 470, true);
connectionListPopup.showAtLocation(BrowsingActivity.this.findViewById(R.id.connectionListImage), Gravity.CENTER, 0, 0);
}
The popup appears and contains the UI described in serverconnection_fragment.xml. The problem is that by creating it this way, the Fragment class ServerConnectionFragment.java is never instantiated so there are no items in the list in my UI, no listeners on buttons, and so on. It seems like there should be a way for me to instantiate the java class, have it inflate the fragment normally and attach event listeners, then pass the view created there into the PopupWindow constructor, but I can’t figure out how. Can anyone help me out?
FYI, I’m building this for Android 2.1 using the Android-support-v4.jar file for the Fragment classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
直接膨胀布局不会导致它实例化片段; Android 只会认为片段和活动都试图引用同一个布局文件只是巧合。
通常,您会使用 FragmentManager.add(int, Fragment) 将片段添加到布局中。但是,您指定的容器 ID 必须是当前 Activity 布局的一部分,而
PopupWindow
则不然。相反,您必须将片段添加到片段管理器而不指定容器,然后稍后在片段中(例如onStart()
)您可以显示 PopupWindow。这正是 DialogFragment 的工作原理,并且由于已经有很多支持它,我建议您改用 DialogFragment。对于 Fragment 代码,只需扩展 DialogFragment 而不是 Fragment,并使用 DialogFragment.show(FragmentManager,String) 显示它。您可以通过在
onCreate
方法中调用setStyle(DialogFragment.STYLE_NO_FRAME, getTheme())
来摆脱默认边框。您仍然可以将此片段添加到布局中(正如您所说,在手机上您不希望它显示为弹出窗口),它将按照您的预期工作。Inflating the layout directly will not cause it it to instantiate the fragment; Android would just consider it a mere coincidence that both the fragment and the activity are trying to refer to the same layout file.
Ordinarily, you would use FragmentManager.add(int,Fragment) to add a fragment to a layout. However, the container id that you specify has to be part of the layout of the current Activity, and this is not the case with a
PopupWindow
. Instead, you would have to add the fragment to the fragment manager without specifying a container, and then sometime later in the fragment (e.g.onStart()
) you can show a PopupWindow. This is precisely howDialogFragment
works, and since there is already lots of support for it, I would suggest you switch to using a DialogFragment instead.With your Fragment code, simply extend DialogFragment instead of Fragment, and use DialogFragment.show(FragmentManager,String) to display it. You can get rid of the default border by calling
setStyle(DialogFragment.STYLE_NO_FRAME, getTheme())
in theonCreate
method. You can still add this Fragment to a layout (as you say, on a phone you do not want it to be shown as a popup) and it will work as how you expect.