首页 > 其他分享 >每日总结

每日总结

时间:2023-02-22 21:48:08浏览次数:29  
标签:总结 androidx fragment 每日 Bundle import android View

Fragment 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();
    }
}

 

 

标签:总结,androidx,fragment,每日,Bundle,import,android,View
From: https://www.cnblogs.com/copyjames/p/17146048.html

相关文章

  • 2023/02/21每日总结
    这两天发现自己之前学习的视频多的是苦苦坚持,没有感受到乐趣,现在找到适合自己的视频,从前面开始学一学。publicstaticvoidmain(String[]args)中args可随意更改,只是变量......
  • 【算法训练营day58】LeetCode739. 每日温度 LeetCode496. 下一个更大元素
    LeetCode739.每日温度题目链接:739.每日温度独上高楼,望尽天涯路直接看题解。慕然回首,灯火阑珊处单调栈一般用来解决一维数组,任意一个元素的右边或者左边第一个比自己......
  • 每日总结(3)
    所用时间:下午5个小时,上午计网课程代码:32博客:0知识点:springboot的控制层访问数据库vue组件化开发,利用vuecli1@RestController2publicclassUserControl......
  • 2023.2.22AcWing蓝桥杯集训·每日一题
    知识点为双指针。AcWing1238.日志统计(蓝桥杯辅导课)题目描述小明维护着一个程序员论坛。现在他收集了一份”点赞”日志,日志共有\(N\)行。其中每一行的格式是:tsid......
  • 【Vue Router】019-动态路由 和 学习总结
    1.19动态路由1.19.1概述添加路由到你的路由上通常是通过​​routes​​​,但是在某些情况下,你可能想在应用程序已经运行的时候添加或删除路由。具有可扩展接口(如​​Vue......
  • 今日学习总结-03-开始学习安卓-安装Genymotion虚拟机
    今天先安装一个Genymotion虚拟机,日后用起来更加方便首先去官网下载:点第二个有vbox的版本然后是简单的安装步骤 注册并登录进入主页面  新建一个虚拟设备  ......
  • 2.22学习总结
    终于实现了Javaweb对数据库的增,查功能!前期失败原因总结如下1.tomcat服务器使用版本为10.0,代码中总出现许多无法解决且不报错的问题导致无法实现正常供能,更换9.0之后问题得......
  • 每日小结(3)
    今天终于写出了一个小模块的增删改查操作,看了黑马的视频,敲了很多次键盘,终于搞得差不多了,还差一些按条件查询的正在学。目前的只是对用信息的增删改查,这几天弄完之后就着手......
  • 2023年2月22日学习总结
    今天继续去实现一个简单的增删改查的项目,通过向同学寻求帮助,按照同学的模板根据自己的需求制作了一个简单的学生信息管理系统,实现了对信息的增删改查。接着下载安装并配置......
  • 2023.2.22每日总结
    从昨天学习json添加依赖包的时候就发现的问题——无法解alibaba的依赖包,maven的setting.xml设置里也配置了阿里的镜像仓库,于今天解决 ......