我正在尝试在我的程序中的两个碎片之间传递数据。它只是一个存储在列表中的简单字符串。该列表在片段A中公开,当用户单击列表项时,我需要它显示在片段B中。内容提供者似乎只支持ID,所以这将不起作用。有什么建议吗?
发布于 2015-05-21 07:16:59
你为什么不用Bundle呢?在您的第一个片段中,这里是如何设置它的:
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);然后在第二个片段中,使用以下命令检索数据:
Bundle bundle = this.getArguments();
int myInt = bundle.getInt(key, defaultValue);Bundle为许多数据类型提供了put方法。请参阅http://developer.android.com/reference/android/os/Bundle.html
发布于 2013-02-09 21:36:47
因此,假设您有控制Frag A和Frag的Activity AB。在Frag中,您需要一个Activity AB可以实现的接口。在示例android代码中,它们具有:
private Callbacks mCallbacks = sDummyCallbacks;
包含此片段的所有活动都必须实现的/*A回调接口。此机制允许向活动通知项目选择。*/
public interface Callbacks {
/*Callback for when an item has been selected. */
public void onItemSelected(String id);
}
/*A dummy implementation of the {@link Callbacks} interface that does nothing. Used only when this fragment is not attached to an activity. */
private static Callbacks sDummyCallbacks = new Callbacks() {
@Override
public void onItemSelected(String id) {
}
};回调接口放在您的一个片段(假设是片段A)中。我认为这个回调接口的用途就像是Frag A中的一个嵌套类,任何活动都可以实现它。因此,如果片段A是一个TV,那么CallBacks就是允许活动AB使用片段A的TV遥控器(接口)。我可能在细节上错了,因为我是一个菜鸟,但我确实让我的程序在所有屏幕尺寸上都能完美地工作,这就是我使用的。
因此,在片段A中,我们有:(我取自Android的示例程序)
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
//mCallbacks.onItemSelected( PUT YOUR SHIT HERE. int, String, etc.);
//mCallbacks.onItemSelected (Object);
}在Activity AB内部,我们覆盖onItemSelected方法:
public class AB extends FragmentActivity implements ItemListFragment.Callbacks {
//...
@Override
//public void onItemSelected (CATCH YOUR SHIT HERE) {
//public void onItemSelected (Object obj) {
public void onItemSelected(String id) {
//Pass Data to Fragment B. For example:
Bundle arguments = new Bundle();
arguments.putString(“FragmentB_package”, id);
FragmentB fragment = new FragmentB();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction().replace(R.id.item_detail_container, fragment).commit();
}所以在活动AB中,你基本上是把所有东西都放到一个Bundle中,然后把它传递给B。如果你不确定如何使用Bundle,那就去查查这个类。
我基本上是按照Android提供的示例代码来做的。就是那个有DummyContent的人。当你创建一个新的安卓应用程序包时,它的标题是MasterDetailFlow。
发布于 2016-04-08 21:47:47
1-第一种方式是定义一个接口
public interface OnMessage{
void sendMessage(int fragmentId, String message);
}
public interface OnReceive{
void onReceive(String message);
}2-在您的活动中实现OnMessage接口
public class MyActivity implements OnMessage {
...
@Override
public void sendMessage(int fragmentId, String message){
Fragment fragment = getSupportFragmentManager().findFragmentById(fragmentId);
((OnReceive) fragment).sendMessage();
}
}3-在您的代码片段中实现OnReceive接口
public class MyFragment implements OnReceive{
...
@Override
public void onReceive(String message){
myTextView.setText("Received message:" + message);
}
}这是处理片段之间消息传递的样板版本。
在片段之间处理数据通道的另一种方法是使用事件总线。
1-注册/注销到事件总线
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}2-定义事件类
public class Message{
public final String message;
public Message(String message){
this.message = message;
}
}3-将此事件发布到应用程序中的任何位置
EventBus.getDefault().post(new Message("hello world"));4-订阅该事件以在片段中接收它
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessage(Message event){
mytextview.setText(event.message);
}有关事件总线模式的更多details, use cases, and an example project。
https://stackoverflow.com/questions/5194548
复制相似问题