Android Fragment传递数据以及跳转

功能实现建立三个点击事件对三个碎片进行切换,并且在三个碎片之中建立两个点击事件可以实现Fragment与Fragment相互传递数据并跳转。

代码如下:

Main3Activity 
   private Button button1;
    private Button button2;
    private Button button3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        button1=findViewById(R.id.ButtonA);
        button2=findViewById(R.id.ButtonB);
        button3=findViewById(R.id.ButtonC);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment=new OneFragment();//创建待添加的碎片实例
                FragmentManager manager=Main3Activity.this.getSupportFragmentManager();//获取FragmentManager
                FragmentTransaction transaction=manager.beginTransaction();//开启一个事务
                transaction.replace(R.id.name,fragment);//向容器添加或替换碎片
                transaction.commit();//提交事务
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment=new TwoFragment();
                FragmentManager manager=Main3Activity.this.getSupportFragmentManager();
                FragmentTransaction transaction=manager.beginTransaction();
                transaction.replace(R.id.name,fragment);
                transaction.commit();

            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment=new ThreeFragment();
                FragmentManager manager=Main3Activity.this.getSupportFragmentManager();
                FragmentTransaction transaction=manager.beginTransaction();
                transaction.replace(R.id.name,fragment);
                transaction.commit();
            }
        });

    }
activity_main3.xml



    
    


    

        
创建一个叫oneFragment的class并继承Fragment
public class OneFragment extends Fragment {
    private TextView textView1;
    private TextView textView2;
    private Button   button1;
    private Button   button2;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.layout_one,container,false);
        textView1=view.findViewById(R.id.takeA);
        textView2=view.findViewById(R.id.takeB);
        button1=view.findViewById(R.id.skipA);
        button2=view.findViewById(R.id.skipB);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TwoFragment twoFragment=new TwoFragment();//实例化要跳转的fragment对象(也就是你要跳转的下一个碎片)
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                transaction.add(R.id.name,twoFragment).addToBackStack(null).commitAllowingStateLoss();
                Bundle bundle=new Bundle();//声明一个Bundle对象
                String data=textView1.getText().toString();
                String g=textView2.getText().toString();
                Log.d("ff", "onClick: ");
                bundle.putString("msga",data);//用Bundle对象携带需要传递的信息
                bundle.putString("msgg",g);
                twoFragment.setArguments(bundle);//将bundle对象绑定fragment
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThreeFragment threeFragment=new ThreeFragment();
                Log.d("ff", "onClick: 1");
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                Log.d("ff", "onClick: 2");
                transaction.add(R.id.name,threeFragment).addToBackStack(null).commitAllowingStateLoss();
                String b=textView1.getText().toString();
                String k=textView2.getText().toString();
                Log.d("ff", "onClick: 6");
                Bundle bundle=new Bundle();
                bundle.putString("msgb",b);
                bundle.putString("msgk",k);
               threeFragment.setArguments(bundle);
            }
        });
        return view;

    }

layout_one.xml

    
    

    

        
TwoFragment
public class TwoFragment extends Fragment {
    private TextView textView1;
    private TextView textView2;
    private Button button1;
    private Button button2;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.layout_two,container,false);
        textView1=view.findViewById(R.id.takeE);
        textView2=view.findViewById(R.id.takeF);
        button1=view.findViewById(R.id.skipE);
        button2=view.findViewById(R.id.skipF);

        Bundle bundle1=TwoFragment.this.getArguments();
        if (bundle1!=null){//切记要判断内容是否为空,否则程序运行getString方法会报空指针的错误。
            textView1.setText(bundle1.getString("msga"));//msga在oneFragment设置好的key值,在此处获取传递过来的内容。
            textView2.setText(bundle1.getString("msgg"));
        }

        Log.d("ff", "onCreateView: ");

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OneFragment oneFragment=new OneFragment();
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                transaction.add(R.id.name,oneFragment).addToBackStack(null).commitAllowingStateLoss();
                String c=textView1.getText().toString();
                String m=textView2.getText().toString();
                Bundle bundle=new Bundle();
                bundle.putString("msgc",c);
                bundle.putString("msgm",m);
                oneFragment.setArguments(bundle);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThreeFragment threeFragment=new ThreeFragment();
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                transaction.add(R.id.name,threeFragment).addToBackStack(null).commitAllowingStateLoss();
                Bundle bundle=new Bundle();
                String d=textView1.getText().toString();
                String n=textView2.getText().toString();
                bundle.putString("msgb",d);
                bundle.putString("msgk",n);
               threeFragment.setArguments(bundle);
            }
        });
        return view;
    }
   

}

layout_two.xml

    
    

    

        
ThreeFragment
public class ThreeFragment extends Fragment {
    private TextView textView1;
    private TextView textView2;
    private Button button1;
    private Button button2;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.layout_three,container,false);
        textView1=view.findViewById(R.id.takeC);
        textView2=view.findViewById(R.id.takeD);
        button1=view.findViewById(R.id.skipC);
        button2=view.findViewById(R.id.skipD);

        Bundle bundle1=ThreeFragment.this.getArguments();
        if (bundle1!=null) {
            textView1.setText(bundle1.getString("msgb"));
            textView2.setText(bundle1.getString("msgk"));
        }

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OneFragment oneFragment=new OneFragment();
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                transaction.add(R.id.name,oneFragment).addToBackStack(null).commitAllowingStateLoss();
                String e=textView1.getText().toString();
                String o=textView2.getText().toString();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TwoFragment twoFragment=new TwoFragment();
                FragmentTransaction transaction=getFragmentManager().beginTransaction();
                transaction.add(R.id.name,twoFragment).addToBackStack(null).commitAllowingStateLoss();
                Bundle bundle=new Bundle();
                String f=textView1.getText().toString();
                String p=textView2.getText().toString();
                bundle.putString("msga",p);
                bundle.putString("msgg",f);
                twoFragment.setArguments(bundle);
            }
        });


        return view;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}

layout_three.xml

    
    

    

        

你可能感兴趣的:(Android Fragment传递数据以及跳转)