【问题标题】:Change a fragment更改片段
【发布时间】:2013-08-27 18:39:06
【问题描述】:

我有几个片段需要在检查 RadioButton 后显示。如果我不知道之前是哪个片段,如何实现添加/替换片段?以及如何做默认显示片段?

【问题讨论】:

  • 所以你想要一个默认片段,它应该通过 RadioButton 检查更改,通过检查当前片段?
  • 默认片段(fr1)在另一个片段(fr2,fr3,...frn,fr1)上更改

标签: android fragment


【解决方案1】:

如果我理解正确的话,

主活动:

public class MainActivity extends FragmentActivity {

FragmentTransaction ft;
Fragment1           frg1;
Fragment2           frg2;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    frg1 = new Fragment1();
    frg2 = new Fragment2();

    RadioButton btn1 = (RadioButton) findViewById(R.id.radio1);
    btn1.setChecked(true);
    getSupportFragmentManager().beginTransaction().add(R.id.frame, frg1).commit();

    // set listener
    ((RadioGroup) findViewById(R.id.radio_group)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            ft = getSupportFragmentManager().beginTransaction();
            switch (checkedId) {
                case R.id.radio1:
                    ft.replace(R.id.frame, frg1);
                    break;
                case R.id.radio2:
                    ft.replace(R.id.frame, frg2);
                    break;
            }
            ft.commit();
        }
    });
}
}

片段1:

public class Fragment1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    container.removeAllViews();
    return inflater.inflate(R.layout.fragment1, null);
}
}

片段2:

public class Fragment2 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    container.removeAllViews();
    return inflater.inflate(R.layout.fragment2, null);
}
}

activity_main:

<RadioGroup
    android:id="@+id/radio_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment1" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment2" />
</RadioGroup>

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

fragment1.xml:

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Fragment1" >
</TextView>

fragment2.xml:

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Fragment2" >
</TextView>

【讨论】:

  • Lia Pronina,我有几个问题。我可以在电子邮件中问你吗?还是脸书?在俄罗斯=)
  • @Piyush Gupta 我很高兴 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
  • 2019-02-18
相关资源
最近更新 更多