在Fragment和Service类中使用Otto [英] Using Otto in Fragment and Service class

查看:50
本文介绍了在Fragment和Service类中使用Otto的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Fragment类中有一个RecyclerView,还有一个Service类,我想在其中将Item添加到我的RecyclerView中.问题是我对如何做到这一点一无所知.所以我问,有人告诉我使用 Otto .现在,我有库存,因为它不起作用.我的代码可能出了点问题,我不知道,这是我第一次使用 Otto .

I have a RecyclerView inside a Fragment class, and I have a Service class where I wanted to add Item into my RecyclerView. The problem is I have no any idea on how can I possibly do that. So I've asked and someone told me to use Otto. And now I'm stock because it's not working. Maybe there's something wrong with my code, i don't know, This is the first time I used Otto.

这就是我实现片段类"的方式

This is how I implement My Fragment Class

public static class MyFragment extends Fragment {

    RecyclerView recyclerView;
    LinearLayoutManager linearLayoutManager;
    static TheRecyclerAdapter theRecyclerAdpater;
    private List<TheData> theDataList;
    public static Bus bus = new Bus(ThreadEnforcer.MAIN);

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.online_devices, container, false);

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);

        theDataList = new ArrayList<>();
        theRecyclerAdpater = new TheRecyclerAdapter(getActivity().getBaseContext(), theDataList);

        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(theRecyclerAdpater);

        bus.register(this); //I already register my fragment
        getActivity().startService(new Intent(getActivity(),MyService.class)); //I call the Service

        return view;
    }

    //I already Subscribe, I am expecting to execute this Function but nothings happened
    @Subscribe
    public void getMessage(String s) {
        TheData a = new TheData(s);
        theDataList.add(a);
        theRecyclerAdpater.notifyDataSetChanged();
    }
}

这是我的服务等级

public class ServerHelperService extends Service {
    public static Bus bus = new Bus(ThreadEnforcer.MAIN);

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate(){
        bus.post("a"); //This where I'm expecting to call the getMessage function in my fragment
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

有人知道为什么它不起作用吗?

Does anyone has an idea why it's not working?

推荐答案

首先,您必须掌握事件总线的概念.在这是3个部分:

First thing first, you must grasp the concept of event bus. In Otto doc, you can only see technical details about how event bus working in text. So for explanation sake, we borrow from EventBus. Here the images explaining how event bus working: Here are 3 parts:

  1. 制作人,事件的制作人.
  2. 事件,生产者发送的事件.
  3. 订户,接收事件的事件的订户.

制作人只能发送事件.因此,您只需要发送事件.当然,您可以在事件中添加有效载荷".因此,要发送事件,您可以使用类似以下内容的方法:

Producer can only sent event. So you need to sent only event. Of course you can add 'payload' to event. So, to send the event, you can use something like:

bus.post(new SampleEvent("Your Data"));

事件只是一个 pojo 类,如下所示:

Event is only a pojo class, something like this:

public class SampleEvent {
  private String message;
  public SampleEvent(String message) {
    this.message = message;
  }

  public getMessage() {
     return message;
  }
}

甚至是空白类:

public class BlankEvent {
}

然后,您应订阅,以等待并通过以下方式接收活动:

Then you should Subscribe for waiting and receiving the event with:

@Subscribe
public void getMessage(SampleEvent event) {
  String message = event.getMessage();
  // Process the message here.
}

我认为,通过阅读这个简单的说明,您将了解代码中存在的问题.

I think, by reading this simple explanation you will understand what are the problems in your code.

但是我建议您改用 EventBus .OttoBus现在已弃用.不要害怕学习曲线,因为它们具有相同的概念.

But I suggesting you use EventBus instead. OttoBus is deprecated now. Don't be afraid of the learning curve, because they share the same concept.

这篇关于在Fragment和Service类中使用Otto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆