Fragment简述

1.了解FragmentTransaction的方法
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//开启一个事务,getSupportFragmentManager是获取FragmentManage的方式
transaction .add()//往Activity中添加一个Fragment
transaction.remove() //从Activity中移除一个Fragment,如果被移除的Fragment没有添加到回退栈,这个Fragment实例将会被销毁。
transaction.replace()//使用另一个Fragment替换当前的
transaction.hide() //当你的fragment数量固定很少时隐藏当前的Fragment,仅仅是设为不可见,并不会销毁,多的时候可能出现OOM异常,
transaction.show()//显示之前隐藏的Fragment
detach()会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。
attach()重建view视图,附加到UI上并显示。
transatcion.commit()//提交一个事务
2.构造底部菜单
  设置RadioButton的style
  

    <style name="Radio_style">
        <item name="android:layout_width">0dp</item>
        <item name="android:padding">3dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_weight">1</item>
        <item name="android:button">@null</item><!--去除RadioButton默认带的圆点-->
        <item name="android:gravity">center</item>
        <item name="android:textSize">12sp</item>
    </style>


创建主界面的xml文件

<?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:orientation="vertical"
    android:layout_alignParentBottom="true">

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="#FFFFFF"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <RadioButton
            android:text="首页"
            android:textSize="20sp"
            style="@style/Radio_style"
            android:id="@+id/radioButton"
            android:background="@mipmap/ic_launcher"
            android:checked="false" />

        <RadioButton
            android:text="联系人"
            android:textSize="20sp"
            style="@style/Radio_style"
            android:id="@+id/radioButton2" />

        <RadioButton
            android:text="推荐"
            android:textSize="20sp"
            style="@style/Radio_style"
            android:id="@+id/radioButton3" />
        <RadioButton
            android:text="个人中心"
            android:textSize="20sp"
            style="@style/Radio_style"
            android:id="@+id/radioButton4" />
    </RadioGroup>
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/radio_group">
    </FrameLayout>

</RelativeLayout>


创建主页面的Java文件

package com.example.administrator.myapplication;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * Created by Administrator on 2018/10/9.
 */

public class Explain extends AppCompatActivity implements  RadioGroup.OnCheckedChangeListener{
    private RadioGroup rpTab;
    public RadioButton rbDeal,rbPoi,rbMore,rbUser;
    private MyFragment fg1,fg2,fg3,fg4;
//    private MyOnClickListener listener = new MyOnClickListener();
    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.explain);
        bindView();
//        rg.setOnClickListener(listener);
    }

    private void bindView() {
        rpTab = (RadioGroup) findViewById(R.id.radio_group);
        rpTab.setOnCheckedChangeListener(this);

        rbDeal = (RadioButton)findViewById(R.id.radioButton);
        rbPoi = (RadioButton)findViewById(R.id.radioButton2);
        rbMore = (RadioButton)findViewById(R.id.radioButton3);
        rbUser = (RadioButton)findViewById(R.id.radioButton4);

        rbDeal.setChecked(true);
    }
    public void hideAllFragment(FragmentTransaction transaction){
        if(fg1!=null){
            transaction.hide(fg1);//当你的fragment数量固定很少时隐藏当前的Fragment,仅仅是设为不可见,并不会销毁,多的时候可能出现OOM异常,
        }
        if(fg2!=null){
            transaction.hide(fg2);
        }
        if(fg3!=null){
            transaction.hide(fg3);
        }
        if(fg4!=null){
            transaction.hide(fg4);
        }
    }
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//开启一个事务,getSupportFragmentManager是获取FragmentManage的方式
        hideAllFragment(transaction);//隐藏所有的Fragment
        switch (checkedId){
            case R.id.radioButton:
                if(fg1==null){
                    fg1 = new MyFragment("第一个Fragment");
                    transaction.add(R.id.fragment_container,fg1);//往Activity中添加一个Fragment
                }else{
                    transaction.show(fg1);//显示之前隐藏的Fragment
                }
                break;
            case R.id.radioButton2:
                if(fg2==null){
                    fg2 = new MyFragment("第二个Fragment");
                    transaction.add(R.id.fragment_container,fg2);
                }else{
                    transaction.show(fg2);
                }
                break;
            case R.id.radioButton3:
                if(fg3==null){
                    fg3 = new MyFragment("第三个Fragment");
                    transaction.add(R.id.fragment_container,fg3);
                }else{
                    transaction.show(fg3);
                }
                break;
            case R.id.radioButton4:
                if(fg4==null){
                    fg4 = new MyFragment("第四个Fragment");
                    transaction.add(R.id.fragment_container,fg4);
                }else{
                    transaction.show(fg4);
                }
                break;
        }
        transaction.commit();//提交事务
    }
/*    class MyOnClickListener implements View.OnClickListener {
        public void onClick(View view){
        int id=view.getId();
            switch(id){
                case R.id.radioButton:
                    Toast.makeText(Explain.this,"Hello",Toast.LENGTH_SHORT).show();
                case R.id.radioButton2:
                    Toast.makeText(Explain.this,"Hello2",Toast.LENGTH_SHORT).show();
                case R.id.radioButton3:
                    Toast.makeText(Explain.this,"Hello3",Toast.LENGTH_SHORT).show();
                case R.id.radioButton4:
                    Toast.makeText(Explain.this,"Hello4",Toast.LENGTH_SHORT).show();
            }
        }
    }*/
}


创建MyFragment的Java文件

public class MyFragment extends Fragment {
    private String context;
    private TextView mTextView;

    public MyFragment(String context){
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_fragment,container,false);
        mTextView = (TextView)view.findViewById(R.id.textView);
        mTextView.setText(context);
        return view;
    }
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值