文章出处: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);
}
}
简单易懂,是不?