Fragment详解

来源:互联网 发布:耽美文入骨相思知不知 编辑:程序博客网 时间:2024/06/16 19:11

一、Fragment组件作用:Fragment类似于Activity,使得一个界面可以出现在Activity内部,但又不用受Activity约束,拥有自己的生命周期,可以和Activity互相传递数据,在实际项目应用中很常见,它一般作为填充一个FramLayout,可以方便用户随时在同一个Activity中切换不同的界面,一般软件首页都会采用Fragment(之前是TableHost);


二、Fragment生命周期,其和Activity生命周期相似,Activity有7个方法,Fragment拥有6个,唯独少了onRestart()方法,不同点在于切换:

        Fragment互相切换的时候,是一次性销毁现在的fragment,onPouse()--onStop()—onDestroy()再 onCreate()--onStart()---onResume()开启新的Frament

复习下Acitivity,第一个切换到第二个activity时  onPouse(第一个Activity)--onCreate()--onStart()--onResume()--onStop(第一个Acicity),由第二个退回到第一个

onPause()--onRestart()--onStart()--onResume()--onStop()--onDestroy()。


具体实现步骤:

       1、 主界面布局,创建一个帧布局(基本是帧布局)盛装Fragment ;

       2、创建一个类继承Fragment布局,实现onCreateView()方法,将预先定义好的布局填充至Fragmen,return出来;

       3、主界面中创建Fragment对象,创建Fragment管理器,创建FragmentTransaction,调用add方法将其添加进Activity中

      4、传递数据,两边都只需定义方法就可,因为两边都可以轻易获取双方对象,调用方法即可;

主界面布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"    android:layout_height="match_parent"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:showIn="@layout/activity_main" tools:context=".MainActivity"    android:orientation="horizontal">    <LinearLayout        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="match_parent"        android:orientation="vertical">        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="第一个fragment"            android:onClick="click1"/>        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="第二个fragment"            android:onClick="click2"/>        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="第三个fragment"            android:onClick="click3"/>        <EditText            android:id="@+id/et_activity"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <Button            android:text="sure"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="sure"/>    </LinearLayout>   <FrameLayout       android:id="@+id/table"       android:layout_width="0dp"       android:layout_weight="2"       android:layout_height="match_parent"></FrameLayout></LinearLayout>
Fragment界面布局:
<?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:orientation="vertical"    android:background="@android:color/holo_red_light">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="第一个fragment"       />    <TextView        android:id="@+id/et_fra"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="1324561351563"/></LinearLayout>

主界面代码:

public class MainActivity extends AppCompatActivity {    Fragment1 fragment1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        fragment1=new Fragment1();        FragmentManager fragmentManager=getFragmentManager();        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();        //事务是保证界面一起出现        fragmentTransaction.add(R.id.table, fragment1);        //只要是事务,就需提交        fragmentTransaction.commit();    }    public void click1(View view){        fragment1=new Fragment1();        FragmentManager fragmentManager=getFragmentManager();        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();        //事务是保证界面一起出现        fragmentTransaction.add(R.id.table, fragment1);        //只要是事务,就需提交        fragmentTransaction.commit();    }    public void click2(View view){        //将fragment1显示在帧布局中        Fragment2 fragment2=new Fragment2();        FragmentManager fragmentManager=getFragmentManager();        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();        fragmentTransaction.add(R.id.table,fragment2);        fragmentTransaction.commit();    }    public void click3(View view){        //将fragment1显示在帧布局中        Fragment3 fragment3=new Fragment3();        FragmentManager fragmentManager=getFragmentManager();        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();        fragmentTransaction.add(R.id.table,fragment3);        fragmentTransaction.commit();    }    public void sure(View view){        EditText text= (EditText) findViewById(R.id.et_activity);        String name=text.getText().toString();         fragment1.setText(name);    }    public void setText(String text){        EditText name= (EditText) findViewById(R.id.et_activity);        name.setText(text);    }}
fragment1代码(f1,f2,f3)相似:
 
public class Fragment1 extends Fragment {    View v;    TextView textView;    @Nullable    @Override    //返回的View是Fragment1的内容显示在屏幕上    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        v= inflater.inflate(R.layout.fragment1,null);        textView = (TextView) v.findViewById(R.id.et_fra);        return v;    }    public void setText(String text) {        textView.setText(text);    }}

 

0 0
原创粉丝点击