android使用按钮切换fragment

fragment_raise.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/bg_gray">
<RadioGroup
android:id="@+id/fragment_raise_rdog_service"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingTop="20dp"
android:paddingRight="15dp"
android:background="@color/bg_white">

<RadioButton
android:id="@+id/fragment_raise_rdob_raised"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginRight="15dp"
android:button="@null"
android:background="@drawable/fragment_raise_selector"
android:gravity="center"
android:checked="true"
android:text="按钮1"
android:textColor="@color/text_black"
android:textSize="12dp" />

<RadioButton
android:id="@+id/fragment_raise_rdob_raising"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_marginRight="15dp"
android:button="@null"
android:background="@drawable/fragment_raise_selector"
android:gravity="center"
android:text="按钮2"
android:textColor="@color/text_black"
android:textSize="12dp" />
</RadioGroup>
</LinearLayout>

public class RaiseFragment extends Fragment {
private RadioButton radioButtonRaised;
private RadioButton radioButtonRaising;
private FragmentManager fm;
private RaisingGoodsFragment raisingGoodsFragment;
private RaiseWillGoodsFragment raiseWillGoodsFragment;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
LinearLayout rootLayout = (LinearLayout) inflater.inflate(R.layout.fragment_raise, container, false);
radioButtonRaised = rootLayout.findViewById(R.id.fragment_raise_rdob_raised);
radioButtonRaising = rootLayout.findViewById(R.id.fragment_raise_rdob_raising);

initData();
initListener();
return rootLayout;
}

private void initData() {
contextHelper = new ContextHelper(getContext());
fm = this.getChildFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
hideFragment(ft);
if(raisingGoodsFragment == null){
raisingGoodsFragment = new RaisingGoodsFragment();
ft.add(R.id.fragment_raise_fltv, raisingGoodsFragment);
}
ft.show(raisingGoodsFragment);

ft.commit();
}

public void initListener() {

radioButtonRaised.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction ft = fm.beginTransaction();
hideFragment(ft);
if(raisingGoodsFragment == null){
raisingGoodsFragment = new RaisingGoodsFragment();
ft.add(R.id.fragment_raise_fltv, raisingGoodsFragment);
}
ft.show(raisingGoodsFragment);

ft.commit();
}
});

radioButtonRaising.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction ft = fm.beginTransaction();
hideFragment(ft);
if(raiseWillGoodsFragment == null){
raiseWillGoodsFragment = new RaiseWillGoodsFragment();
ft.add(R.id.fragment_raise_fltv, raiseWillGoodsFragment);
}
ft.show(raiseWillGoodsFragment);

ft.commit();
}
});
}

//用于隐藏fragment
private void hideFragment(FragmentTransaction ft){
if(raisingGoodsFragment!=null){
ft.hide(raisingGoodsFragment);
}if(raiseWillGoodsFragment!=null){
ft.hide(raiseWillGoodsFragment);
}
}
}
本文是本人在网上查看很多资料总结的,希望能帮助需要的人,黄色的部分是重点。

猜你喜欢

转载自www.cnblogs.com/zhuchunyan/p/9362039.html