活动属于片段通话功能 [英] Call functions of Activities Belonging to a Fragment

查看:110
本文介绍了活动属于片段通话功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个片段 Android的下工作,很困惑如何从嵌入式应用程序发送和接收数据。

I am working with multiple Fragments under Android and am confused on how to send and receive data from embedded apps.

对于一个简单的解释,我有一个 ListFragment MapFragment 使用方法说明的here 。这意味着列表家住 ListFragment 类,但是 MapActivity 活动这是 LocalActivityManagerFragment 下调用。

For a simple explanation, I have a ListFragment and a MapFragment using the method explained here. This means that the List lives in the ListFragment class, however the MapActivity is an Activity that is called under the LocalActivityManagerFragment.

一切工作的各个片段列表航点,航点和当前位置上的<$ C语言实现的$ C> MapFragment 。我也知道如何使用 getFragmentManager()来获得个人的片段和使用他们的活动。但是,我不能用这个方法来访问在 MapActivity 函数。我如何可以使用命令,如 goToLocation (以下 MapActivity 创建),如果我点击一个项目上的的ListView ,如果我不能简单地用 getFragmentManager ??

Everything works on the individual Fragments: Lists of waypoints, waypoints and current location implemented on the MapFragment. I also understand how to use getFragmentManager() to get individual Fragments and use their events. However, I cannot use this method to access a function under MapActivity. How can I use a command such as goToLocation (created under MapActivity) if I click on an item on the ListView, if I can't simply use getFragmentManager??

推荐答案

对于一个片段和活动之间的沟通:

For communication between a Fragment and an activity:

活动>片段:

通话的片段使用这个公共的方法。

Call on the public methods of the fragment using this.

TestFragment testFrag = (TestFragment) getFragmentManager().getFragmentById(R.id.testfragment);

if(testFrag != null && testFrag.isAdded())
    testFrag.testMethod("Test");

片段>活动

要发送消息到你需要使用一个接口,这样的片段类添加此活动:(不以任何方式)

To send messages to the activity you need to use an interface so in the fragments class add this: (Not in any method)

public interface testInterface{
    public void testMethod(String test);
}

testInterface mCallback;

@Override
public void onAttact(Activity a){
    try{
        mCallback = (testInterface) a;
    }catch(Exception e){
        //TODO
    }
}

再以任何方法可以去调用这样的:

then in any method you can go and call this:

mCallback.testMethod("hello");

然后为活动做它,所以它实现testInterface 并导入接口

再有这样的方法在活动

@Override
public void testMethod(String testString){

}

:)只要别无所求了。

:) Just ask for anything more.

修改

这是你的问题,我想这是我第一次读的就是你想要的遗憾,仍然可以覆盖你想要的。

From my first read of your question I thought this is what you wanted sorry, could still cover what you wanted.

我觉得你的要求仅仅是一个onItemClick方法吧?

I think what your asking for is just a onItemClick method right?

所以在界面,你可以有

public void onItemClicked(AdapterView<?> parent, View view, int position, long id);

那么当onItemClick被称为片段,你可以在它使用

then when onItemClick has been called in the fragment you can use this in it

mCallback.onItemClicked(parent, view, position, id);

,然后在活动中,鉴于其实施可以让界面

and then in the activity, given its implementing the interface you can have

@Override
public void onItemClicked(AdapterView<?> parent, View view, int position, long id){

}

这篇关于活动属于片段通话功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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