问题
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