android fragment

核心内容

   FragmentManager fm = getFragmentManager();
   FragmentTransaction transaction = fm.beginTransaction();
   mWeixin = new FragmentContent();
   transaction.replace(R.id.activity_content, mWeixin);
//   transaction.addToBackStack(null);//此时可以返回
   transaction.commit();


先来看下activity_main布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.devin.fragment.MainActivity">
    <fragment
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:name="com.example.devin.view.FragmentTitle"
        android:id="@+id/activity_title"/>

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/activity_content"
        android:layout_below="@+id/activity_title"
        android:layout_above="@+id/activity_bottom"/>

    <include
        android:id="@+id/activity_bottom"
        android:layout_width="fill_parent"
        android:layout_height="47dp"
        layout="@layout/bottom"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>
 
public class FragmentContent extends Fragment {
    private TextView textView = null ;
    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
       // return super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_content, container, false);
        textView = (TextView) view.findViewById(R.id.TvContent);
        textView.setText("我们是内容");
        return view;
    }
}

主要的Java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mTabWeixin;
    private Button mTabFriend;

    private FragmentContent mWeixin;  //FragmentContent
    private FragmentFriend mFriend;  //FragmentFriend

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化控件和声明事件
        mTabWeixin = (Button) findViewById(R.id.button1);
        mTabFriend = (Button) findViewById(R.id.button2);
        mTabWeixin.setOnClickListener(this);
        mTabFriend.setOnClickListener(this);
        setDefaultFragment();// 设置默认的Fragment
    }

    private void setDefaultFragment()
    {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        mWeixin = new FragmentContent();
        transaction.replace(R.id.activity_content, mWeixin);
     //   transaction.addToBackStack(null);//此时可以返回
        transaction.commit();
    }

    @Override
    public void onClick(View v)
    {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();// 开启Fragment事务
        switch (v.getId()){
            case R.id.button1:
                if (mWeixin == null)
                {
                    mWeixin = new FragmentContent();
                }
                // 使用当前Fragment的布局替代id_content的控件
                transaction.replace(R.id.activity_content, mWeixin);
                break;
            case R.id.button2:
                if (mFriend == null)
                {
                    mFriend = new FragmentFriend();
                }
                transaction.replace(R.id.activity_content, mFriend);
                break;
            case R.id.button3:
                break;
        }
        transaction.addToBackStack(null);//此时可以返回
        transaction.commit();    // 事务提交
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值