欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android学习之fragment

程序员文章站 2022-05-12 15:47:21
...

概念

Fragment (碎片)


         Fragment是Android3.0新增的,它与Activity十分相似,可以说成是微小版的activity,但不能等同于。用来在一个Activity中描述一些行为或一部分用户界面。使用多个Fragment可以在一个单独的Activity中建立多个UI面板,也可以在多个Activity中使用Fragment。
可以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,更重要的是Fragment拥有自己的生命周期和接收、处理用户的事件,这样就不必在Activity写一堆控件的事件处理的代码了。还可以动态的添加、替换和移除某个Fragment。

生命周期


        一个Fragment必须是被嵌入到一个Activity中,它的生命周期直接被其所属的宿主Activity生命周期影响,它的状态会随宿主的状态变化而变化。Fragment的生命周期和它所在的Activity是密切相关的。如果Activity是暂停状态,其中所有的Fragment都是暂停状态;如果Activity是stopped状态,这个Activity中所有的Fragment都不能被启动;如果Activity被销毁,那么它其中的所有Fragment都会被销毁。但是,当Activity在活动状态,可以独立控制Fragment的状态

Android学习之fragment

简单的fragment跳转小样例

activity

package com.example.myapplication3;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {
    BlankFragment blankFragment = new BlankFragment();
    BlankFragment2 blankFragment2 = new BlankFragment2();
    BlankFragment3 blankFragment3= new BlankFragment3();
    BlankFragment4 blankFragment4= new BlankFragment4();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button);
        getSupportFragmentManager().beginTransaction().replace(R.id.lin_layout,blankFragment).commit();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.lin_layout,blankFragment).commit();

            }
        });
        Button button2 = findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.lin_layout,blankFragment2).commit();

            }
        });
        Button button3 = findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.lin_layout,blankFragment4).commit();

            }
        });
        Button button4= findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.lin_layout,blankFragment3).commit();

            }
        });



    }





}

blankfragment

package com.example.myapplication3;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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

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



public class BlankFragment extends Fragment {
    List<Chat> list = new ArrayList<>();

    RecyclerView mRecyclerView;

    String[] na = {"秋秋", "彭彭彭彭", "我又发现", "实验教学准备兼职", "兼职文员", "此群涉嫌违规禁止访问",
            "京东JD.COM", "test", "test", "test", "test", "test"};
    String[] co = {"马上看完了", "312", "http://ffm.qq.com", "学刚:新年快乐", "鹿订记结账小姐姐:[OK]",
            "徐一凡:[发呆]", "iPhone全场抢卷立减700元", "test", "test", "test", "test", "test"};
    String[] ti = {"13:00", "星期二", "2020/1/27", "2020/1/1", "2019/4/27", "13:06:", "13:01", "test",
            "test", "test", "test", "test"};


    int[] id = new int[100];


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

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

        mRecyclerView = getActivity().findViewById(R.id.recyclerView);

        id[0] = (int) R.drawable.b;
        id[1] = (int) R.drawable.a1;
        id[2] = (int) R.drawable.a2;
        id[3] = (int) R.drawable.a3;
        id[4] = (int) R.drawable.b1;
        id[5] = (int) R.drawable.b3;
        id[6] = (int) R.drawable.b4;
        id[7] = (int) R.drawable.b4;
        id[8] = (int) R.drawable.b4;
        id[9] = (int) R.drawable.b4;
        id[10] = (int) R.drawable.b4;
        id[11] = (int) R.drawable.b4;

        for (int i = 0; i < 12; i++) {
            Chat chat = new Chat();
            chat.name = na[i];
            chat.content = co[i];
            chat.time = ti[i];
            chat.pic = id[i];
            list.add(chat);

        }
        mRecyclerView.setAdapter(new Myadapter());
        //
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


    }

    public class Myadapter extends RecyclerView.Adapter<MyRcycleviewhoder> {

        @NonNull
        @Override
        public MyRcycleviewhoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        创建item,及其缓存类
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_layout, parent, false);
            MyRcycleviewhoder myRcycleviewhoder = new MyRcycleviewhoder(view);
            return myRcycleviewhoder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyRcycleviewhoder holder, int position) {
            Chat chat = list.get(position);

            holder.name.setText(chat.name);
            holder.content.setText(chat.content);
            holder.time.setText(chat.time);
            holder.pic.setImageDrawable(getResources().getDrawable((int) chat.pic));
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    public class MyRcycleviewhoder extends RecyclerView.ViewHolder {//缓存处理4级

        public TextView name;
        public TextView content;
        public TextView time;
        public ImageView pic;

        public MyRcycleviewhoder(@NonNull View itemView) {

            super(itemView);//初始化模块

            name = itemView.findViewById(R.id.name);
            content = itemView.findViewById(R.id.content);
            time = itemView.findViewById(R.id.time);
            pic = itemView.findViewById(R.id.pic);
        }
    }
}





本案例的xml文件和此处几乎一样。