Fragment的使用

2018-07-15  本文已影响0人  Aptitude

Fragment

Blog首语:今天在android上写绘制曲线的心得,如果想要别人的轮子,可是又对别人的代码毫无头绪,如果长度还可以接受的话,那就自己手敲复制一份,在自己写的过程中去看每个方法的意义,这样对代码就基本熟悉了。

之前也看过两三遍Fragment的用法,也手敲过别人写的代码,可是自己没有上手真正写过,发现今天要用到的时候还是有点模糊,所以决定再整理一下。

基本算法的写法

1.左右侧的碎片布局。

命名为left_fragment.xml和right_fragment.xml,其中布局文件的内容和一般布局的文件一样。

2.左右侧的fragment类,命名为LeftFragment和RightFragment,分别都继承于Fragment。

这里说明一下Fragment会有两个不同的包可以选择,在《第一行代码》中强烈建议使用的是support-v4库中的android.support.v4.app.Fragment,说可以在所有系统版本中保持功能一致性。而我在使用了android.support.v4.app.Fragment时发生了启动app闪退崩溃的现象,在将其改为android.app.Fragment时成功运行。因此在这里建议当发生这种现象时可以改变包试一下能不能解决问题。

  写一下其中一个例子:

  public class LeftFragment extends Fragment{

       publicView onCreateView(LayoutInflater inflater,ViewGroup contatiner,BundlesavedInstanceState){

              Viewview = inflater.inflate(R.layout.left_fragment,container,false);

              returnview;

       }

}

3.之后就是写要将fragment嵌入的activity的代码了。

比如要嵌入activity_main.xml中,则

   <LinearLayout xmlns:tools="http://schemas.android.com/tools"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   android:orientation="vertical" >

<Fragment

         android:id="@+id/left_fragment"

         android:name="com.example.fragmenttest.LeftFragment"

         android:layout_width="0dp"

         android:layout_height="match_parent"

         android:layout_weight="1"/>

<Fragment

         android:id="@+id/right_fragment"

         android:name="com.example.fragmenttest.RightFragment"

         android:layout_width="0dp"

         android:layout_height="match_parent"

         android:layout_weight="1"/>

   </LinearLayout>

动态更新碎片

情景:点击一个按钮后将原来的碎片布局改变为另外一个。

1.创建要更新的碎片。

同时改变要嵌入的activity_main.xml中的相应fragment的代码。新的碎片同上面碎片的创建方法,activity_main.xml中的内容变化如下:

           

    <linearLayout xmlns:tools="http://schemas.android.com/tools"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   android:orientation="vertical" >

<Fragment

         android:id="@+id/left_fragment"

         android:name="com.example.fragmenttest.LeftFragment"

         android:layout_width="0dp"

         android:layout_height="match_parent"

         android:layout_weight="1"/>

   <FrameLayout

         android:id="@+id/right_layout"

         android:name="com.example.fragmenttest.RightFragment"

          android:layout_width="0dp"

         android:layout_height="match_parent"

         android:layout_weight="1"/>

</LinearLayout>

2.可以写一个更新fragment的方法。

  private void replaceFragment(Fragmentfragment){

       //获取FragmentManager

       FragmentManagerfragmentManager = getSupportFragmentManager();

       //开启一个事务

       FragmentTransactiontransaction =fragmentManager.beginTransaction();

       //向容器内添加碎片,需要添加容器ID和要添加的碎片实例,通过replace()实现

       transaction.replace(R.id.right_layout,fragment);

       //提交事务,调用commit()完成

       transaction.commit();

}

模拟返回栈

transation.addToBackStack(null);

碎片和活动的通信

在活动中调用碎片:

RightFragment rightFragment=(RightFragment)getFragmentManager().findFragmentById(R.id.right_fragment);

在碎片中调用活动:

MainActivity activity=(MainActivity)getActivity();

碎片的生命周期(图源《第一行代码》)

理论部分就到这里,最后再贴一下自己写的部分代码:

Fragment.xml

   <FrameLayout  xmlns:tools="http://schemas.android.com/tools"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   tools:context="com.example.stardream.usb_uart.CardioChartFragment">

<TextView

       android:id="@+id/test"

       android:layout_width="match_parent"

       android:layout_height="match_parent"

       android:text="@string/hello_blank_fragment" />

<RelativeLayout

       android:id="@+id/cardiograph"

       android:layout_width="match_parent"

       android:layout_height="match_parent">

<RelativaLayout/>

<FrameLayout/>

Activity_main.xml

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_margin="3dip"

android:layout_weight="1"

android:background="#202020"

android:tag="Write block" >

<Fragment

        android:id="@+id/chart"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:name="com.example.stardream.usb_uart.CardioChartFragment"

/>

Fragment.java

   public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             BundlesavedInstanceState) {

       Viewview=inflater.inflate(R.layout.fragment_cardio_chart,container,false);

       breathWave=(RelativeLayout)view.findViewById(R.id.cardiograph);

       tv=(TextView)view.findViewById(R.id.test);

       initCardiograph();

       receivedMessage("START");

       return view;

}

最终界面显示

(实现了数据传输中对数据的波形图显示,只是下面的Graph部分使用了Fragment):

  不早了,睡觉去~今天的list没有完成,真是糟糕!

上一篇 下一篇

猜你喜欢

热点阅读