【问题标题】:Communication between fragment in Navigation DrawerNavigation Drawer 中 Fragment 之间的通信
【发布时间】:2016-05-06 05:27:09
【问题描述】:

我正在尝试在单击按钮时将数据从 Fragment A 发送到 NAVIGATION Drawer 的 Fragment B。我尝试了 bundle 和 intent,但它们都不起作用。

在片段 A 中,当我单击数据时,我有 editText 和按钮将数据传递给另一个片段。

在 Fragment B 中有 textView 将显示 editText 数据,但我无法在 Navigation Drawer 中的 Fragment 之间进行通信

【问题讨论】:

  • 到目前为止你尝试了什么。发布您的代码。
  • 我认为你需要在他的片段教程中使用 youtube 上的滑动器检查界面,你会得到一些东西
  • 发布你的代码和logcat.....查看堆栈溢出的答案stackoverflow.com/a/22999749/3678308

标签: android android-fragments


【解决方案1】:

从第一个片段启动片段时

Bundle bundle = new Bundle();
bundle.putString("key", YOUR_EDITVIEW_TEXT);

Fragment fragment = new SECONDFragment();

if (arguments != null) {
    fragment.setArguments(arguments);
}

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, fragment);
ft.addToBackStack("");
ft.commit();

SecondFragment

private String mData;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mData = getArguments().getString("key");
    }


}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.YourLayout, container, false);

    TextView text = (TextView) rootView.findViewById(R.id.yourTextView);
    text.setText(mData);
    return rootView;
}

【讨论】:

    【解决方案2】:

    让您的Activity 进行沟通。

    在您的MainActivity 中获取一个public static 变量,从那里您可以控制导航抽屉的Fragment 替换。当您单击FragmentA 中的按钮时,将EditText 中的值存储到MainActivitypublic static 变量中。然后在加载 FragmentB 时检查 public static 值是否为空。如果不是null,则将该变量的值放在您想要的位置。

    这不是在片段之间传递值的优雅方式,但在您的情况下,它可以正常工作。

    如果您正在寻找如何将值从一个片段传递到另一个片段,请尝试类似的方法。

    // To pass some value from FragmentA
    FragmentB mFragmentB = new FragmentB();
    Bundle args = new Bundle();
    args.putString("VALUE", value);
    mFragmentB.setArguments(args);
    

    并且从您的FragmentB 使用代码来获取传递的值。

    Bundle args = getArguments();
    int value = args.getString("VALUE");
    

    【讨论】:

      【解决方案3】:

      1- 创建应用程序类

      public class MyApplication extends Application {
        @Override
        public void onCreate() {
          super.onCreate();
          mInstance = this;
        }
       private static MyApplication mInstance;
        public static synchronized MyApplication getInstance() {
          return mInstance;
        }
        String mytext;
      
      public String getMytext() {
          return mytext;
      }
      
      public void setMytext(String mytext) {
          this.mytext = mytext;
      }
      }
      

      2-manifast 中的应用名称标签

      <application
          android:name=".MyApplication"
       .......
      

      3- 从第一个片段开始

      MyApplication.getInstance().setMytext("your text here");
      

      4- 来自其他 Fragment

      String text=MyApplication.getInstance().getMytext();
      

      【讨论】:

        【解决方案4】:
        //Put the value
        YourNewFragment ldf = new YourNewFragment ();
        Bundle args = new Bundle();
        args.putString("KEY", "VALUE");
        ldf.setArguments(args);
        
        //Inflate the fragment
        getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
        In onCreateView of the new Fragment:
        
        //Retrieve the value
        String value = getArguments().getString("KEY");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多