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. Eventbus function is very powerful, today when doing a function, encountered the collision of click event and data transmission update problem. The specific 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 issue, I put in the code of the Onitemclick event commented, and then in the GridView item layout based on the ID, and then in the adapter to the entire item to do an onclick () Click event, the problem is resolved.

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 post, a parameter pos of type int is passed, which is eventbus executed in activity This pos is used when Oneventmainthread (Categoryitemevent event).
}
});

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.