我有片段的主要活动
public class MainActivity extends Activity implements OnClickListener {
Fragment fragment;
Button btn1, btn2,btn3;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.button1);
btn2 = (Button)findViewById(R.id.button2);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
MyFragment myfragment = new MyFragment();
ft.add(R.id.myFragment, myfragment);
ft.commit();
}
@Override
public void onClick(View v){
Fragment fragment = null;
if(v == btn1){
fragment = new Fragment1();
}else if(v == btn2){
fragment = new Fragment2();
}
FragmentTransaction transcation = getFragmentManager().beginTransaction();
transcation.replace(R.id.myFragment,fragment);
transcation.addToBackStack(null);
transcation.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
transcation.commit();
}
}
和三个类似的简单片段
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate( R.layout.fragment2, container, false);
}
}
它适用于 HC 和 ICS,但如果我尝试在旧版本 2.3 上运行。它崩溃了....我也添加了支持的库,但不起作用。还有一个问题......请向我推荐有助于从正常开发转向片段的好教程或视频。