静态Fragment

来源:互联网 发布:nginx 密码访问 编辑:程序博客网 时间:2024/05/06 15:46

http://www.jianshu.com/p/c4d29c390525



一、Fragment知识概要

1. Fragment可以作为Activity界面的一部分出现2. 可以在一个Activity中同时楚翔多个Fragment,并且一个Fragment也可以在多个Activity中使用3. 在Activity运行过程中可以添加、移除或替换Fragment4. Fragment可以相应自己的输入事件,并且有自己的生命周期并且它的生命周期受宿主Activity的影响

二、Fragment的静态加载

要导入v4的包以便向下兼容

import android.support.v4.app.Fragment;

在Activity的layout文件中声明Fragment,需要特别注意fragment标签中的android:name属性指定了在layout中实例化的Fragment类

缺点:布局写死了使用不灵活

(1) 定义一个布局并创建Fragment类(布局代码就不给出来了很简单一个TextView一个Button)

public class MyFragment extends Fragment {    private String text1;    public String getText1() {        return text1;    }    public void setText1(String text1) {        this.text1 = text1;    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        /**         * resource:Fragment需要加载的布局         * root:加载layout的父ViewGroup         * attachToRoot:false不反回父ViewGroup         */        View view = inflater.inflate(R.layout.fragment,container, false);//把layout布局文件转换成View对象         TextView textView = (TextView) view.findViewById(R.id.textView1);        Button button = (Button) view.findViewById(R.id.button1);        button.setText("获取内容");        button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                String value = getText1();                Toast.makeText(getActivity(), "value"+value, 0).show();            }        });        return view;    }}

(2) 在布局文件中声明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" >    <fragment         android:id="@+id/fragment"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:name="com.example.fragment.MyFragment"        /></LinearLayout>

(3) 在Activity中使用该Fragment

public class MainActivity2 extends Activity {    private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main2);//已经加载fragment中的布局文件可以直接findViewById它里面的控件        Button button = (Button) findViewById(R.id.button1);        tv = (TextView) findViewById(R.id.textView1);        button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                tv.setText("textview改变了!");            }        });    }}

三、Fragment的动态加载

优点:运行时Activity允许动态的移除或者替换Fragment

Fragment事务:根据用户的交互情况,对Fragment进行添加、移除、替换、以及执行其他动作,提交给Activity的每一套变化被称作一个事务。每一个事务都是同时执行一套变化,可以在一个事务中设置你想执行的变化,包括add()、remove()、replace(),然后提交给Activity,必须调用commit()方法。如果允许用户通过back键退回到前一个Fragment状态,调用commit()之前可以加入addToBackStack()方法。

注意:Activity动态的添加Fragment必需有一个容器View来容纳Fragment的layout布局

创建两个Fragment类(如上所示就不给出示例代码了)接下来就可以在Fragment中动态加载了:

public class MainActivity3 extends Activity {    private Button button;    private Fragment fragment;    private boolean flag = true;    @Override    protected void onCreate(Bundle savedInstanceState) {                super.onCreate(savedInstanceState);        setContentView(R.layout.main3);        init();        button = (Button) findViewById(R.id.button);        button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                FragmentManager manager = getFragmentManager();                FragmentTransaction fragmentTransaction = manager.beginTransaction();                if (flag) {                    MyFragment4 fragment4 = new MyFragment4();                                           //containerViewId ,fragment                         fragmentTransaction.replace(R.id.layout, fragment4);                    flag = false;                }else{                    MyFragment3 fragment3 = new MyFragment3();                    fragmentTransaction.replace(R.id.layout, fragment3);                    flag = true;                }                fragmentTransaction.commit();            }        });    }    public void init(){        FragmentManager manager = getFragmentManager();        FragmentTransaction fragmentTransaction = manager.beginTransaction();        MyFragment3 fragment3 = new MyFragment3();        fragmentTransaction.add(R.id.layout, fragment3);        fragmentTransaction.commit();    }}

四、Fragment的通信

1.动态加载的Fragment通信

fragment之间永远不能通信都是通过他们所依附的Activity来通信的,通过接口回调的方式来通信

Activity--->Fragment:在activity中创建Bundle数据包,并调用fragment的setArguments(Bundle bundle)方法

fragment--->Activity:在fragment类中定义一个接口,并在他所属的activity中实现该接口,fragment在他的onAttach()方法执行期间捕获该接口的实现,然后就可以调用该接口方法,以便跟activity通信。

下面的代码实现了在一个Activity下有两个Fragment,在一个Fragment中输入值在另一个Fragment显示

public class MainActivity extends Activity implements MyListener {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        FragmentManager manager = getFragmentManager();        FragmentTransaction fragmentTransaction = manager.beginTransaction();        Fragment1 fragment1 = new Fragment1();        fragmentTransaction.add(R.id.layout1, fragment1);        fragmentTransaction.commit();    }       //在mainActivity中实现MyListener接口并实现它未实现的方法,在该方法中传递数据    @Override    public void send(String info) {        Toast.makeText(this, "Activity成功接收" + info, 0).show();                Fragment2 fragment2 = new Fragment2();        Bundle bundle = new Bundle();        bundle.putString("name", info);        fragment2.setArguments(bundle);        FragmentManager manager = getFragmentManager();        FragmentTransaction fragmentTransaction = manager.beginTransaction();        //替换该Fragment                        fragmentTransaction.replace(R.id.layout2, fragment2, "fragment2");        fragmentTransaction.commit();    }}

上面的MainActivity布局就是添加两个Linearlayout,下面是创建两个Fragment,Fragment1的布局就是一个EditText一个Button,Fragment2的布局就是一个TextView。

 public class Fragment1 extends Fragment {    EditText et;    Button enter;    private String info;    @Override    public void onAttach(Activity activity) {        listener = (MyListener) activity;        super.onAttach(activity);    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment1_lay,container, false);        et = (EditText) view.findViewById(R.id.et);        enter = (Button) view.findViewById(R.id.enter);        enter.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                info = et.getText().toString();                listener.send(info);            }        });        return view;    }    public MyListener listener;        //定义一个接口    public interface MyListener{         public void send(String info);    }}
public class Fragment2 extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment2_lay,container, false);        TextView tv = (TextView) view.findViewById(R.id.tv);        tv.setText(getArguments().get("name")+"");//获取传递的值        Toast.makeText(getActivity(), "fragment"+getArguments().get("name")+"", 0).show();        return view;    }}

2.静态加载的fragment通信

  • fragment可以调用getActivity()方法获取他所在的Activity
  • Activity可以调用FragmentManager的findFragmentById()或findFragmentByTag()方法获取Fragment

如果是静态加载的Fragmentn那么向Activity传递数据就很简单了,在MainActivity的onCreate方法中:

        FragmentManager fragmentManager = getFragmentManager();          Fragment fragment = fragmentManager.findFragmentById(R.id.frag);//静态加载在Activity布局中设置的Fragment的ID        MyFragment frag = (MyFragment) fragment;        frag.setText1("fragment静态传值");

在Fragment类中设置它的get、set方法,

private String text1;    public String getText1() {        return text1;    }    public void setText1(String text1) {        this.text1 = text1;    }

下面直接调用就可以了:

         String value = getText1();


文/猿小v(简书作者)
原文链接:http://www.jianshu.com/p/c4d29c390525
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

0 0