单击列表视图上的项目后如何刷新数据片段 [英] How to refresh the data after click the item on listview for fragment

查看:29
本文介绍了单击列表视图上的项目后如何刷新数据片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 fragment AFragment 和 BFragment.AFragment 有一个 ListView.BFragment 有一个 Texview 和一个 Button.当用户单击 ListView 上的项目时,Afragment 会将片段和 putString 更改为 BFragment.BFragment 从 Afragment 获取数据并将日期显示到 TextView.我该怎么办?

解决方案

你可以使用 libs https://github.com/greenrobot/EventBus .很简单!!!

EventBus 3 步

定义事件:

 public static class MessageEvent {/* 需要的附加字段 */}

准备订阅者:声明并注释您的订阅方法,可选择指定线程模式:

@Subscribe(threadMode = ThreadMode.MAIN)public void onMessageEvent(MessageEvent event) {/* 做某事 */};

注册和取消注册您的订阅者.例如在 Android 上,Activity 和 Fragment 通常应该根据它们的生命周期进行注册:

 @Override公共无效 onStart() {super.onStart();EventBus.getDefault().register(this);}@覆盖公共无效 onStop() {super.onStop();EventBus.getDefault().unregister(this);}

发布活动:

 EventBus.getDefault().post(new MessageEvent());

I have two fragments both AFragment and BFragment. The AFragment has a ListView. The BFragment has a Texview and a Button. The Afragment will change the fragment and putString to BFragment when the user clicked the item on ListView. The BFragment got the data from Afragment and display the date to TextView. How can I do?

The sample is like that

解决方案

You can use libs https://github.com/greenrobot/EventBus . very easy!!!

EventBus in 3 steps

Define events:

 public static class MessageEvent { /* Additional fields if needed */ }

Prepare subscribers: Declare and annotate your subscribing method, optionally specify a thread mode:

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(MessageEvent event) {/* Do something */};

Register and unregister your subscriber. For example on Android, activities and fragments should usually register according to their life cycle:

 @Override
 public void onStart() {
 super.onStart();
 EventBus.getDefault().register(this);
 }

 @Override
 public void onStop() {
 super.onStop();
 EventBus.getDefault().unregister(this);
 }

Post events:

 EventBus.getDefault().post(new MessageEvent());

这篇关于单击列表视图上的项目后如何刷新数据片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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