在Android 2.2中需要Fragment的帮助

时间:2012-01-05 17:57:08

标签: android replace android-fragments

我第一次使用Android 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;
}


}

单击/选中单选按钮时调用的片段

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();

我的问题是片段没有被替换......我可以在第二个片段后面看到HelloFragment Layout。

1 个答案:

答案 0 :(得分:1)

如果首先以编程方式添加片段,则只能以编程方式删除/替换片段。

从main_activity.xml中删除片段节点,以便frameLayout中没有任何内容。然后在你的onCreate做这样的事情:

FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frameLayout, new HelloWorldFragment());
transaction.addToBackStack(null);
transaction.commit();

然后你的替换代码看起来像:

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.frameLayout, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

此外,您可能需要更改HelloWorldFragment以使用LayoutInflater.inflate的重载,将false添加为最后一个参数attachToRoot。