回調接口 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>