Android 开发学习(四)

1. Fragment 动态添加和管理


其实对于fragment都是通过transaction来操作的!

  • 通过transaction调用各种方法来操作fragment。
// fragment事务
FragmentTransaction transaction = fragmentManager.beginTransaction();
// 替换R.id.framelayout  fixme 其实对于fragment都是通过transaction来操作的!
transaction.replace(R.id.framelayout,fragment);

通过addToBackStack方法来实现一个栈的效果,进而达到一个返回键的时候出栈的退回的效果。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

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

    </Button>

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

    </Button>

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

    </FrameLayout>

</LinearLayout>

MainActivity

package com.example.fragmentbase;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

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

import java.util.logging.Logger;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.btn);
        // 让MainActivity实现View.OnClickListener接口,进而直接传入this,就可以直接定义onClick方法。
        button.setOnClickListener(this);

        Button button2 = findViewById(R.id.btn2);
        button2.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        // view.getId()获取id值
        switch (view.getId()){
            case R.id.btn:
                replaceFragment(new BlankFragment1());
                break;
            case R.id.btn2:
                replaceFragment(new ItemFragment2());
                break;
        }
    }

    // 动态切换fragment
    private void replaceFragment(Fragment fragment) {
        // fragment管理器
        FragmentManager fragmentManager = getSupportFragmentManager();
        // fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 替换R.id.framelayout  fixme 其实对于fragment都是通过transaction来操作的!
        transaction.replace(R.id.framelayout,fragment);
        // 给当前transaction添加一个栈,参数是栈的名字。这样手机点击返回的时候就是根据出栈来折返fragment页面了。
        transaction.addToBackStack(null);
        // 事务提交
        transaction.commit();
    }

}

BlankFragment1 和 ItemFragment2对象就是创建出来的fragment。

2. Activity 与 Fragment通信(原生方案 Bundle)


通过原生Bundle来进行通信。

MainActivity

// 通过使用bundle来传递值:
Bundle bundle = new Bundle();
bundle.putString("message","hello,world!");
BlankFragment1 bf = new BlankFragment1();
// 给bf对象设置bundle参数
bf.setArguments(bundle);

BlankFragment1

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // 这样就可以直接该对象通过getArguments()方法来获取
    Bundle bundle = getArguments();
    String message = bundle.getString("message");
    Log.d(TAG,"onCreate:" + message);
}


bundle.putParcelable方法的使用:
在这里插入图片描述

3. 动态添加Fragment 5个步骤


在这里插入图片描述

4. Activity 与 Fragment通信(Java语言中类与类自己通信常用方法:接口)


Java语言中类与类自己通信常用方法:接口。

IFragmentCallback接口:

package com.example.fragmentbase;

public interface IFragmentCallback {

    void sendMsgToActivity(String string);

    String getMsgFromActivity(String msg);

}

BlankFragment1:在里面声明一个对应上面的接口即可。

package com.example.fragmentbase;

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;
import android.widget.Button;
import android.widget.Toast;


public class BlankFragment1 extends Fragment {

    private static String TAG = "BlankFragment1";

    private View rootView;

    public BlankFragment1() {
    }

    // 直接声明一个对应的接口变量。
    private IFragmentCallback fragmentCallback;

    public void setFragmentCallback(IFragmentCallback callback){
        fragmentCallback = callback;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 这样就可以直接该对象通过getArguments()方法来获取
        Bundle bundle = getArguments();
        String message = bundle.getString("message");
        Log.d(TAG,"onCreate:" + message);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.fragment_blank1, container, false);
        }
        Button btn3 = rootView.findViewById(R.id.btn_3);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String msgFromActivity = fragmentCallback.getMsgFromActivity(null);
                Toast.makeText(BlankFragment1.this.getContext(),msgFromActivity,Toast.LENGTH_SHORT).show();
                fragmentCallback.sendMsgToActivity("hello,I’m from Fragment");
            }
        });
        return rootView;
    }
}

MainActivity:在里面声明接口对象并进行操作。

  • Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();提示框的效果。
package com.example.fragmentbase;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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

import java.util.logging.Logger;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.btn);
        // 让MainActivity实现View.OnClickListener接口,进而直接传入this,就可以直接定义onClick方法。
        button.setOnClickListener(this);

        Button button2 = findViewById(R.id.btn2);
        button2.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        // view.getId()获取id值
        switch (view.getId()){
            case R.id.btn:
                Bundle bundle = new Bundle();
                bundle.putString("message","hello,world!");
                BlankFragment1 bf = new BlankFragment1();
                // 给bf对象设置bundle参数
                bf.setArguments(bundle);
                bf.setFragmentCallback(new IFragmentCallback() {
                    @Override
                    public void sendMsgToActivity(String msg) {
                        // 传递一个上下文参数
                        Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public String getMsgFromActivity(String msg) {
                        return "hello,world!!!!!";
                    }
                });
                replaceFragment(bf);
                break;
            case R.id.btn2:
                replaceFragment(new ItemFragment2());
                break;
        }
    }

    // 动态切换fragment
    private void replaceFragment(Fragment fragment) {
        // fragment管理器
        FragmentManager fragmentManager = getSupportFragmentManager();
        // fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 替换R.id.framelayout  fixme 其实对于fragment都是通过transaction来操作的!
        transaction.replace(R.id.framelayout,fragment);
        // 给当前transaction添加一个栈,参数是栈的名字。这样手机点击返回的时候就是根据出栈来折返fragment页面了。
        transaction.addToBackStack(null);
        // 事务提交
        transaction.commit();
    }

}

5. Fragment生命周期


对应生命周期如下:
在这里插入图片描述


几个生命周期对应的几个操作:
在这里插入图片描述


总结:
在这里插入图片描述

6. ViewPager2的使用(场景:页面来回切换)


android有两种一个是ViewPager和ViewPager2。推荐使用ViewPager2。

  • ViewPager2具有懒加载的功能。
    在这里插入图片描述

activity_main.xml: 先声明一个ViewPager2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <androidx.viewpager2.widget.ViewPager2
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPager"
        >
    </androidx.viewpager2.widget.ViewPager2>

</LinearLayout>

MainActivity:

  • 配置一个adapter。
package com.example.viewpagerandfragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager2 viewPager2 = findViewById(R.id.viewPager);
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter();
        // 配置viewPager2的adapter
        viewPager2.setAdapter(viewPagerAdapter);
    }
}

item_pager.xml:配置每一页的控件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="#ff4532"
        android:textSize="32dp"
        android:text="hello"
        android:id="@+id/tvTitle" />

</RelativeLayout>

ViewPagerAdapter:

  • 配置holder。
  • 配置list<data>的数据。
  • 在onCreateViewHolder中,创建并返回hoder对象,并且指定好对应的控件组件(对应上面的item_pager.xml)。
  • 在onBindViewHolder中,操作控件的操作。
package com.example.viewpagerandfragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

// ViewPagerAdapter.ViewPagerViewHolder 对应 ViewPagerAdapter类中的ViewPagerViewHolder类。
public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewPagerViewHolder> {

    // 用来改变页面内容的data数据
    private List<String> titles = new ArrayList<>();

    // 改变界面颜色值
    private List<Integer> colors = new ArrayList<>();

    public ViewPagerAdapter(){
        for (int i = 0; i < 10; i++) {
            titles.add("itholmes" + i);
            if (i % 2 == 0){
                colors.add(R.color.black);
            } else {
                colors.add(R.color.blue);
            }
        }
    }

    @NonNull
    @Override
    public ViewPagerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pager,parent,false) 获取item_pager对应的layout。
        return new ViewPagerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pager,parent,false));
    }

    // position来指定item的位置。
    @Override
    public void onBindViewHolder(@NonNull ViewPagerViewHolder holder, int position) {
        // 设置TextView的文本内容
        holder.mTv.setText(titles.get(position));
        // 设置背景颜色 , 注意因为这里是用的colors里面的配置内容所以要用setBackgroundResource方法来定义。
        holder.mContainter.setBackgroundResource(colors.get(position));
    }

    // 这里不能一直返回0,如果一直返回0那么就没有页面。一般设置为对应data的list的size。
    @Override
    public int getItemCount() {
        return 10;
    }

    // 继承一个RecyclerView.ViewHolder,同样操作holder。
    class ViewPagerViewHolder extends RecyclerView.ViewHolder{

        TextView mTv;

        RelativeLayout mContainter;

        public ViewPagerViewHolder(@NonNull View itemView) {
            super(itemView);
            mContainter = itemView.findViewById(R.id.container);
            mTv = itemView.findViewById(R.id.tvTitle);
        }

    }

}

7. Fragment 与 ViewPager 联合操作(经常使用!)


模拟微信下方的导航栏:


总结:
在这里插入图片描述

8. 什么是Activity?


可以理解为每打开一个应用就相当于打开了一个activity。

在这里插入图片描述

9. Activity的创建


默认的Activity:
在这里插入图片描述
如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Demo01">
        <!-- 默认的Activity -->
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <!-- 激活默认的Activity的意图 -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>

</manifest>

两种创建Activity的方式:

第一种:直接new一个,创建一个Activity:
在这里插入图片描述

第二种:手动创建。

  • 1.创建类继承AppCompatActivity。
package com.example.activityjudge;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

// 继承AppCompatActivity
public class MainActivity2 extends AppCompatActivity {

    // 实现onCreate
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 指定layout
        setContentView(R.layout.activity_main2);
    }

}
  • 2.创建类对应的layout。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity2">

</androidx.constraintlayout.widget.ConstraintLayout>
  • 3.要在AndroidManifest.xml清单文件中,进行注册。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Demo01">
        <!-- 默认的Activity -->
        <activity
            android:name=".MainActivity"
            android:exported="true">

            <!-- 激活默认的Activity的意图 -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
        <!-- 组件必须在清单文件里面注册 -->
        <activity
            android:name=".MainActivity2">
        </activity>
    </application>

</manifest>

10. Activity之间的跳转


MainActivity:

  • startActivity(new Intent(this,MainActivity2.class));来进行跳转。
package com.example.activityjudge;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

// 默认的activity
public class MainActivity extends AppCompatActivity {

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

    // 跳转第二个activity的点击事件
    public void startActivity2(View view){
        // 进行跳转
        startActivity(new Intent(this,MainActivity2.class));
    }

}

activity_main.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=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是第一个Activity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startActivity2"
        android:text="跳转activity2" />


</LinearLayout>

MainActivity2:要跳转到的activity。

package com.example.activityjudge;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

// 继承AppCompatActivity
public class MainActivity2 extends AppCompatActivity {

    // 实现onCreate
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 指定layout
        setContentView(R.layout.activity_main2);
    }

}

activity_main2.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=".MainActivity2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是第二个Activity"/>

</LinearLayout>

别忘记在清单里面注册:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Demo01">
        <!-- 默认的Activity -->
        <activity
            android:name=".MainActivity"
            android:exported="true">

            <!-- 激活默认的Activity的意图 -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
        <!-- 组件必须在清单文件里面注册 -->
        <activity
            android:name=".MainActivity2">
        </activity>
    </application>

</manifest>

11. Activity的生命周期


其实和fragment差不多。
在这里插入图片描述

要注意的就是一个activity跳转到另外一个activity会经过那些周期函数:

  • 仅仅会走onPause和onStop两个函数。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值