Fragment & EventBus

Fragment & EvenBus

Video


activity_maim.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragment"
android:name="klapper.caseupload.fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout="@layout/fragment1_layout" />
<fragment
android:id="@+id/fragment2"
android:name="klapper.caseupload.fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/fragment"
android:layout_marginTop="30dp"
tools:layout="@layout/fragment2_layout" />
</RelativeLayout>

fragment 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1:"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="@+id/send_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="Send Message" />
</RelativeLayout>

fragment 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 2:"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="Receive String..."
android:textSize="15sp"
android:textStyle="italic" />
</RelativeLayout>

MainActivity.java

1
2
3
4
5
6
7
8
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

fragment1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class fragment1 extends Fragment
{
private View rootView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
{
rootView = inflater.inflate(R.layout.fragment1_layout, container, false);
Button sendMessage = (Button)rootView.findViewById(R.id.send_btn);
sendMessage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
EventBus.getDefault().post(new SomeEvent("Hello this is Fragment1!!"));
}
});
return rootView;
}
}
> fragment2.java
```java
public class fragment2 extends Fragment
{
private View rootView;
private TextView receiveString;
private String tmpString;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
{
rootView = inflater.inflate(R.layout.fragment2_layout, container, false);
receiveString = (TextView)rootView.findViewById(R.id.textView2);
return rootView;
}
@Subscribe(threadMode = ThreadMode.MainThread)
public void onEven(SomeEvent event)
{
tmpString += event.getMessage() + "\n";
receiveString.setText(tmpString);
}
@Override
public void onStart()
{
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop()
{
super.onStop();
EventBus.getDefault().unregister(this);
}
}
文章目錄
  1. 1. Fragment & EvenBus
|