add ListFragment with FragmentTransaction is not supported?

痞子三分冷 提交于 2019-12-13 16:17:11

问题


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 ?


回答1:


Maybe the problem is because you are importing android.support.app.ListFragment




回答2:


Try using nested fragments. In that way you can wrap your ListFragment inside a Fragment, which you can use in your FragmentTransaction

Nested Fragments



来源:https://stackoverflow.com/questions/9196823/add-listfragment-with-fragmenttransaction-is-not-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!