Fragment(碎片)

主界面layout文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.administrator.myapplication.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:id="@+id/layou_fragment"></LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="首页"
            android:id="@+id/bt_1"
            android:onClick="bt1_onClick"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="购买"
            android:id="@+id/bt_2"
            android:onClick="bt1_onClick"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="我的"
            android:id="@+id/bt_3"
            android:onClick="bt1_onClick"/>
    </LinearLayout>
</LinearLayout>
View Code

java主函数

 1 package com.example.administrator.myapplication;
 2 
 3 
 4 import android.support.v4.app.FragmentActivity;
 5 import android.support.v4.app.FragmentManager;
 6 import android.support.v7.app.AppCompatActivity;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.Toast;
11 
12 import com.example.administrator.myapplication.com.db.fragment.BuyFragment;
13 import com.example.administrator.myapplication.com.db.fragment.MainFragment;
14 import com.example.administrator.myapplication.com.db.fragment.PersonFragment;
15 
16 public class MainActivity extends FragmentActivity {
17     Button bt_1;
18     Button bt_2;
19     Button bt_3;
20     MainFragment mMainFragment;
21     BuyFragment mBuyFragment;
22     PersonFragment mPersonFragment;
23     FragmentManager mFragmentManager;
24 
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.activity_main);
29         mMainFragment=new MainFragment(getApplicationContext());
30         mFragmentManager=getSupportFragmentManager();
31         mFragmentManager.beginTransaction().add(R.id.layou_fragment,mMainFragment).commit();
32 
33     }
34     public void bt1_onClick(View view)
35     {
36         switch (view.getId())
37         {
38             case R.id.bt_1:
39                 mMainFragment=new MainFragment(getApplicationContext());
40                 mFragmentManager.beginTransaction().replace(R.id.layou_fragment,mMainFragment).commit();
41                 Toast.makeText(MainActivity.this, "首页", Toast.LENGTH_SHORT).show();
42                 break;
43             case R.id.bt_2:
44                 mBuyFragment=new BuyFragment(getApplication());
45                 mFragmentManager.beginTransaction().replace(R.id.layou_fragment,mBuyFragment).commit();
46                 Toast.makeText(MainActivity.this, "购买", Toast.LENGTH_SHORT).show();
47                 break;
48             case R.id.bt_3:
49                 mPersonFragment=new PersonFragment(getApplicationContext());
50                 mFragmentManager.beginTransaction().replace(R.id.layou_fragment,mPersonFragment).commit();
51                 Toast.makeText(MainActivity.this, "个人", Toast.LENGTH_SHORT).show();
52                 break;
53         }
54     }
55 }
View Code

首页Fragment的Java类:

package com.example.administrator.myapplication.com.db.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.myapplication.R;

/**
 * Created by Administrator on 2016/8/9.
 */
public class MainFragment extends Fragment {
    private Context mContext;
    public MainFragment(Context context)
    {
        mContext=context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=LayoutInflater.from(mContext).inflate(R.layout.fragment_main,null);
        return view;
    }
}
View Code

购买Fragment的Java类:

 1 package com.example.administrator.myapplication.com.db.fragment;
 2 
 3 
 4 import android.content.Context;
 5 import android.os.Bundle;
 6 import android.support.annotation.Nullable;
 7 import android.support.v4.app.Fragment;
 8 import android.view.LayoutInflater;
 9 import android.view.View;
10 import android.view.ViewGroup;
11 
12 import com.example.administrator.myapplication.R;
13 
14 /**
15  * Created by Administrator on 2016/8/9.
16  */
17 public class BuyFragment extends Fragment {
18     private Context mContext;
19     public BuyFragment(Context context)
20     {
21         mContext=context;
22     }
23 
24     @Nullable
25     @Override
26     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
27        View view=LayoutInflater.from(mContext).inflate(R.layout.fragment_buy,null);
28         return view;
29     }
30 }
View Code

个人Fragment的Java类:

 1 package com.example.administrator.myapplication.com.db.fragment;
 2 
 3 
 4 import android.content.Context;
 5 import android.os.Bundle;
 6 import android.support.annotation.Nullable;
 7 import android.support.v4.app.Fragment;
 8 import android.view.LayoutInflater;
 9 import android.view.View;
10 import android.view.ViewGroup;
11 
12 import com.example.administrator.myapplication.R;
13 
14 /**
15  * Created by Administrator on 2016/8/9.
16  */
17 public class PersonFragment extends Fragment {
18     private Context mContext;
19     public PersonFragment(Context context)
20     {
21         mContext=context;
22     }
23 
24     @Nullable
25     @Override
26     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
27         View view=LayoutInflater.from(mContext).inflate(R.layout.fragment_person,null);
28         return view;
29     }
30 }
View Code

 

 

posted on 2016-07-28 11:54  beens  阅读(166)  评论(0编辑  收藏  举报

导航