在android中,如何将数据从一个片段发送到另一个片段?

在android中,如何将数据从一个片段发送到另一个片段?,android,android-layout,android-fragments,android-activity,Android,Android Layout,Android Fragments,Android Activity,你好 我使用pager和fragment创建了一个简单的选项卡视图。因此,我的视图中有两个选项卡。在一个选项卡中,我有列表视图,其中每行都有文本视图和收藏夹图像按钮。在第二个选项卡中,我需要显示第一个选项卡中最喜欢的所有项目名称。因此,我需要将列表从一个片段发送到另一个列表 这是我的密码 Mainactivity.java public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

你好 我使用pager和fragment创建了一个简单的选项卡视图。因此,我的视图中有两个选项卡。在一个选项卡中,我有列表视图,其中每行都有文本视图和收藏夹图像按钮。在第二个选项卡中,我需要显示第一个选项卡中最喜欢的所有项目名称。因此,我需要将列表从一个片段发送到另一个列表

这是我的密码

Mainactivity.java

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
    ViewPager viewPager;
    FragmentpagerAdapter fragmentpagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ActionBar actionBar =getActionBar();
        viewPager = (ViewPager) findViewById(R.id.pager);
        fragmentpagerAdapter =new FragmentpagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(fragmentpagerAdapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.addTab(actionBar.newTab().setText("Stations").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("fav Station").setTabListener(this));

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
            actionBar.setSelectedNavigationItem(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }


    @Override
    public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {

        viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {

    }
}
活动\u main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pager"
   >


</android.support.v4.view.ViewPager>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#325633"
   >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list_view">
    </ListView>

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/station_name"
        android:padding="10dp"
        android:textColor="#eee345"
        android:textAppearance="?android:textAppearanceLarge"
        />

    <ImageButton android:id="@+id/favorite"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"

        android:background="#00ffffff"
     />


</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ee2333">

</LinearLayout>
fragmentone.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pager"
   >


</android.support.v4.view.ViewPager>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#325633"
   >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list_view">
    </ListView>

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/station_name"
        android:padding="10dp"
        android:textColor="#eee345"
        android:textAppearance="?android:textAppearanceLarge"
        />

    <ImageButton android:id="@+id/favorite"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"

        android:background="#00ffffff"
     />


</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ee2333">

</LinearLayout>
fragmenttwo.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pager"
   >


</android.support.v4.view.ViewPager>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#325633"
   >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list_view">
    </ListView>

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/station_name"
        android:padding="10dp"
        android:textColor="#eee345"
        android:textAppearance="?android:textAppearanceLarge"
        />

    <ImageButton android:id="@+id/favorite"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"

        android:background="#00ffffff"
     />


</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ee2333">

</LinearLayout>

如上图所示,我选择第一站是我最喜欢的站。我需要在第二个选项卡上显示吗?这可能吗?

有几种方法可以做到这一点

最基本也是最推荐的方法是在发送片段中定义一个接口,然后让容器活动实现该接口,这样您就可以将数据从片段发送到活动。您可以从中向接收片段发送数据。
看看谷歌的官方教程:


另一种方式是使用总线事件样式,它比前一种要酷得多。你可以这样做。订阅接收片段,然后从发送片段发送您想要的任何数据,无需通信或编写冗长的接口。

实现这一点的方法很少

最基本也是最推荐的方法是在发送片段中定义一个接口,然后让容器活动实现该接口,这样您就可以将数据从片段发送到活动。您可以从中向接收片段发送数据。
看看谷歌的官方教程:


另一种方式是使用总线事件样式,它比前一种要酷得多。你可以这样做。订阅您的接收片段,然后只从发送片段发送您想要的任何数据,无需通信或编写冗长的接口。

假设您正在将数据从片段A发送到片段B,那么最佳做法是使用接口,然后通过容器活动在片段之间通信。下面是一个小片段,它将提供我想说的内容的框架:

步骤1:在片段中定义一个接口并重写onAttach()方法,使容器活动必须实现该接口并为其方法提供主体

    public interface MyInterfaceListener{
      public void myMethod(yourParameters)//this method will hold parameters which you can then use in your container activity to send it to FragmentB 
}

private MyInterfaceListener listener;
@Override
public void onAttach(Activity activity) {
  super.onAttach(activity);
  if (activity instanceof MyInterfaceListener) {
    listener = (MyInterfaceListener) activity;// This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception 
  } else {
    throw new ClassCastException(activity.toString()
        + " must implemenet MyListFragment.OnItemSelectedListener");
  }
}
现在,在片段A中,可以将值传递给myMethod(),如:
if(listener!=null){
myMethod(yourArguments);
}

步骤2:现在,在容器活动中实现回调接口

public class MyActivity extends Activity implements MyInterfaceListener{
        @Override
         public void myMethod(yourParameters) {
              //do your stuff here. do whatever you want to do with the //parameter list which is nothing but data from FragmentA.
                FragmentB fragment = (FragmentB) getSupportFragmentManager().findFragmentById(R.id.yourFragmentB);
                fragment.methodInFragmentB(sendDataAsArguments);// calling a method in FragmentB and and sending data as arguments. 
        }
}
第三步:在片段B中有一个方法,例如methodInFragmentB(yourParameters)


我希望上面的描述能有所帮助。谢谢。

假设您正在将数据从片段A发送到片段B,那么最佳做法是使用接口,然后通过容器活动在片段之间进行通信。下面是一个小片段,它将提供我想说的内容的框架:

步骤1:在片段中定义一个接口并重写onAttach()方法,使容器活动必须实现该接口并为其方法提供主体

    public interface MyInterfaceListener{
      public void myMethod(yourParameters)//this method will hold parameters which you can then use in your container activity to send it to FragmentB 
}

private MyInterfaceListener listener;
@Override
public void onAttach(Activity activity) {
  super.onAttach(activity);
  if (activity instanceof MyInterfaceListener) {
    listener = (MyInterfaceListener) activity;// This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception 
  } else {
    throw new ClassCastException(activity.toString()
        + " must implemenet MyListFragment.OnItemSelectedListener");
  }
}
现在,在片段A中,可以将值传递给myMethod(),如:
if(listener!=null){
myMethod(yourArguments);
}

步骤2:现在,在容器活动中实现回调接口

public class MyActivity extends Activity implements MyInterfaceListener{
        @Override
         public void myMethod(yourParameters) {
              //do your stuff here. do whatever you want to do with the //parameter list which is nothing but data from FragmentA.
                FragmentB fragment = (FragmentB) getSupportFragmentManager().findFragmentById(R.id.yourFragmentB);
                fragment.methodInFragmentB(sendDataAsArguments);// calling a method in FragmentB and and sending data as arguments. 
        }
}
第三步:在片段B中有一个方法,例如methodInFragmentB(yourParameters)


我希望上面的描述能有所帮助。谢谢。

在片段中创建一个接口,并在相应的活动中实现它

您可以参考此链接:
在片段中创建一个接口,并在相应的活动中实现该接口

您可以参考此链接:

您需要将您的FragmentpagerAdapter添加到您的帖子中。好的,我会发布的。。!!一种解决方案可能是创建一个全局静态变量和函数,您可以将数据保存在其他片段中。您可以使用第一个片段的静态方法来使用该静态变量。您需要将FragmentpagerAdapter添加到您的postok我将发布该。。!!一种解决方案可能是创建一个全局静态变量和函数,您可以将数据保存在其他片段中。您可以使用第一个片段的静态方法使用该静态变量。