Fragment传值

***Fragment间传值

*1.利用setArguments(bundle)方法

案列实现效果:当点击左边传值按钮后,右边的fragment才出现界面,之后点击取值按钮,会将值呈现在textView中

activity_main.xml中:

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="passValue"
    android:text="传值"/>

<FrameLayout 
    android:id="@+id/fl_container"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="#999"
    android:layout_weight="1">
</FrameLayout>

content_fragment.xml中:

 <Button 
    android:id="@+id/btn_getValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="取值"/>

<TextView 
    android:id="@+id/tv_value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

MainActivity.java中:

public class MainActivity extends Activity {
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        manager = getFragmentManager();
    }
    
    //当点击传值按钮时,Fragment才加入到Activity中,并传值
    public void passValue(View view){
        FragmentTransaction transaction = manager.beginTransaction();
        ContentFragment fragment = new ContentFragment();   
        Bundle args = new Bundle();
        args.putString("key", "我是Activity传递的信息");
        fragment.setArguments(args);//传递数据
        transaction.add(R.id.fl_container,fragment);
        transaction.commit();
    }
}

ContentFragment.java中:

public class ContentFragment extends Fragment {
    private Bundle arguments;
    private Button btnGetValue;
    private TextView tvValue;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.content_fragment, null);
        btnGetValue = (Button) view.findViewById(R.id.btn_getValue);
        tvValue = (TextView) view.findViewById(R.id.tv_value);
        
        arguments = getArguments();
        
        //在按钮点击事件中取值
        btnGetValue.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if(arguments!=null){
                    String value = arguments.getString("key", "");
                    tvValue.setText("接收的信息="+value);
                }               
            }
        });
        
        return view;
    }
}

1503569-20181015150142833-67714438.png

*2.接口回调

案列实现效果:左右界面开始都已经显示,当点击左边传值按钮后,再点击取值按钮,会将值呈现在textView中

activity_main.xml中:

<Button  
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="传值"
    android:onClick="setValue"
    android:textSize="20sp"/>

<FrameLayout 
    android:id="@+id/fragment_container"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#999">    
</FrameLayout>

content_fragment.xml中:

<Button 
    android:id="@+id/btn_getValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="getValue"
    android:text="取值"
    android:textSize="20sp"/>

<TextView 
    android:id="@+id/tv_getValue"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:textSize="20sp"/>

MainActivity.java中:

public class MainActivity extends Activity {
    private FragmentManager manger;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        manger = getFragmentManager();
        FragmentTransaction transaction = manger.beginTransaction();
        ContentFragment fragment = new ContentFragment();
        transaction.add(R.id.fragment_container, fragment, "content");//第三个参数tag:是fragment唯一的标识
        transaction.commit();
        
    }
    public void setValue(View view){
        //根据标识找到一个fragment对象;
        ContentFragment fragment = (ContentFragment) manger.findFragmentByTag("content");
        fragment.setMsg("我在Activity中");
    }
}

ContentFragment.java中:

public class ContentFragment extends Fragment {
    private Button btn_getValue;
    private TextView tv_getValue;
    private String msg;
    
    public void setMsg(String msg){
        this.msg = msg;
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.content_fragment, null);
        btn_getValue = (Button) view.findViewById(R.id.btn_getValue);
        tv_getValue = (TextView) view.findViewById(R.id.tv_getValue);
        btn_getValue.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if(msg!=null){
                    tv_getValue.setText(msg);
                }else{
                    tv_getValue.setText("值为空!");
                }
            }
        });
        return view;
    }
}

1503569-20181016135825769-1789555267.png

转载于:https://www.cnblogs.com/SanguineBoy/p/9791077.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值