Android : Fragment 传递数据 — 简单应用

示例图:

Android : Fragment 传递数据 — 简单应用_第1张图片

创建 Fragment  new -> Fragment -> Fragment(Blank)

MainActivity.java

package com.example.fragmentdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.tv_get_result);

        TwoFragment twoFragment = new TwoFragment();
        //数据传递
        Bundle bundle = new Bundle();
        bundle.putString("name","张三丰");
        twoFragment.setArguments(bundle);

        //接收TwoFragment 传递过来的参数
        twoFragment.setPassingData(new TwoFragment.Myinterface() {
            @Override
            public void getResult(String data) {
                textView.setText(data);
            }
        });

        //动态添加Fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
          // 通过id , 把 twoFragment 添加到LinearLayout布局中
        fragmentTransaction.add(R.id.linear_layout,twoFragment);
        // 替换
//        fragmentTransaction.replace(R.id.linear_layout,twoFragment);
        
        //删除
//        fragmentTransaction.remove(twoFragment);
        // 添加到回退栈  点击返时重新创建fragment  
       fragmentTransaction.addToBackStack(null);
       
        //提交
        fragmentTransaction.commit();
    }
}

HomeFragment.java

package com.example.fragmentdemo;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class HomeFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }
}

TwoFragment.java

package com.example.fragmentdemo;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class TwoFragment extends Fragment {
    private TextView textView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_two, container, false);
        //获取数据
        String name = getArguments().getString("name");
        textView = view.findViewById(R.id.tv_get_data);
        textView.setText(name);

        return view;
    }

    // 给主页面传递参数
    public void setPassingData(Myinterface myinterface){
        String name = "周芷若";
        myinterface.getResult(name);
    }
    //定义一个接口
    public interface Myinterface{
        void getResult(String data);
    }
}

主布局 activity_main.xml





    
    
    
    

    

fragment_home.xml




    
    


fragment_two.xml




    
    
    
    

你可能感兴趣的:(Android,相关,android,笔记)