这里简单的介绍一下接口回调功能
xml 布局文件
activity_main.xml
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
fragment01.xml
android:layout_height="match_parent"
android:background="#ffff00"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:hint="输入要传递的值"
android:ems="10" >
fragment02.xml
android:layout_height="match_parent"
android:background="#ccff44"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="接受传来的值"
android:padding="10dp"
/>
从这里开始是java代码片段
在这里我只介绍接口其他自行理解
Fragment01.java
package com.example.demo3_fragmentjiekou;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class Fragment01 extends Fragment {
private View view;
private EditText editText1;
private Button button1;
private Callback callback;
//进入Fragment生命周期的第一个方法onAttach 利用里面获得的参数也就是当前访问的activity,
//将此activity赋值给下面的Callback接口里面 这样接口里面就有了当前的activity设置此接口也,
//就访问了本fragment的activity中的实现接口方法
//同样访问本fragment的activity也要实现此接口
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
callback = (Callback) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.fragment01, container, false);
editText1 = (EditText) view.findViewById(R.id.editText1);
button1 = (Button) view.findViewById(R.id.button1);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String string = editText1.getText().toString();
callback.setcallback(string);
}
});
}
//定义接口
public interface Callback{
//定义接口中的抽象方法并且传值验证接口回调
public void setcallback(String name);
}
}
Fragment02.xml
package com.example.demo3_fragmentjiekou;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment02 extends Fragment {
private View view;
private TextView textview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.fragment02, container, false);
textview = (TextView) view.findViewById(R.id.text);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Bundle bundle = getArguments();
String name = bundle.getString("name");
textview.setText(name);
}
//1.定义一个接受值的方法
public void getfragment(String name){
//3.设置参数
Bundle args = new Bundle();
//将值存入了Bundle
args.putString("name", name);
setArguments(args);
}
}
MainActivity.java
package com.example.demo3_fragmentjiekou;
import com.example.demo3_fragmentjiekou.Fragment01.Callback;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
//实现了Fragment01中的Callback接口重写setcallback方法形成回调
public class MainActivity extends FragmentActivity implements Callback{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//默认加载Fragment01
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction beginTransaction = supportFragmentManager.beginTransaction();
beginTransaction.replace(R.id.relative, new Fragment01());
beginTransaction.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void setcallback(String name) {
Fragment02 fragment02 = new Fragment02();
fragment02.getfragment(name);
Log.i("jiaobaba", name);
FragmentTransaction beginTransaction = getSupportFragmentManager().beginTransaction();
beginTransaction.replace(R.id.relative, fragment02);
beginTransaction.addToBackStack(null);
beginTransaction.commit();
}
}