如何从片段数据同一fragmentactivity内发送到片段? [英] How to send data from fragment to fragment within same fragmentactivity?

查看:143
本文介绍了如何从片段数据同一fragmentactivity内发送到片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个容器,其中有个片段我的第一个片段做工不错,现在我想将它发送到第二个片段
这是我的第一个片段

i have a container in which there are two fragments my 1st fragment is working good now i want to send it to 2nd fragment
This is my 1st Fragment

private class LoadLink extends AsyncTask<String, Void, String[]> {

    @Override
    protected String[] doInBackground(String... params) {
        Document document = null;
        try {
            document = Jsoup.connect(params[0].toString()).timeout(10 * 1000).userAgent("Mozilla").get();

            download = document.getElementsByClass("actions").first().select("a").attr("href").toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String[]{download, params[1]};
    }

}

现在我想知道如何得到它在我的第二个片段

Now i want to know how to receive it in my 2nd fragment

推荐答案

首先传达到承载使用接口作为回调的片段1的活性。然后你就可以传达给fragment2。

First communicate to the activity that hosts the fragment1 using a interface as a callback. Then you can communicate to fragment2.

您找到更多的信息和code段@

You find more info and code snippets @

http://developer.android.com/training/basics/fragments/ communicating.html

例如:

FragmentOne ---> MainActivity ---> FramentTwo

FragmentOne ---> MainActivity ---> FramentTwo

MainActivity实现接口 ReturnData 和覆盖 senData

MainActivity implements the interface ReturnData and overrides senData

public class MainActivity extends Activity implements ReturnData{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentOne newFragment = new FragmentOne();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.container, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();

    }

    @Override
    public void sendData(String result) {
        // TODO Auto-generated method stub
        FragmentTwo newFragment = new FragmentTwo();
        Bundle args = new Bundle();
        args.putString("key",result);
        newFragment.setArguments(args);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.container, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();

    }

}

activity_main.xml

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/container"
    android:layout_height="fill_parent" >

</FrameLayout>

布局有一个的FrameLayout 这是其中添加视图组/替换framgents

The layout has a FrameLayout which is a view group to which you add/replace framgents

FragmentOne.java

FragmentOne.java

FragmentOne使用接口回调到活动中沟通的价值。

FragmentOne uses interface as a callback to the activity to communicate value.

public class FragmentOne extends Fragment {

    public interface ReturnData
    {
        public void sendData(String result);
    }

    ReturnData mCallback;
     @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);

            // This makes sure that the container activity has implemented
            // the callback interface. If not, it throws an exception
            try {
                mCallback = (ReturnData) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString()
                        + " must implement ReturnData");
            }
        }
    TextView tv2;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.frag1,container,false);
        tv2 = (TextView) rootView.findViewById(R.id.textView2);
        Button b= (Button) rootView.findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mCallback.sendData(tv2.getText().toString());
            }

        });
        return rootView;
    }   
}

frag1.xml

frag1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="88dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:text="This is Fragment One" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="82dp"
        android:text="Hello is communicated to Fragment Two on Button Click" />

</RelativeLayout>

FragmentTwo

FragmentTwo

public class FragmentTwo extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.frag2,container,false);
        TextView tv1 = (TextView) rootView.findViewById(R.id.textView1);
        String text = getArguments().getString("key");
        tv1.append(text);
        return rootView;
    }   
}

frag2.xml

frag2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="36dp"
        android:text="Display : " />

</RelativeLayout>

捕捉

这篇关于如何从片段数据同一fragmentactivity内发送到片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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