我嘗試添加listFragment with fragmentTransation.add(...),但我不能這樣做,這是一個錯誤。我想添加一個像正常片段一樣的listfragment,有可能嗎?Android:添加列表片段到交易
這裏是我的主要活動代碼:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentList fragmentList = FragmentList.createFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.relativeLayout,fragmentList); //WRONG
fragmentTransaction.commit();
}
這裏是我listfragment類代碼:
public class FragmentList extends ListFragment
{
public static FragmentList createFragment()
{
FragmentList fragmentList = new FragmentList();
return fragmentList;
}
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
ListView listView = getListView();
String[] stringa = {"A","B","C"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,stringa);
listView.setAdapter(arrayAdapter);
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater,container,savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment, container, false);
return rootView;
}
}
發佈錯誤請! – ApolloSoftware
@ApolloSoftware在fragmentTransaction.add(R.id.relativeLayout,fragmentList)下面有一條紅線; – Curio
您可以擴展公共類MainActivity擴展AppCompatActivity,FragmentActivity? – ApolloSoftware