Fragment高级

Fragment回退栈

代码展示

public class Main2Activity extends AppCompatActivity {
private FragmentManager supportFragmentManager;
private BlankFragment blankFragment;
private Button butBack;


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

    initViews();
}

private void initViews() {
    supportFragmentManager = getSupportFragmentManager();//获取碎片管理者
    FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();//创建事务
    blankFragment = new BlankFragment();//创建碎片
    butBack = (Button) findViewById(R.id.but_back);

    fragmentTransaction.add(R.id.content_layout2, blankFragment);//添加到布局里
    fragmentTransaction.addToBackStack("1");//添加到回退栈
    fragmentTransaction.commit();//提交事务

    butBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            supportFragmentManager.popBackStack();//弹栈
        }
    });
}

}

注意事项

按返回键时会将fragment弹栈。

Activity给Fragment传值

代码展示

activity的代码跟xml文件
public class MainActivity<fragment> extends AppCompatActivity {
private EditText etText;
private Button btnSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    etText = (EditText) findViewById(R.id.et_text);
    btnSend = (Button) findViewById(R.id.btn_send);

    //获取管理者
    final FragmentManager supportFragmentManager = getSupportFragmentManager();
    //开启事务
    final FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
    //添加哟个碎片
    BlankFragment blankFragment = new BlankFragment();
    fragmentTransaction.add(R.id.fg_fg,blankFragment);
    //提交事务
    fragmentTransaction.commit();

    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //重新开始事务   一个事务只能够提交一次
            FragmentTransaction fragmentTransaction1 = supportFragmentManager.beginTransaction();
            //重新new一个需要传值的Fragment对象
            BlankFragment blankFragment1 = new BlankFragment();
            //替换这个碎片
            fragmentTransaction1.replace(R.id.fg_fg,blankFragment1);
            //提交事务
            fragmentTransaction1.commit();
            //获取输入框的值
            String s = etText.getText().toString();
            //采用bundle对象传值
            Bundle bundle = new Bundle();
            bundle.putString("url",s);
            //用Fragment对象的setArguments方法传bundle对象
            blankFragment1.setArguments(bundle);

        }
    });

}

}

<?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"
android:orientation="vertical">

<EditText
    android:id="@+id/et_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/btn_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="发送"/>
<LinearLayout
    android:id="@+id/fg_fg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.a2_25_fragment_gao_ji.BlankFragment"
    android:orientation="horizontal">
</LinearLayout>
#### fragment的代码跟xml文件
public class BlankFragment extends Fragment {
private TextView tv_show;

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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    //把xml文件转化成View视图对象
    View inflate = inflater.inflate(R.layout.fragment_blank, container, false);
    //获取展示框TextView
    tv_show = inflate.findViewById(R.id.tv_show);
    //获取Bundle对象
    Bundle arguments = getArguments();
    //非空判断
    if(arguments != null){
        //获取输入框的值并展示
        String url = arguments.getString("url");
        tv_show.setText(url);
    }
    return inflate;
}

}

<?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:id="@+id/tv_show"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

效果展示

在这里插入图片描述

注意事项

注意,传递bundle对象的时候是用fragment对象传的。

Fragment给Activity传值

代码展示

activity的代码跟xml文件
public class Main2Activity extends AppCompatActivity implements BlankFragment2.MyListener {

private TextView tvShow2;

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

private void initView() {
    tvShow2 = (TextView) findViewById(R.id.tv_show2);
}
//重写接口中的方法
@Override
public void sendMessage(String s) {
    tvShow2.setText(s);
}

}

<?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">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tv_show2"
    android:textSize="30sp"
    android:hint="提示而已"
    />
<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.a2_25_fragment_gao_ji.BlankFragment2"
    android:id="@+id/fg_fg2"></fragment>
#### fragment的代码跟xml文件
public class BlankFragment2 extends Fragment {

private EditText fmTitleEtId;
private Button fmTitleButtonId;
//定义私有化的接口对象
private MyListener myListener;

@Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);
    //拿到与当前fragment相关的Acticity
    myListener = (MyListener) getActivity();
}

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

//定义自定义接口
public interface MyListener{
    void sendMessage(String s);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View inflate = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
    //获取控件
    fmTitleEtId = inflate.findViewById(R.id.fm_title_et_id);
    fmTitleButtonId = inflate.findViewById(R.id.fm_title_button_id);

    //点击事件
    fmTitleButtonId.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //获取输入框的值
            String s = fmTitleEtId.getText().toString();
            //非空判断
            if(s != null){
                //实现接口中的方法
                myListener.sendMessage(s);
            }

        }
    });


    return inflate;
}

}

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

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fm_title_et_id"
    />

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

效果展示

在这里插入图片描述

注意事项

这里是采用接口回调的方式写的。
注意,接口回调就是在类里面声明一个接口,里面有一个方法。然后声明私有化的接口对象,可以用来执行那个方法,但是还得把这个对象与activity连接起来,在哪里实现这个接口就在那里重写那个方法。

Fragment给Fragment传值

代码展示

fragment代码
public class LeftFragment extends Fragment {

private EditText etSend;
private Button btnSend3;


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    //把xml文件换成View视图对象
    View inflate = inflater.inflate(R.layout.fragment_left, container, false);
    //获取控件
    etSend = (EditText) inflate.findViewById(R.id.et_send);
    btnSend3 = (Button) inflate.findViewById(R.id.btn_send3);

    btnSend3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //获取输入框的值
            String s = etSend.getText().toString();
            //因为两个fragment在一个activity里面,所有可以通过拿到activity里面所有的指定组件.
            TextView textView = getActivity().findViewById(R.id.tv_show3);
            textView.setText(s);

        }
    });
    return inflate ;
}

}

fragment的xml文件
<?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"
tools:context=".LeftFragment"
android:orientation="vertical">

<!-- TODO: Update blank fragment layout -->
<EditText
    android:id="@+id/et_send"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/btn_send3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="发送消息"/>
<?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"
tools:context=".RightkFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:id="@+id/tv_show3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />
#### activity的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"
tools:context=".Main3Activity"
android:orientation="horizontal">
<fragment
    android:id="@+id/fg_left"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:name="com.example.a2_25_fragment_gao_ji.LeftFragment"></fragment>

<fragment
    android:id="@+id/fg_right"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:name="com.example.a2_25_fragment_gao_ji.RightkFragment"></fragment>

效果展示

在这里插入图片描述

注意事项

首先,两个fragment,左边是输入框和发送消息的按钮,右边的是展示框。
然后,两个fragment都得在activity的xml布局中展示,在去敲代码,这样才有效果。

fragment多层嵌套

代码展示

FragmentManager childFragmentManager = getChildFragmentManager();
 FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
 fragmentTransaction.add(R.id.left_fragment_id,new RightFragment());
 fragmentTransaction.commit();

特别说明

就是在Fragment布局中引入另一个Fragment
基本操作相同
值得注意的是获取碎片管理者时需要通过
FragmentManager childFragmentManager = getChildFragmentManager()
其余操作一样

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值