zoukankan      html  css  js  c++  java
  • 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);
    }
    }
    }
    本文是本人在网上查看很多资料总结的,希望能帮助需要的人,黄色的部分是重点。
  • 相关阅读:
    Pytest单元测试框架——Pytest+Allure+Jenkins的应用
    Postman+Newman+Git+Jenkins接口自动化测试
    Pytest单元测试框架——Pytest简介
    unittest单元测试框架
    Postman学习笔记(二)
    CukeTest+Puppeteer的Web自动化测试(二)
    Postman学习笔记(一)
    CukeTest+Puppeteer的Web自动化测试(一)
    Puppeteer笔记(八):Puppeteer执行自定义Javascript方法
    Puppeteer笔记(七):Puppeteer切换浏览器TAB页
  • 原文地址:https://www.cnblogs.com/zhuchunyan/p/9362039.html
Copyright © 2011-2022 走看看