Fragment进阶补充&viewpager

Fragment进阶补充&viewpager关于Fragment传值1.Activity给Fragment传值2.Fragment给Activity传值3.Fragment给Fragment传值ViewpagerViewpager是什么?使用步骤自动翻页关于Fragment传值1.Activity给Fragment传值Activity:public class Activity e...
摘要由CSDN通过智能技术生成

关于Fragment传值

1.Activity给Fragment传值

Activity:

public class Activity extends AppCompatActivity {
   
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a_2_f);

        editText =  findViewById(R.id.et_id);
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.linear_layout_id,new ShowContextFragment());
        fragmentTransaction.commit();
    }
    public void click(View view) {
   
        //取到输入的值
        String string = editText.getText().toString();
        //创建fragment对象
        ShowContextFragment showContextFragment = new ShowContextFragment();
        //创建bundle
        Bundle bundle = new Bundle();
        bundle.putString("key",string);
        //给fragment对象赋值
        showContextFragment.setArguments(bundle);
        //动态修改fragment
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.linear_layout_id,showContextFragment);
        fragmentTransaction.commit();
    }
}

xml:

<?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=".A2FActivity">

    <EditText
        android:id="@+id/et_id"
        android:hint="来啊,传点啥吧!"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点我试试"
        android:onClick="click"
        />

    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/linear_layout_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>

</LinearLayout>

Fragment:

public class ShowContextFragment extends Fragment {
   

    private TextView textView;
    public ShowContextFragment() {
   
        // Required empty public constructor
    }
    //现在text里面的值是"hello_blank_fragment",思路就是拿到控件,重新赋值即可.
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   
        View inflate = inflater.inflate(R.layout.fragment_show_context, container, false);
        //1 拿控件
        textView = inflate.findViewById(R
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值