Fragment的传值

基础传值

Activity给Fragment传值

Activity的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edt"/>
   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/edbut"
       android:onClick="click"
       android:text="传值"
       />
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fl">

    </FrameLayout>


</LinearLayout>
Fragment的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BlankFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/asd"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>
Activity的逻辑代码
package com.example.day04;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {
    private EditText edt;
    private Button edbut;
    private FrameLayout fl;

    private  FragmentManager supportFragmentManager;
    private FragmentTransaction fragmentTransaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fl = (FrameLayout) findViewById(R.id.fl);
        edt = (EditText) findViewById(R.id.edt);
        edbut = (Button) findViewById(R.id.edbut);

        supportFragmentManager = getSupportFragmentManager();//不是new,而是get出来的
        fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fl,new BlankFragment());//在对应的布局中,添加对应的样式代码片段
        fragmentTransaction.commit();//展示

    }

    public void click(View view) {
        String string = edt.getText().toString();//获取数据
        BlankFragment blankFragment = new BlankFragment();//获取这个视图碎片
        Bundle bundle = new Bundle();//创建用于传值的bundl
        bundle.putString("key",string);//将值存放进去

        blankFragment.setArguments(bundle);//将这个bundle存放到对应的视图碎片中

        supportFragmentManager = getSupportFragmentManager();
        fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fl,blankFragment);//为这个视图碎片更新对应的代码及数据
        fragmentTransaction.commit();

    }
}

Fragment的逻辑代码
package com.example.day04;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class BlankFragment extends Fragment {

    private TextView asd;



    public BlankFragment() {
        // Required empty public constructor
    }


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

        asd = inflate.findViewById(R.id.asd);

        Bundle arguments = getArguments();//获取传输的值

        if(arguments != null){//判断是否为空
            String key = arguments.getString("key");//从中拿去数据
            asd.setText(key);//赋值
        }

        return inflate;
    }

}

Fragment给Activity传值

Activity的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".F2AActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/f2a_tv_id"
        android:textSize="30sp"
        android:hint="提示而已"
        />

    <fragment
        android:name="com.example.day004.fragment.ShowTitleFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/f2a_fm_id">

    </fragment>

</LinearLayout>

Fragment的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".fragment.ShowTitleFragment">
    
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fm_title_et_id"
        android:hint="整的啥吧"
        />

    <Button
        android:id="@+id/fm_title_button_id"
        android:text="发送了啊"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Activity的逻辑代码
package com.example.day004;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.example.day004.fragment.ShowTitleFragment;

public class F2AActivity extends AppCompatActivity implements ShowTitleFragment.MyListener {

    private TextView textView;
    @Override
    public void sendMessage(String string) {
        textView.setText(string);//发送信息
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_f_2_a);
        textView = findViewById(R.id.f2a_tv_id);//获取组件
    }
}

Fragment的逻辑代码
package com.example.day004.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.example.day004.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShowTitleFragment extends Fragment {

    private EditText editText;
    private Button button;

    private MyListener listener;

    public ShowTitleFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        //拿到与当前fragment关联的Activity.
        listener = (MyListener) getActivity(); 
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_show_title, container, false);

       //取到所有控件
        editText = inflate.findViewById(R.id.fm_title_et_id);
        button = inflate.findViewById(R.id.fm_title_button_id);
        //点击事件
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = editText.getText().toString();//获取这个视图组件的数据
                if(string != null){
                    //注意了.这里是父类引用指向了子类对象,其实是activity中的sendmessage方法在执行.
                    listener.sendMessage(string);
                }
            }
        });
		//返回fragment中的布局视图
        return inflate;
    }
   //自定义的接口
    public interface MyListener{
        void sendMessage(String string);
    }

}

Fragment给Fragment传值

Activity的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/lffl"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>


    </LinearLayout>
   <LinearLayout
       android:layout_width="0dp"
       android:layout_weight="1"
       android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/rigfl"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>
   </LinearLayout>

</LinearLayout>
Activity的逻辑代码
package com.example.month12;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        FragmentManager supportFragmentManager2 = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction2 = supportFragmentManager2.beginTransaction();
        fragmentTransaction2.add(R.id.rigfl,new rightFragment(),"right");//先添加要接收数据的视图片段
        fragmentTransaction2.commit();


        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.lffl,new leftFragment(),"left");//再添加要发送数据的视图片段
        fragmentTransaction.commit();
    }

}

LeftFragment的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".leftFragment">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ET"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/but"
        android:text="传输"
        />

</LinearLayout>
LeftFragment的逻辑代码
package com.example.month12;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class leftFragment extends Fragment {
    private EditText ET;
    private Button but;

    private Myfrag myfrag;

    public leftFragment() {
        // Required empty public constructor
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        myfrag = (Myfrag) getActivity().getSupportFragmentManager().findFragmentByTag("right");//获取要传值的地址
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_left, container, false);

        ET = (EditText) inflate.findViewById(R.id.ET);
        but = (Button) inflate.findViewById(R.id.but);

        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = ET.getText().toString();
                if(string != null){
                    //注意了.这里是父类引用指向了子类对象,其实是activity中的sendmessage方法在执行.
                    myfrag.myfrag(string);//并传值
                }
            }
        });

        // Inflate the layout for this fragment
        return inflate;
    }

    public interface Myfrag{//创建接口
        public void myfrag(String s);
    }

}

RightFragment的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".rightFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/righttv"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>
RightFragment的逻辑代码
package com.example.month12;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class rightFragment extends Fragment implements leftFragment.Myfrag {
    private TextView righttv;

    public rightFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_right, container, false);

        righttv = (TextView) inflate.findViewById(R.id.righttv);

        // Inflate the layout for this fragment
        return inflate;
    }

    @Override
    public void myfrag(String s) {//接口回调,填充数据
        righttv.setText(s);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值