Fragment & EvenBus

Video


activity_maim.xml

<?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

<?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

<?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

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

fragment1.java

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

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);
    }
}

results matching ""

    No results matching ""