0

我想在 Radiobutton (用作选项卡)上使用片段管理器更改片段(@+id/content_frame),但是当片段加载我的单选按钮消失时 ononCheckedChanged 没有显示。

这是我的 main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/content_frame2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioGroup
        android:id="@+id/buttongroup1"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >

        <at.bookworm.widget.TabControlButton
            android:id="@+id/opt_ct"
            style="@style/Widget.Holo.SegmentedControl"
            android:checked="true"
            android:text="City Taxi"
            android:textColor="@color/black"
            android:textStyle="normal" />

        <at.bookworm.widget.TabControlButton
            android:id="@+id/opt_os"
            style="@style/Widget.Holo.SegmentedControl"
            android:text="Outstation"
            android:textColor="@color/black"
            android:textStyle="normal" />

        <at.bookworm.widget.TabControlButton
            android:id="@+id/opt_lo"
            style="@style/Widget.Holo.SegmentedControl"
            android:text="Local"
            android:textColor="@color/black"
            android:textStyle="normal" />
    </RadioGroup>
    <FrameLayout  android:id="@+id/content_frame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</FrameLayout>

<ListView
    android:id="@+id/left_drawer"
    style="?android:attr/dividerHorizontal"
    android:layout_width="100dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/silver_two"
    android:choiceMode="singleChoice" >

    <!--
    <LinearLayout 
        android:id="@+id/left_drawer"
        android:orientation="vertical"
        android:layout_width="240dp"
        android:layout_height="match_parent">
        <ImageView 
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/my_account"/>
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="Test"/>
    </LinearLayout>
    -->

 </ListView>

  </android.support.v4.widget.DrawerLayout>

我在我的 MainActivity 中使用片段管理器替换使用片段这里是代码

公共类 MainActivity 扩展 Activity{

片段 mainFragment = new MainFragment();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    radioGroup = (RadioGroup) findViewById(R.id.buttongroup1);
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {

                    public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.popBackStack(null,              FragmentManager.POP_BACK_STACK_INCLUSIVE);
            fragmentManager.beginTransaction().replace(R.id.content_frame, mainFragment).commit();
        }
    });

}

广播组破解后它继续

public class MainFragment extends Fragment{

static View rootView;
------
----
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null)
                parent.removeView(rootView);
    }
    try {
        rootView = inflater.inflate(R.layout.route_fragment, container, false);
    } catch (InflateException e) {

    }
return rootView;
}
4

1 回答 1

0

请检查您在片段中是否正在执行类似的操作:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.article_view, container, false);

而不是 setContentView

这是一个很好的资源:

http://developer.android.com/training/basics/fragments/creating.html

于 2013-07-08T19:55:35.843 回答