zoukankan      html  css  js  c++  java
  • Android中碎片的使用

    Fragment的使用

    其实碎片很简单,但是网上胡乱充数的博文太多了,以至于我们有时候觉得比较乱,今天就来简单讲解一下碎片的使用.
    碎片的使用分为两种,静态添加碎片和动态添加碎片,我们就先来看一下静态添加碎片如何实现.

    静态添加碎片

    首先,先建两个Layout文件,这就是碎片的布局文件,大家可能也发现了,Android Studio里面可以直接快速建立碎片,就像Activity一样,但是这样会生成很多没用的代码,所以我们还是选择自己创建碎片布局.

    两个布局都很简单,里面只有一个居中显示的TextView,下面贴一下代码.

    第一个布局文件:fragment_first.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_light"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="This is the first fragment!"/>
    </LinearLayout>
    

    第二个布局文件fragment_second.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_green_light"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="This is the second fragment!"/>
    </LinearLayout>
    

    现在布局文件建完了,我们该建立他们对应的Fragment了,也就是后台代码了。新建两个类,分别叫FirstFragmentSecondFragment,都继承于Fragment,需要注意一点,我们教程里面所使用的Fragment全都是android.support.v4.app.Fragment这个包下的,这样更有利于程序的兼容性.

    贴一下两个类的代码,也很简单,只是重写了onCreateView方法来加载不同的布局文件.

    public class FirstFragment extends Fragment {
        private View view;//得到碎片对应的布局文件,方便后续使用
    
        //记住一定要重写onCreateView方法
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.fragment_first, container, false);//得到对应的布局文件
            return view;
        }
    }
    
    public class SecondFragment extends Fragment {
        private View view;//得到碎片对应的布局文件,方便后续使用
    
        //记住一定要重写onCreateView方法
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.fragment_second, container, false);//得到对应的布局文件
            return view;
        }
    }
    

    好,基本的工作我们做完了,现在我们用两个Activity展示如何静态添加碎片动态添加碎片.

    静态添加控件的话,需要使用fragment控件,指定其名称是你刚才创建的Fragment就可以,让我们来看一下.

    先贴一下第一个Activity的布局:

    <?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:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="me.worldskills.android.testfragment.MainActivity">
    
        <fragment
            android:id="@+id/main_firstfragment"
            android:name="me.worldskills.android.testfragment.FirstFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"></fragment>
    
        <fragment
            android:id="@+id/main_secondfragment"
            android:name="me.worldskills.android.testfragment.SecondFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"></fragment>
    
        <Button
            android:id="@+id/main_btnGoNext"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="Go to next page"
            android:textAllCaps="false"/>
    </LinearLayout>
    

    其中那个按钮是用来跳转到下一个界面的,也就是动态添加碎片案例的Activity,在这里可以忽略.

    这里我们看见了,两个fragment分别指定nameFirstFragmentSecondFragment,也就是你刚才创建的两个Fragment,一定要记得加上包名.对了,还有一个问题,就是这样的话是没有预览的,如果想要预览,需要在fragment标签中加上一句代码:

    Tools:layout="@layout/布局文件名称"
    

    好了,静态添加碎片就完成了,什么?就这么简单,对啊...就这么简单.

    动态添加碎片

    动态添加碎片我们就不需要用fragment控件了,而是需要用个FrameLayout控件,这是为什么呢,首先我们都知道FrameLayout中的控件,都是从左上角开始显示,不用进行位置控制,动态添加碎片其实就是向容器里面动态添加碎片,而fragment控件只能用来静态绑定一个碎片.

    先贴一下第二个Activity的布局:

    <?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:id="@+id/activity_second"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/second_btnLoadFirst"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Load First!"
            android:textAllCaps="false"/>
    
        <Button
            android:id="@+id/second_btnLoadSecond"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Load Second!"
            android:textAllCaps="false"/>
    
        <FrameLayout
            android:id="@+id/second_fl"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    </LinearLayout>
    

    上面的两个按钮用来加载不同的碎片,而下面的FrameLayout就是碎片显示的容器.

    废话不多说,贴代码:

    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    
    public class SecondActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            //点击第一个按钮的时候加载第一个碎片
            findViewById(R.id.second_btnLoadFirst).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction transaction = fragmentManager.beginTransaction();
                    transaction.replace(R.id.second_fl, new FirstFragment());
                    transaction.commit();
                }
            });
    
            //点击第二个按钮的时候加载第二个碎片
            findViewById(R.id.second_btnLoadSecond).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getSupportFragmentManager().beginTransaction().replace(R.id.second_fl, new SecondFragment()).commit();//简写
                }
            });
        }
    }
    

    getSupportFragmentManager方法用来获得一个碎片管理器对象(使用这个方法的时候注意是android.support.v4.app包下的哦),然后通过这个方法开始一个碎片事物对象,这个对象比较关键,可以用来动态添加碎片,调用它的replace方法,会把指定容器里面的其他控件全部清除掉,然后添加新的碎片进去.在这里就是先把R.id.second_f1里面的控件清空,然后添加传入一个FirstFragment进去.

    替换完之后一定要记得调用commit方法提交,要不然你的所有操作都不会生效,切记.

    Everything has its time and that time must be watched. If you have any question, please contact email linqtosql@qq.com.
  • 相关阅读:
    深度学习实战 Tricks
    Latex 琐碎
    Latex 琐碎
    数据网站(数据集的获取)、打分网站
    数据网站(数据集的获取)、打分网站
    matplotlib —— 添加文本信息(text)
    hdu 1506 Largest Rectangle in a Histogram
    poj2007
    OpenRisc-43-or1200的IF模块分析
    RHEL 6.4 安装svn和apache
  • 原文地址:https://www.cnblogs.com/Fill/p/7343855.html
Copyright © 2011-2022 走看看