【问题标题】:How can activity call a method inside fragment created dynamically活动如何调用动态创建的片段内的方法
【发布时间】:2014-11-15 10:53:19
【问题描述】:

我有一个使用以下代码创建片段的主要活动

private void launchFragment(int pos)
{
    Fragment f = null;
    String title = null;
    if (pos == 1)
    {
        title = "Friends";
        f = new FriendList();
    }
    else if (pos == 2)
    {
        title = "Notes";
        f = new NoteList();
    }
    else if (pos == 3)
    {
        title = "Projects";
        f = new ProjectList();
    }
    else if (pos == 5)
    {
        title = "About";
        f = new AboutUs();
    }
    else if (pos == 6)
    {
        startActivity(new Intent(this, Login.class));
        finish();
    }
    if (f != null)
    {
        while (getSupportFragmentManager().getBackStackEntryCount() > 0)
        {
            getSupportFragmentManager().popBackStackImmediate();
        }
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, f).addToBackStack(title)
                .commit();
    }
}

这是一个片段的代码。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    View v = inflater.inflate(R.layout.group_chat, null);

    loadConversationList();

    contactName = this.getArguments().getString("contactusername");

    contactId = this.getArguments().getString("contactid");


    ListView list = (ListView) v.findViewById(R.id.list);
    adp = new ChatAdapter();
    list.setAdapter(adp);
    list.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    list.setStackFromBottom(true);

    txt = (EditText) v.findViewById(R.id.txt);
    txt.setInputType(InputType.TYPE_CLASS_TEXT
            | InputType.TYPE_TEXT_FLAG_MULTI_LINE);

    setTouchNClick(v.findViewById(R.id.btnCamera));
    setTouchNClick(v.findViewById(R.id.btnSend));
    return v;
}

我想在上面的片段类中调用一个方法。我无法做到这一点,因为我没有在 XML 文件中给出片段的 ID。我没有使用 XML 加载静态片段。因此,我没有身份证。

我已经在 StackOverFlow 上看到了 this 和 this 问题,但它们并没有解决我的问题。

如果有人知道如何解决这种情况,请提供帮助。

【问题讨论】:

  • 您是否在所有片段中都有该方法,或者您只想在其中一个中调用该方法?

标签: java android android-fragments


【解决方案1】:

首先让你的所有片段都实现一个接口。该接口将返回一个字符串(例如),它将标识您的片段,然后在使用findFragmentById() 获取片段后将您的片段转换为它,如下所示:

创建你的界面

 public interface IFragmentName
 {
    public String getFragmentName();
 }

实现你的接口(例如在NoteList)

public NoteList extends Fragment implements IFragmentName
{
   //Do your stuff...
   public String getFragmentName()
   {
       return "NoteList";
   }

}

在此之后从您的活动中获取您当前的片段

IFragmentName myFragment = (IFragmentName) getSupportFragmentManager().findFragmentById(R.id.content_frame);

最后检查您的 getFragmentName() 值并转换为您想要的片段:

if(myFragment.getFragmentName().equals("NoteList")
{
   NoteList myNoteListFragment = (NoteList) myFragment;
   myNoteListFragment.callMyMethod(); //here you call the method of your current Fragment.
}

我在没有任何 IDE 的情况下编写了这些 sn-ps,所以可能我错过了分号或类似的东西 :)

希望对你有帮助

【讨论】:

  • 谢谢,你的速度超级快……让我一会儿试试……谢谢:)
  • 这是一个了不起的人,再次感谢...我搜索了很多互联网,但找不到这个解决方案...我将答案标记为最佳并给了你一票:)
  • 有一个问题,为什么它返回 null 给 myFragment...我检查了文档,它说 getFragmentById 返回片段或 null,为什么它无法找到 content_frame 片段。
  • 在我的activity的xml中,content_frame是FrameLayout的id,用来放片段
  • 抱歉,我刚刚意识到我使用的是 getSupportFragmentManager() 而不是 getFragmentManager()。请同时更新答案,以免其他人感到困惑......
【解决方案2】:

我知道我来晚了。但这对其他人有用。

如果您使用 ViewPager 来呈现片段,请在您的父 Activity 中使用此代码。

Check Solution here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2021-12-23
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多