Application of Eventbus framework in Android Multi-pane (Fragment)

Source: Internet
Author: User
Tags eventbus

Usually multi-pane design, such as fragment A is a headline,fragement B is detail, then B usually need to implement a Click event interface, this way, two fragment is high coupling, and need in fragment A write a interface, if a lot of fragment, the situation is more complicated, the chaos. Here you can use EventbusThis framework, a typical publisher-subscriber pattern. For specific reference: Https://github.com/greenrobot/EventBus use the following methods: This example comes from a typical multi-fragment example of Android developers, The left fragment is Headlinefragment, contains a lot of headlines of the ListView, the right is detailfragment, click on the left fragment in one will refresh the right fragment. A. First define your own event, here is actually casually write a class, example headline need to pass a click when the ListView position to detail fragment, so you can write a package int type messageevent:
 Public class messageevent {    publicint  position;      Public Messageevent (int  position) {        this. Position = position;    }} 
B. In subscribers (i.e. detail fragment or fragment B), OnStart and OnStop are registered/unregistered:
Detailfragment.java

@Overridepublicvoid OnStart () { super. OnStart (); Eventbus.getdefault (). Register (this);} @Overridepublicvoid OnStart () { super. OnStart (); Setnewscategory (0); Eventbus.getdefault (). Register (this);}
C. In subscribers (that is, detail fragment or fragment B), write a function called OnEvent, the parameter returned is the messageevent type, the function is called method, Waiting for headline to pass over the Messageevent object that wraps the position data
Detailfragment.java
 Public void onEvent (Messageevent event) {    int index = event.positioin;    // Do something using index, such as refreshing article content }
D. In the publisher (i.e. headline fragment or fragment A), execute the POST method in the appropriate place, that is, publish the messageevent, Here, the post is sent in the callback that the Headlinefragment ListView is clicked on:
Headlinefragment.java


Public void int long ID) {
Eventbus.getdefault (). Post (new messageevent (position));}

This way, the OnEvent method of Detailfragment will be called, containing position messageevent is also passed the past, then detailfragment according to get position to refresh the interface.

When you're done, you can find two fragment without writing any interface or implements any interfaces, or instantiating a listenser and performing the so-called Setonlistener. The internal process here is roughly, Eventbus in a component to register the time to record the OnEvent function inside the component, an application may have more than one onevent function waiting to be called, here is the OnEvent function parameter type and number to distinguish. The use of the need to pay attention to a problem, here Eventbus is through the Java reflection mechanism to find this onevent method, so when packaging needs to be confused if you need to remember not to confuse onevent this function, you can add in the Proguard file:
class * {    publicvoid onevent* (* *);}
About Eventbus more ways to use and source code can refer to Https://github.com/greenrobot/EventBus/blob/master/HOWTO.md

Application of Eventbus framework in Android Multi-pane (Fragment)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.