第二周总结

fragment可以看做APP 页面跳转,可以看做一个小型的activity,其流程是在main_activity的xml里面可以定一个Fragmentlayout的布局,这个布局用于存放fragment
然后给这个页面设定一个id,在主活动里面通过单击监听事件将建好的fragment通过fragment事务管理的replace函数放到指定的fragment页面d
代码如下:
main_activity.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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bnt1"
        android:text="@string/修改"/>

    <Button
        android:id="@+id/bnt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/replace" />

       <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/framelayout"
        android:background="@color/colorPrimaryDark">

    </FrameLayout>


</LinearLayout>
 

Fragment文件(删除原来的onTouch()函数):

 
package com.example.fragmentmanager;

import android.content.Context;
import android.net.Uri;
import android.nfc.Tag;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class BlankFragment1 extends Fragment {


    private static final String TAG="BlankFragment1" ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle=this.getArguments();
       String loft= bundle.getString("message");
        Log.e(TAG,"信息是"+loft);

    }

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

Main_activity:

 
package com.example.fragmentmanager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTabHost;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.FrameMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import java.io.BufferedReader;

public class MainActivity extends AppCompatActivity {

    private Button bntt1;
    private Button bntt2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bntt1=findViewById(R.id.bnt1);
        bntt2=findViewById(R.id.bnt2);
        bntt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle=new Bundle();
                bundle.putString("message","like");
                BlankFragment1 blankFragment1 = new BlankFragment1();
                blankFragment1.setArguments(bundle);

                replaceFragment(blankFragment1);
            }
        });
        bntt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replaceFragment(new ItemFragment());
            }
        });
    }

    private void replaceFragment(Fragment fragment) {
        //获取fragment管理器管理fragment
        FragmentManager fragmentManager=getSupportFragmentManager();
        //开启事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //将传入的fragment作为参数传递到framelayout界面,即是主函数的xml文件里的个Fragmentlayout布局里面
        fragmentTransaction.replace(R.id.framelayout,fragment);
        //将fragment存入栈中,实现依次退出
        fragmentTransaction.addToBackStack(null);
        //关闭事务
        fragmentTransaction.commit();
    }
}
posted @ 2023-06-03 18:00  cojames  阅读(3)  评论(0编辑  收藏  举报