Fragment碎片入门教程

一、碎片Fragment的使用步骤:

1、在layout文件夹下新建fragment.xml文件
2、在src下新建我们自己的MyFragment,继承Fragment,使用inflater.inflate(R.layout.fragmenttest,container,false)方法加载fragment.xml文件
3、在main.xml中加入<fragment android:name="MyFragment完整路径" />
4、在MainActivity.class中加载main.xml布局文件

二、代码:

1、在layout文件夹下新建left_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ffaa"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="40sp"
        android:text="@string/detailContent" />

</LinearLayout>

2、在src下新建我们自己的NewTitleFragment.class:

package com.example.fragmenttest;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class NewTitleFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		/**
		 * 加载碎片fragment的方法
		 */
		View view = inflater.inflate(R.layout.left_fragment, container, false) ;
		return view ;
	}
	
}
3、在layout文件夹的activity_main.xml文件中加入上面新建的 NewTitleFragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/left_fragment"
        android:name="com.example.fragmenttest.NewTitleFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/right_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <fragment
            android:id="@+id/righy_fragment"
            android:name="com.example.fragmenttest.NewContentFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>

</LinearLayout>
4、其他两个xml文件和fragment文件代码省略,下面展示MainActivity代码:

package com.example.fragmenttest;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button1) ;
        button.setOnClickListener(this) ;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			DetailContentFragment fragment = new DetailContentFragment() ;
			FragmentManager fragmentManager = getFragmentManager() ;
			FragmentTransaction transaction = fragmentManager.beginTransaction() ;
			transaction.replace(R.id.right_layout, fragment) ;//fragment是被替换的碎片
			transaction.addToBackStack(null) ;
			transaction.commit() ;
			break;

		default:
			break;
		}
	}
}

三、fragment和activity之间通信

1、在activity中获得fragment:

LeftFragment fragment = (LeftFragment)getFragmentManager.findFragmentById(R.id.left_fragment);

接着就可以在activity中调用碎片fragment中的方法了

2、在fragment中获得activity:

MainActivity activity = (MainActivity)getActivity() ;

接着就可以在碎片fragment中调用活动MainActivity中的方法了。

3、在fragment中调用fragment中的方法:

首先,在一个碎片中得到与它关联的活动;

然后,通过这个活动去获取另一个碎片中的实例。


四、下载demo

fragment入门demo下载地址








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值