第五单元
#EventBus Fragment代码
package com.example.morning.fragment;
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 com.example.morning.Message;
import com.example.morning.R;
import org.greenrobot.eventbus.EventBus;
/**
* A simple {@link Fragment} subclass.
*/
public class AP_eventBus_fragment extends Fragment {
public AP_eventBus_fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_ap_event_bus_fragment, container, false);
TextView viewById = inflate.findViewById(R.id.ap_eventBus_button);
viewById.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new Message().setName("丁一").setAge(24));
}
});
return inflate;
}
}
java代码
package com.example.morning.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.example.morning.Message;
import com.example.morning.R;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class AP_eventBus extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ap_event_bus);
EventBus.getDefault().register(this);
}
@Subscribe
public void getMessage(Message msg){
Toast.makeText(this, msg.toString(), Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
自定义Message类
package com.example.morning;
public class Message {
private String name;
private int age;
public String getName() {
return name;
}
public Message setName(String name) {
this.name = name;
return this;
}
public int getAge() {
return age;
}
public Message setAge(int age) {
this.age = age;
return this;
}
@Override
public String toString() {
return "Message{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
实现传值
fragment之间实现互相传值
左边的的fragment
package com.example.morning.fragment;
import android.content.Context;
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.Button;
import android.widget.EditText;
import com.example.morning.R;
/**
* A simple {@link Fragment} subclass.
*/
public class MF1 extends Fragment {
private MyListener my;
public MF1() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
my= (MyListener) getActivity();
}
private EditText MF1Edit;
private Button MF1Button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_mf1, container, false);
MF1Edit = (EditText) inflate.findViewById(R.id.MF1_edit);
MF1Button = (Button) inflate.findViewById(R.id.MF1_button);
MF1Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String trim = MF1Edit.getText().toString().trim();
my.sendMessage(trim);
}
});
return inflate;
}
public interface MyListener{
void sendMessage(String str);
}
}
左边fragment的布局
<?xml version="1.0" encoding="utf-8"?><!-- TODO: Update blank fragment layout -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入"
android:background="@android:drawable/edit_text"
android:id="@+id/f2_edit"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击提交"
android:background="#DB7194"
android:id="@+id/f2_button"
/>
右边的fragment
package com.example.morning.fragment;
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 com.example.morning.R;
/**
* A simple {@link Fragment} subclass.
*/
public class MF3 extends Fragment implements MF2.send{
public MF3() {
// Required empty public constructor
}
private TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_mf3, container, false);
tv=inflate.findViewById(R.id.fm3_text);
return inflate;
}
@Override
public void sendMessage(String str) {
tv.setText(str);
}
}
右边的布局
<?xml version="1.0" encoding="utf-8"?><!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/fm3_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"
/>
eventbus
package com.example.morning.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.example.morning.Message;
import com.example.morning.R;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
public class AP_eventBus extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ap_event_bus);
EventBus.getDefault().register(this);
}
@Subscribe
public void getMessage(Message msg){
Toast.makeText(this, msg.toString(), Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
event 对应的fragment
package com.example.morning.fragment;
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 com.example.morning.Message;
import com.example.morning.R;
import org.greenrobot.eventbus.EventBus;
/**
* A simple {@link Fragment} subclass.
*/
public class AP_eventBus_fragment extends Fragment {
public AP_eventBus_fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_ap_event_bus_fragment, container, false);
TextView viewById = inflate.findViewById(R.id.ap_eventBus_button);
viewById.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new Message().setName("丁一").setAge(24));
}
});
return inflate;
}
}