碎片(fragment)的简单使用

1、在layout布局里面创建两个碎片

left_fragment.xml

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

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

right_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical"
    android:background="@color/black">

</LinearLayout>

2、编写对应的fragment

LeftFragment.java

public class LeftFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState) {
		return inflater.inflate(R.layout.left_fragment, container, false);
	} 
}

RightFragment.java

public class RightFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState) {
		return inflater.inflate(R.layout.right_fragment, container, false);
	} 
}

3、创建一个activity,在布局里面引入碎片

<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.LeftFragment"
	android:layout_width="0dp"
	android:layout_height="match_parent"
	android:layout_weight="1" />
	<fragment
	android:id="@+id/right_fragment"
	android:name="com.example.fragmenttest.RightFragment"
	android:layout_width="0dp"
	android:layout_height="match_parent"
	android:layout_weight="1" />
</LinearLayout>

4、动态添加碎片

1、创建一个碎片和布局
another_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00"
android:orientation="vertical" >

</LinearLayout>

AnotherFragment.java

public class AnotherFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState) {
		return inflater.inflate(R.layout.right_fragment, container, false);
	} 
}

2、改变activity的布局

<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.LeftFragment"
	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/right_fragment"
		android:name="com.example.fragmenttest.RightFragment"
		android:layout_width="match_parent"
		android:layout_height="match_parent" />
	</FrameLayout>
</LinearLayout>

3、在activity代码里改变碎片名字

protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	Button button = (Button) findViewById(R.id.button);
	button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
	switch (v.getId()) {
		case R.id.button:
			AnotherRightFragment fragment = new AnotherRightFragment();
			FragmentManager fragmentManager = getFragmentManager();
			FragmentTransaction transaction = fragmentManager.beginTransaction();
			transaction.replace(R.id.right_layout, fragment);
			//在提交事务之前调用addToBackStack可以通过back键返回到之前的右侧碎片页面
			transaction.addToBackStack(null);
			transaction.commit();
		break;
	} 
}

先new一个碎片对象,再通过getFragmentManager()开启事务,用replace方法替换之前的碎片,最后提交事务就可以了。

5、碎片和活动之间进行通信

1、活动调用碎片方法

Fragment fragment = getFragmentManager().findFragmentById(R.id.right_fragment);

2、碎片调用活动的方法

MainActivity mainActivity = (MainActivity) getActivity();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值