正确创造一个PopupWindow片段 [英] Properly creating a fragment in a PopupWindow

查看:300
本文介绍了正确创造一个PopupWindow片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid开发,并感到困惑如何完成我想要做的。我做了一些阅读和学习有关的片段,所以我可以分享不同的屏幕尺寸设计的布局和code。我有一对夫妇创建的片段,并已经成功地使用它们。但我有一个情况我想显示手机上的正常活动片段,而是要显示在PopupWindow片段(或类似的东西,如果有更好的选择),在平板电脑上。

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.

我已经设法弄清楚如何膨胀的片段,并在PopupWindow显示它是单击按钮时。我的code是这样的:

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

在弹出窗口,其中包含在serverconnection_fragment.xml描述的UI。现在的问题是,通过建立这种方式,该片段类ServerConnectionFragment.java从不实例所以出现在列表上的按钮没有任何项目在我的UI,没有听众,等等。好像应该有办法为我实例化的Java类,有它吹大片段通常并附加事件侦听器,然后通过建立有进PopupWindow构造的观点,但我无法弄清楚如何。谁能帮助我?

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?

仅供参考,我使用的是Android的支持,v4.jar文件的片段类建立这种为Android 2.1。

FYI, I’m building this for Android 2.1 using the Android-support-v4.jar file for the Fragment classes.

谢谢!

推荐答案

直接充气的布局也不会导致其它实例化片段;机器人将只考虑一个巧合,无论是片段和活动试图引用相同的布局文件

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.

通常情况下,你可以使用<一个href="http://developer.android.com/reference/android/app/FragmentTransaction.html#add%28int,%20android.app.Fragment%29">FragmentManager.add(int,Fragment)到的片段添加到布局。但是,您指定的容器ID必须是当前活动布局的一部分,这是不与 PopupWindow 的情况。相反,你将不得不片段添加到片段经理没有指定一个容器,然后晚些时候在片段(如 ONSTART()),您可以显示PopupWindow。这是precisely如何 DialogFragment 的作品,而且因为已经有了很多的支持,我会建议您切换到使用DialogFragment代替。

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 how DialogFragment works, and since there is already lots of support for it, I would suggest you switch to using a DialogFragment instead.

使用您的片段code,简单地扩展,而不是片段DialogFragment,并使用<一个href="http://developer.android.com/reference/android/app/DialogFragment.html#show%28android.app.FragmentManager,%20java.lang.String%29">DialogFragment.show(FragmentManager,String)要显示它。你可以通过调用摆脱默认边框的的setStyle(DialogFragment.STYLE_NO_FRAME,getTheme())的onCreate 方法。您还可以将此片段添加到布局(如你所说,在手机上你不希望它被显示为一个弹出),它会工作,你如何期望。

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 the onCreate 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.

这篇关于正确创造一个PopupWindow片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆