Android开发

Fragment的简单Demo

2019-03-01  本文已影响0人  肥得流油

简介

Fragment 是碎片化的 Activity,是 Activity 的模块化组成部分。例如,可以在 Activity 中使用多个 Fragment 来构建多窗格 UI,以及在多个 Activity 中重复使用某个 Fragment。
Fragment 必须依赖 Activity 存在,其生命周期直接受到宿主 Activity 生命周期的影响。 例如,当 Activity 暂停时,其中所有的 Fragment 也会暂停;当 Activity 被销毁时,所有 Fragment 也会被销毁。

开始Demo编写

目录结构总览:


image.png

界面总览:


image.png

主界面布局: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="horizontal"
    tools:context=".MainActivity">
<!-- LinearLayout 左侧任务栏-->
   <LinearLayout
       android:orientation="vertical"
       android:layout_weight="1"
       android:layout_width="0dp"
       android:layout_height="match_parent">
       <Button
           android:id="@+id/bt_1"
           android:text="1"
           android:layout_width="wrap_content"
           android:layout_height="0dp"
           android:layout_weight="1"/>
       <Button
           android:id="@+id/bt_2"
           android:text="2"
           android:layout_width="wrap_content"
           android:layout_height="0dp"
           android:layout_weight="1"/>


   </LinearLayout>
    <!-- LinearLayout 左侧任务栏-->
    <LinearLayout
        android:orientation="vertical"
        android:layout_weight="8"
        android:layout_width="0dp"
        android:layout_height="match_parent">
<FrameLayout
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp">
    <fragment
        android:id="@+id/fragment_up"
        android:layout_width="match_parent"
        android:name="com.example.lp.myfragment.upFragment"
        android:layout_height="match_parent"
        />

</FrameLayout>
        <FrameLayout
            android:id="@+id/fragment_donw"
            android:layout_weight="9"
            android:layout_width="match_parent"
            android:layout_height="0dp">


        </FrameLayout>

    </LinearLayout>
</LinearLayout>

上方fragment布局:fragment_up.xml:

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

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/tv_up"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</FrameLayout>

下方fragment布局1(2):fragment_down01.xml:(布局fragment_down02.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=".down01Fragment">

    <TextView
        android:id="@+id/tv_01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

upFragment:


public class upFragment extends Fragment {
    private static final String TAG="upFragment";
    private TextView textView;

    public upFragment() {
        // Required empty public constructor
        Log.i(TAG, "upFragment: ");
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.i(TAG, "onCreateView: ");
        View view= inflater.inflate(R.layout.fragment_up, container, false);
        textView=view.findViewById(R.id.tv_up);
        textView.setText("测试上方碎片");
        textView.setTextColor(Color.BLUE);
        return view;
    }



    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.i(TAG, "onAttach: ");

    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(TAG, "onDetach: ");

    }

}

down01Fragment: (down02Fragment相同)

public class down01Fragment  extends Fragment {
    private static final String TAG="down01Fragment";
    private TextView textView;

    public down01Fragment() {
        // Required empty public constructor
        Log.i(TAG, "down01Fragment: ");
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.i(TAG, "down01Fragment onCreateView: ");
        View view= inflater.inflate(R.layout.fragment_down01, container, false);
        textView=view.findViewById(R.id.tv_01);
        textView.setTextColor(Color.GREEN);
        textView.setText("down01Fragment");
        return view;
    }



    @Override
    public void onAttach(Activity activity) {
        Log.i(TAG, "down01Fragment onAttach: ");
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        Log.i(TAG, "down01Fragment onDetach: ");
        super.onDetach();

    }

}

MainActivity:


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG="MainActivity";
    private FragmentTransaction fragmentTransaction;

    private down01Fragment down01Fragment;
    private down02Fragment down02Fragment;

    private Button button1;
    private Button button2;

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

    private void initFragment() {
        Log.i(TAG, "initFragment: ");
       //fragmentTransaction=getFragmentManager().beginTransaction();
        fragmentTransaction=getSupportFragmentManager().beginTransaction();
        down01Fragment=new down01Fragment();
        down02Fragment=new down02Fragment();
        fragmentTransaction.replace(R.id.fragment_donw,down01Fragment);//replace的形式是每次都刷新,add show不会刷新,只是hide隐藏起来
        fragmentTransaction.commit();

    }

    private void initView() {
        Log.i(TAG, "initView: ");
        button1=findViewById(R.id.bt_1);
        button2=findViewById(R.id.bt_2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_1:
                fragmentTransaction.replace(R.id.fragment_donw,down01Fragment);
                fragmentTransaction.commit();
                break;
            case R.id.bt_2:
                fragmentTransaction.replace(R.id.fragment_donw,down02Fragment);
                fragmentTransaction.commit();
                break;
        }
    }
}

需要注意的是,fragment和v4包中的app.fragment的使用的区别
android.support.v4.app.Fragment:可以兼容到安卓1.6的版本
v4包下的fragment所在的activity必须继承FragmentActivity,否则会报错。
使用v4时,FragmentTransaction 的创建要使用:
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
getFragmentManager()对应的是 app;
功能上没啥区别。

附上demo链接:https://github.com/CodeLpea/PracticceOfAndorid.git

image.png

fragment还有add show的形式展示,有空可以自己尝试一下哈。
demo中删了两行代码,会造成app挂掉,
试试找出来哈,自己写一遍的代码才是自己的,对吧。
谢谢观看。

上一篇 下一篇

猜你喜欢

热点阅读