不支持使用 FragmentTransaction 添加 ListFragment ?
我有一个 ListFragment
:
public class MyListFragment extends ListFragment{
...
}
为了在我的 Android 2.1 API 7 项目中使用 Fragment,我使用 支持包。 (支持包的所有内容都已配置)
在我的主机中 Activity 看起来像:
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class FragmentNavActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
FragmentManager fMgr = getSupportFragmentManager();
FragmentTransaction fTrans = fMgr.beginTransaction();
MyListFragment myListFragment = new MyListFragment();
fTrans.add(android.R.id.content, myListFragment).commit(); //problem here
...
}
}
问题是 FragmentTransaction
的 .add(int, Fragment) 方法接受第二个类型为
Fragment
的参数,但是,我有 ListFragment
,这是该方法不可接受的。
那么,如何使用 FragmentTransaction
添加 ListFragment
呢?
I have a ListFragment
:
public class MyListFragment extends ListFragment{
...
}
In order to use Fragment in my Android 2.1 API 7 project, I use the support package. (Everything has configured for the support package)
In my host Activity looks like:
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class FragmentNavActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
FragmentManager fMgr = getSupportFragmentManager();
FragmentTransaction fTrans = fMgr.beginTransaction();
MyListFragment myListFragment = new MyListFragment();
fTrans.add(android.R.id.content, myListFragment).commit(); //problem here
...
}
}
The problem is that the FragmentTransaction
's .add(int, Fragment)
method accept arguments with the second one of type Fragment
, however, I have ListFragment
which is not acceptable by this method.
So, how to add ListFragment
with FragmentTransaction
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许问题是因为您正在导入
android.support.app.ListFragment
Maybe the problem is because you are importing
android.support.app.ListFragment
尝试使用嵌套片段。通过这种方式,您可以将 ListFragment 包装在 Fragment 中,您可以在 FragmentTransaction 中使用它
嵌套片段
Try using nested fragments. In that way you can wrap your ListFragment inside a Fragment, which you can use in your FragmentTransaction
Nested Fragments