activity与fragment以及fragment与fragment之间的通信

demo下载地址:https://download.csdn.net/download/rely_on_yourself/10711450

  • activity向其引用的fragment传值

第一步 : Activity中创建Fragment对象 , 调用setArguments(bundle) 方法存储值
第二步 : Fragment中调用getArguments()获取传递的Bundle对象 , 解析获取具体值

具体代码:
Activity Code

public class OneActivity extends AppCompatActivity {

   private FragmentTransaction mFragmentTransaction;
   private FragmentManager mManager;
   private EditText mEt;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_one);
       mEt = findViewById(R.id.et);
       OneFragment oneFragment = new OneFragment();
       mManager = getFragmentManager();
       mFragmentTransaction = mManager.beginTransaction();
       mFragmentTransaction.add(R.id.ll,oneFragment);
       mFragmentTransaction.commit();
   }

   public void sendValue(View view) {
       //创建Bundle对象 , 将需要传递的数据存储到bundle中 , 然后调用fragment的setArguments()方法传递bundle
       String value = mEt.getText().toString().trim();
       OneFragment oneFragment = new OneFragment();
       Bundle bundle = new Bundle();
       bundle.putString("lyx",value);
       oneFragment.setArguments(bundle);

       mManager = getFragmentManager();
       mFragmentTransaction = mManager.beginTransaction();
       mFragmentTransaction.replace(R.id.ll,oneFragment);
       mFragmentTransaction.commit();
   }
}

Fragment Code(注意:此处使用的Fragment是app包下的)

public class OneFragment extends Fragment {

    private TextView mTv;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one, null);
        mTv = view.findViewById(R.id.tv);
        //先调用getArguments()方法获取bundle , bundle对象中根据key值获取传递的数据 , 展示在TextView中
        Bundle bundle = getArguments();
        if (bundle != null) {
            String value = bundle.getString("lyx");
            mTv.setText(value);
        }

        return view;
    }
}
  • fragment向其所属的activity传值

第一步 : Fragment中定义传值的回调接口 , 在声明周期的onAttach()或onCreate()方法中获取接口的实现 .
第二步 : Fragment需要传值的位置调用接口回调方法传值 .
第三步 : Activity实现回调接口重写回调方法获取传递的值 .

具体代码:
Activity Code

public class TwoActivity extends AppCompatActivity implements TwoFragment.MyListener {

    private TextView mTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        mTv = findViewById(R.id.tv);
    }

    @Override
    public void sendMessage(String str) {
        //回传数据进行展示
        if (!TextUtils.isEmpty(str)) {
            mTv.setText(str);
        }
    }
}

Fragment Code

public class TwoFragment extends Fragment {

    private EditText mEt;
    private Button mBtn;
    private MyListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mListener = (MyListener) activity;
    }
    
//    @Override
//    public void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        //此处创建Listener具体的对象
//        //Listener是一个接口  接口是不能直接实例化为具体对象的  这个时候创建对象时  都是创建接口的子类
//        //现在TwoActivity是Listener接口的子类  因为TwoActivity实现了MyListener接口
//        mListener = (MyListener) getActivity();
//    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_two, null);
        mEt = view.findViewById(R.id.et);
        mBtn = view.findViewById(R.id.btn);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String value = mEt.getText().toString().trim();
                //接口回调的形式回传数据
                //java多态   父类的引用指向子类的对象
                mListener.sendMessage(value);
            }
        });
        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        //调用onDetach()方法 , 这个时候要把传递进来的activity对象释放掉
        mListener = null;
    }

    public interface MyListener {
        void sendMessage(String str);
    }

}
  • 属于同一个activity的多个或两个fragment之间的传值

实际开发中 , 同一个Activity中会引用多个Fragment , 多个Fragment之间也会涉及到一个传值的过程 . 实际上Fragment之间传值 , 可以通过上面介绍的两种方式结合起来传值 , fragment先传值给activity , 然后activty再传值给另外一个fragment . 出了这种方式之外 , 还有几种其他方式传值 .

1.调用getFragmentManager().findFragmentById()获取Fragment对象调用方法;
2.getFragmentManager().findFragmentById().getView().findViewById()根据id获取activty中的fragment对象获取fragment视图 , 根据id获取视图中控件对象;
3.getActivity().findViewById()获取当前所属Activity根据id获取View对象.

具体代码:
ThreeLeftFragment Code

public class ThreeLeftFragment extends Fragment {

    private EditText mEt;
    private Button mBtn;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_three_left, null);
        mEt = view.findViewById(R.id.et);
        mBtn = view.findViewById(R.id.btn);
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String value = mEt.getText().toString().trim();

                //方式一:可以调用findFragmentById()方法根据id获得fragment的对象 , 调用fragment中的方法赋值
                ThreeRightFragment fragment = (ThreeRightFragment) getFragmentManager().findFragmentById(R.id.right);
                fragment.setTextView(value);

                //方式二:先调用getFragmentManager()获取fragmentManager对象 , 然后调用findFragmentById()方法获得右侧fragment
                //再调用getView()获得右侧fragment的view对象 , 最后调用view的findViewById获得赋值的控件
//                ThreeRightFragment fragment = (ThreeRightFragment) getFragmentManager().findFragmentById(R.id.right);
//                TextView tv = fragment.getView().findViewById(R.id.tv);
//                tv.setText(value);

                //方式三:先调用getActivity()方法获取所属的activity的对象 , 然后调用findViewById()获取目标控件
//                TextView tv = getActivity().findViewById(R.id.tv);
//                tv.setText(value);


            }
        });
        return view;
    }
}

ThreeRightFragment Code

public class ThreeRightFragment extends Fragment {

    private TextView mTv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_three_right, null);
        mTv = view.findViewById(R.id.tv);
        return view;
    }

    public void setTextView(String string) {
        mTv.setText(string);
    }
}

你可能感兴趣的:(Android)