Simple use of Eventbus in Android

Source: Internet
Author: User

Eventbus is an open source framework that facilitates communication with various components in Android, an open source address; Https://github.com/greenrobot/EventBus. The Eventbus feature is very powerful when doing a function today. Encountered a conflict of click events and data transfer update issues.

The detail point is that there is a GridView on the header of the ListView that can be refreshed up and down. At this point, the Onitemclick event of the GridView conflicts with the Onitemclick event of the ListView.

And I'm going to do the data transfer when I implement the Griview click event.

Resolution Procedure 1:

Onitemclick Click Conflict problem, I put in the code of the Onitemclick event staring, and then in the GridView item layout by adding an ID, and then in adapter to the entire item to do an onclick () Click event, the problem is overcome.

Resolution 1: Data transfer problem, the use of Eventbus:

Register Eventbus in activity: in the OnCreate () method; Eventbus.getdefault (). Register (this);

Then in OnStop () or Ondestory (): Eventbus.getdefault (). Unregister (this);

Event handling Method Oneventmainthread:

public void Oneventmainthread (Categoryitemevent event) {
Bundle bundle = new bundle ();
Bundle.putint (...);
...//pass data, implement jump
}

What is done in adapter is the post event;

Holder.categoryRela.setOnClickListener (New Onclicklistener () {

@Override
public void OnClick (View v) {
Eventbus.getdefault (). Post (New Categoryitemevent (POS));//At the time of post, an int type of the reference POS is passed, and this parameter is Eventbus run in the activity This pos is used when Oneventmainthread (Categoryitemevent event).


}
});

Simple use of Eventbus in Android

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.