【问题标题】:How to send data from fragment to fragment within same fragment activity?如何在同一片段活动中将数据从片段发送到片段?
【发布时间】:2014-05-04 09:42:19
【问题描述】:

我有一个容器,其中有两个片段,我的第一个片段运行良好,现在我想将它发送到第二个片段。 这是我的第一个片段

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]};
    }

}

现在我想知道如何在我的第二个片段中接收它

【问题讨论】:

  • 将值绑定到一个包,然后使用 setargument() 方法您可以将值发送到片段

标签: java android android-fragments send


【解决方案1】:

首先使用接口作为回调与承载 fragment1 的 Activity 通信。然后就可以和fragment2通信了。

您可以找到更多信息和代码 sn-ps @

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

例子:

FragmentOne ---> MainActivity ---> FramentTwo

MainActivity 实现接口ReturnData 并覆盖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

<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

FragmentOne.java

FragmentOne 使用接口作为 Activity 的回调来传递值。

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

<?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>

片段二

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

<?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>

快照

【讨论】:

  • 好的。好吧,如果您可以帮助我编写代码,那么我是 android 新手,强烈推荐。谢谢
  • @NarenderNishad 如果您是新手,而不是获得更好理解和自己实现的代码。将来会有所帮助。文档有代码 sn-ps。看看文档
  • @NarenderNishad 请不要编辑其他用户对您自己的邮政编码的回答。请将其发布为您自己的答案。我发布了一个带有快照的完整示例,请检查相同
  • @NarenderNishad 如果你有一个 asycntask 作为片段的内部类,你可以在onPostExecute 中调用mCallback.sendData("your data") 并将值传递给托管片段的MainActivity
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 1970-01-01
  • 2012-09-26
相关资源
最近更新 更多