简单搞定两个Fragment之间的交互。
在MainActivity中的布局activity_main:
<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="horizontal" >
<fragment
android:name="com.example.androideventbusdemo.LeftFragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<fragment
android:name="com.example.androideventbusdemo.RightFragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
MainActivity的代码:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
然后就是左右两个Fragment的交互:
LeftFragment的布局就是三个按钮,这里略去,然后LeftFragment的代码如下:
import org.simple.eventbus.EventBus;
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;
public class LeftFragment extends Fragment {
public static final String CLICK_TAG = "click_user";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container, false);
view.findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post("你好");
}
});
view.findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post("Hello world", CLICK_TAG);
}
});
view.findViewById(R.id.btn3).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post("Hello world", "asyc");
}
});
EventBus.getDefault().register(this);
return view;
}
@Override
public void onDestroy() {
EventBus.getDefault().unregister(this);
super.onDestroy();
}
}
RightFragment的布局省去,就是一个TextView,用来显示左边Fragment传过来的数据,RightFragment的代码如下:
import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subcriber;
import org.simple.eventbus.ThreadMode;
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;
import android.widget.Toast;
public class RightFragment extends Fragment {
private TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment, container, false);
textView = (TextView) view.findViewById(R.id.textview);
EventBus.getDefault().register(this);
return view;
}
@Subcriber
private void sayNiHao(String str){
textView.setText(str);
}
@Subcriber(tag = LeftFragment.CLICK_TAG)
private void sayHelloWorld(String str){
textView.setText(str);
}
@Subcriber(tag = "asyc", mode = ThreadMode.ASYNC)
private void sayAAA(String str){
Toast.makeText(getActivity(), "哥在异步线程中", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
EventBus.getDefault().unregister(this);
super.onDestroy();
}
}
最后就可以完美看到两个Fragment的交互了。