java - 使用标签在同一 Activity 的 fragment 之间传输多个字符串

标签 java android android-fragments

我无法弄清楚如何在同一 Activity 上托管的两个 fragment 之间共享数据。

目标: 我想将微调器的选定位置的字符串和选定 ListView 位置的图像 URL 字符串从 fragment A 传输到 fragment B。

尝试: 我在这里阅读了有关此问题的 fragment 文档 http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity 并继续创建了以下接口(interface)以在 fragment 和主机 Activity 之间使用。

public interface OnSelectionListener {
public void OnSelectionListener(String img, String comments );

}

然后我继续在 fragment A 的 onCreateView 方法中实现它,如下所示:

  postList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            ListData link = data.get(position);
            String permalink = link.getComments();
            String largeImg = link.getImageUrl();



            Fragment newFragment = new DetailsView();

            FragmentTransaction transaction = getFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();

                //pass data to host activity
            selectionListener.OnSelectionListener(permalink,largeImg);


        }
    });

也在 onAttach 方法中

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        selectionListener = (OnSelectionListener)getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement onSelectionListener");
    }
}

Host Activity 中,我实现了我编写的接口(interface)并重写了方法,如下所示:

 @Override
public void OnSelectionListener(String img, String comments) {
    DetailsView detailsView = new DetailsView();
    DetailsView dView = (DetailsView)getSupportFragmentManager().findFragmentByTag(detailsView.getCustomTag());
    dView.setInformation(img, comments);
}

在 fragment B 中,我按以下方式设置“标签”

 private String tag;

public void setCustomTag(String tag)
{
    this.tag = tag;
}

public String getCustomTag()
{
    return tag;
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setCustomTag("DETAILS_VIEW");

我的想法是,可以通过从宿主 Activity 调用此方法将信息传递给 fragment B

 void setInformation (String info, String img){
    RedditDetailsTask detailsTask = new RedditDetailsTask(null,DetailsView.this);
    detailsTask.execute(info);
    setDrawable(img);
}

我需要什么: 我想知道如何正确使用标签来让它工作,我没有在我的xml中声明任何 fragment id,而是选择在fragment_container中交换 fragment 。

我也不确定这是否是在 fragment 之间传递多个字符串的好方法。我是一名新手程序员,所以我知道我的逻辑可能看起来很尴尬,但我正在尽力学习正确地做到这一点。如果更多的高级开发人员能够为我指出正确的方向,我将不胜感激。

最佳答案

您不需要使用标签。看一下这个例子。 Activity 实现了一个接口(interface),允许您从 Fragment1 与 Activity 进行对话,然后 Activity 将信息转发到 Fragment2。

我省略了有关 FragmentManager 等的所有 Android 内容。

interface FragmentListener {

    void onTalk(String s1);

}

public class MyActivity extends Activity implements FragmentListener {

    Fragment2 fragment2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        // Find fragment2 and init
    }

    @Override
    public void onTalk(String s1) {
        fragment2.onListen(s1);
    }

    private static class Fragment1 extends Fragment {

        private FragmentListener communication;

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            communication = (FragmentListener) activity;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_one, container, false);
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            // or in an onClick listener
            communication.onTalk("blah blah");
        }
    }

    private static class Fragment2 extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_two, container, false);
        }

        public void onListen(String s1) {
            Log.d("TADA", s1);
        }
    }
}

关于java - 使用标签在同一 Activity 的 fragment 之间传输多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24831177/

相关文章:

java - 按指定顺序递增数字

java - 即使应用程序有子窗口也能保持焦点

android - 找不到Android支持库

android - 如何在用户点击通知时直接打开 Android 回合制多人游戏应用程序?

java - 使用 Otto 的内存泄漏 Leakcanary 报告

android - FragmentActivity 操作栏选项菜单

fragment 内的 Android map

java - 在 HashMap 中搜索会返回所有存储的值,而不仅仅是搜索到的值

java - 如何使用包含其他资源链接的 Spring HATEOAS REST 资源?

java - 如何在android java中将多个gridviews添加到一个scrollview?