【Android知识点精讲】(4)Fragment详解(二)

文章出处:http://blog.csdn.net/scarthr/article/details/42121193

我们接着上一讲的内容。这节我们来看一下Fragment的回调机制。


一 Fragment的独立性

因为Fragment是具有自己独立的布局和逻辑代码的,所以也就造就了它非常独立灵活的功能,我们可以把需要处理的代码放在它所关联的Activity中,进而操作不同的Fragment,这也就证明了Fragment的独立性。

我们在主布局中定义两个fragment:

<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:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <fragment
        android:id="@+id/fm_top"
        android:name="com.thr.fragment3.TopFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fm_bottom"
        android:name="com.thr.fragment3.BottonFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>
每一个fragment记得指定好id(或tag)和name(或class)。

TopFragment:

package com.thr.fragment3;

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

public class TopFragment extends Fragment implements OnClickListener {

	private TopClickListner topClickListner;

	public interface TopClickListner {
		public void onTopClick(String name);
	}

	@Override
	public void onAttach(Activity activity) {
		if (activity instanceof TopClickListner) {
			topClickListner = (TopClickListner) activity;
		}
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_top, null);
		view.setOnClickListener(this);
		return view;
	}

	@Override
	public void onClick(View v) {
		if (topClickListner != null) {
			topClickListner.onTopClick("This is top speaking...");
		}
	}

}
我们在这里定义 一个TopClickListner,它的实现在主Activity中,所以实现了Fragment的独立。

MainActivity:

package com.thr.fragment3;

import com.thr.fragment3.TopFragment.TopClickListner;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity implements TopClickListner {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public void onTopClick(String name) {
		BottonFragment bottomFragment = (BottonFragment) getFragmentManager()
				.findFragmentById(R.id.fm_bottom);
		bottomFragment.setContent(name);
	}
}
我们这里实现TopClickListner,调用BottonFragment的setContent方法实现另一个Fragment的交互。

BottomFragment:

package com.thr.fragment3;

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

public class BottonFragment extends Fragment {

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

	public void setContent(String content) {
		TextView tv = (TextView) getView();
		tv.setText(content);
	}
}
简单易懂,是不?


源码下载





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值