回调接口 java
回调就是A和B有合做关系,A的某一个环节须要B本身告诉A要怎么作,这就是回调,回调必定有接口)android
下面是一个简单的例子(两个fragment)之间的通讯,看一张图片吧数组
说一下我本身对接口回调的理解,哪一个页面须要修改UI,则须要哪一个页面去实现接口(通常状况都是Activity和fragment去修改UI),app
在更改UI这个Fragment或者Activity中须要持有上一个fragment和activity的设置的引用,也就是说须要上一个页面设置这个接口的ide
类,好比个人例子中(就是设置第一个动做产生的对象)this
FragmentManager manager = getActivity().getSupportFragmentManager();
Fragment_one mFragment_one = (Fragment_one) manager.findFragmentById(R.id.one);spa
然而此时 new OnMyClickLisener() 就是Fragment_two的对象,由于Fragment_two 实现了OnMyClickLisener 这个接口.net
//先设置了这个3d
mFragment_one.setOnMyClickLisener(new OnMyClickLisener() {
@Override
public void getMyIndex(int index) {
Log.i("520it","Fragment_two=index"+index);
tv.setText("你点击"+index);
}
});xml
因此在Fragment_one中的 myClickLisener就是Fragment_two的对象,因此调取 myClickLisener.getMyIndex(index);
在Fragment_two中科院接收到到值
这个案例是点击上面的数组下面一块儿变化,固然也能够用其余方式来实现
下面我本身画的流程图
上代码
MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<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">
<fragment android:name="com.example.fragment_interface.Fragment_one"
android:layout_weight="1"
android:layout_width="match_parent"
android:id="@+id/one"
android:layout_height="0dp"/>
<fragment android:name="com.example.fragment_interface.Fragment_two"
android:layout_weight="1"
android:layout_width="match_parent"
android:id="@+id/two"
android:layout_height="0dp"/>
</LinearLayout>
//定义的接口数据
OnMyClickLisener.java
package com.example.fragment_interface;
public interface OnMyClickLisener {
//界面须要修改
public void getMyIndex(int index);
}
Fragment_one.java
package com.example.fragment_interface;
import android.os.Bundle;
import android.support.annotation.Nullable;
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;
public class Fragment_one extends Fragment {
private Button one_btn;
int index =0;
//先定义一个空的
OnMyClickLisener myClickLisener;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment_one, null);
one_btn = (Button)view.findViewById(R.id.one_btn);
return view;
}
//这个方法在fragment_two里面调取设置,此时的listener就是至关于fragment_two,
public void setOnMyClickLisener(OnMyClickLisener lisener){
this.myClickLisener = lisener;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
one_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
index++;
myClickLisener.getMyIndex(index);
}
});
}
}
fragment_one.xml
<RelativeLayout 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:background="#FF1493"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/one_btn"
android:text="点击"/>
</RelativeLayout>
Fragment_two.java
package com.example.fragment_interface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment_two extends Fragment {
private TextView tv;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment_two, null);
tv = (TextView)view.findViewById(R.id.two);
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment_two, null);
tv = (TextView)view.findViewById(R.id.two);
//监听上面点击
//获取上面的
FragmentManager manager = getActivity().getSupportFragmentManager();
Fragment_one mFragment_one = (Fragment_one) manager.findFragmentById(R.id.one);
//理解,new OnMyClickLisener()至关于在 Fragment_two 实现了OnMyClickLisener 接口
//new OnMyClickLisener() 能够理解为 Fragment_two 的一个类
//至关于在 mFragment_one传递了一个Fragment_two,顾在Fragment_two 能够接收
//Fragment_one 传过来的值
mFragment_one.setOnMyClickLisener(new OnMyClickLisener() {
@Override
public void getMyIndex(int index) {
Log.i("520it","Fragment_two=index"+index);
tv.setText("你点击"+index);
}
});
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
fragment_two.xml
<RelativeLayout 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:background="#D2B48C" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/two" android:text="点击次数++"/> </RelativeLayout>