私はアンドロイド2.2(後方互換性あり)のフラグメントをはじめて使用しています... 簡単な例を作成して動作を確認しました。ここで アンドロイド2.2のフラグメントのヘルプが必要
は、コードのスナップショットに主な活動コード
public class FragmentExampleActivity extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
RadioButton radioButton1 = (RadioButton)findViewById(R.id.radioButton1);
radioButton1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeView1();
}
});
radioButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//changeView1();
}
});
}
protected void changeView1() {
// Create new fragment and transaction
Fragment newFragment = new RadioActivity1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.relativeLayout, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
主な活動レイアウトファイル(main_activity)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/relativeLayout">
<FrameLayout android:id="@+id/frameLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_alignParentTop="true"
android:layout_above="@+id/radioButtonGroupLayout">
<fragment android:name="com.example.fragmentexample.HelloWorldFragment"
android:id="@+id/generalView" android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<RadioGroup android:id ="@+id/radioButtonGroupLayout" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:layout_alignParentBottom="true">
<RadioButton android:id="@+id/radioButton1" android:text="Radio 1"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/radioButton2" android:text="Radio 2"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<RadioButton android:id="@+id/radioButton3" android:text="Radio 3"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</RadioGroup>
</RelativeLayout>
初期フラグメントクラス
public class HelloWorldFragment extends Fragment
{
public HelloWorldFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.hello_world_fragment_layout,null);
return v;
}
}
ですラジオボタンがクリックされたときに、私の問題があることフラグメントである第2のフラグメントクラスの
public class RadioActivity1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.radio_activity_one_layout,null);
return v;
}
}
レイアウト
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
断片を置換するためのコードのスナップショット
// Create new fragment and transaction
Fragment newFragment = new RadioActivity1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.remove(getSupportFragmentManager().findFragmentById(R.id.generalView));
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.relativeLayout, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
チェック/呼ばれるフラグメント置換されていない...私は、2番目のフラグメントの後ろにあるHelloFragment Layoutを見ることができます。