如何在Android中从一个Fragment向另一个Fragment传递数据?


此示例演示如何在Android中从一个Fragment向另一个Fragment传递数据。

步骤 1 − 在Android Studio中创建一个新项目,转到文件 ⇒ 新建项目,并填写所有必需的详细信息以创建新项目。

步骤 2 − 将以下代码添加到res/layout/activity_main.xml。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:orientation = "vertical"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent">
   <LinearLayout
      android:id = "@+id/linearlayout01"
      android:layout_width = "fill_parent"
      android:layout_height = "fill_parent"
      android:background = "#ccc"
      android:layout_weight = "1"
      android:orientation = "vertical">
      <fragment android:name = "com.example.myapplication.FirstFragment"
         android:id = "@+id/frag_1"
         android:layout_width = "fill_parent"
         android:layout_height = "fill_parent" />
   </LinearLayout>
   <LinearLayout
      android:id = "@+id/linearlayout02"
      android:layout_width = "fill_parent"
      android:layout_height = "fill_parent"
      android:layout_weight = "1"
      android:background = "#eee"
      android:orientation = "vertical">
      <fragment android:name = "com.example.myapplication.SecondFragment"
         android:id = "@+id/frag_2"
         android:layout_width = "fill_parent"
         android:layout_height = "fill_parent" />
   </LinearLayout>
</LinearLayout>

在上面的代码中,我们使用了Fragment来在两个Fragment之间传递数据。

步骤 3 − 将以下代码添加到src /MainActivity.java

<?xml version = "1.0" encoding = "utf-8"?>
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity implements OnButtonPressListener {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
   @Override
   public void onButtonPressed(String msg) {
      // TODO Auto-generated method stub
      SecondFragment Obj = (SecondFragment) getSupportFragmentManager().findFragmentById(R.id.frag_2);
      Obj.onFragmentInteraction(msg);
   }
}

步骤 4 − 将以下代码添加到src / FirstFragment.java

<?xml version = "1.0" encoding = "utf-8"?>
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FirstFragment extends Fragment{
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
      ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment, null);
      init(root);
      return root;
   }
   OnButtonPressListener buttonListener;
   @Override
   public void onAttach(Activity activity) {
      super.onAttach(activity);
      try {
         buttonListener = (OnButtonPressListener) getActivity();
      } catch (ClassCastException e) {
         throw new ClassCastException(activity.toString() + " must implement onButtonPressed");
      }
   }
   void init(ViewGroup root) {
      TextView but = (TextView)root.findViewById(R.id.text);
      but.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // TODO Auto-generated method stub
            buttonListener.onButtonPressed("Message From First Fragment");
         }
      });
   }
}

步骤 5 − 将以下代码添加到src / SecondFragment.java

<?xml version = "1.0" encoding = "utf-8"?>
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class SecondFragment extends Fragment {
   TextView textView;
   View view;
   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      view = inflater.inflate(R.layout.fragment, container, false);
      return view;
   }
   public void onFragmentInteraction(String uri) {
      Log.d("sai",uri);
      textView = view.findViewById(R.id.text);
      textView.setText(uri);
   }
}

步骤 6 − 将以下代码添加到res/layout/fragment.xml。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout
   xmlns:android = "http://schemas.android.com/apk/res/android"
   android:layout_width = "match_parent"
   android:gravity = "center"
   android:layout_height = "match_parent">
   <TextView
      android:id = "@+id/text"
      android:textSize = "30sp"
      android:text = "Click"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content" />
</LinearLayout>

步骤 7 − 将以下代码添加到src/OnButtonPressListener.java。

<?xml version = "1.0" encoding = "utf-8"?>
public interface OnButtonPressListener {
   public void onButtonPressed(String msg);
}

让我们尝试运行您的应用程序。我假设您已将您的实际Android移动设备连接到您的计算机。要在Android Studio中运行该应用程序,请打开您的项目中的一个活动文件,然后单击运行 工具栏中的图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 –

现在点击第一个Fragment的TextView,它将显示如下所示的结果 –

点击这里下载项目代码

更新于:2019年7月30日

4K+ 浏览量

启动您的职业生涯

通过完成课程获得认证

开始
广告