Fragment


静态加载


新建fragment的布局

新建一个fragment的布局,就是该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">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button" />
</LinearLayout>

新建fragment类

新建fragment类,继承自Fragment或者它的子类,重写onCreateView()方法,在该方法中调用infalter.inflate()方法加载fragment的布局文件,最后返回加载的view对象:

public class MyFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_fragment, container, false);
        return view;
    }
}

修改Activity的布局文件

在需要加载Fragment的Activity对应的布局文件中添加fragment的标签,name属性是全限定类名,要包含Fragment的包名:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/my_fragment"
        android:name="com.example.fragmenttest.MyFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Activity加载布局文件

Activity在onCreate( )方法中调用setContentView()加载布局文件:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

动态加载

getFragmentManager().beginTransaction().replace(R.id.layout, my_fragment).commit();

组件获取

Fragment获得Activity中的组件: 

getActivity().findViewById(R.id.button);


Activity获得Fragment中的组件(根据id和tag都可以):

getFragmentManager.findFragmentByid(R.id.my_fragment);

数据传递

Activity传递数据给Fragment

在Activity中创建Bundle数据包,调用Fragment实例的setArguments(bundle) 从而将Bundle数据包传给Fragment,然后Fragment中调用getArguments获得 Bundle对象,然后进行解析

Fragment传递数据给Activity

在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口, Fragment就可以通过回调接口传数据:

1. 定义一个回调接口(Fragment中)

public interface CallBack {  
    /*定义一个获取信息的方法*/
    public void getResult(String result);
} 

2. 接口回调(Fragment中)

public void getData(CallBack callBack){  
    /*获取文本框的信息,当然你也可以传其他类型的参数*/  
    String msg = editText.getText().toString();  
    callBack.getResult(msg);  
}


3. 使用接口回调方法读数据(Activity中)

leftFragment.getData(new CallBack() {  
 @Override  
       public void getResult(String result) {
            Toast.makeText(MainActivity.this, "-->>" + result, 1).show();  
            }
        }); 

Fragment与Fragment之间的数据互传

找到要接受数据的fragment对象,直接调用setArguments传数据进去,通常的话是replace时,即fragment跳转的时候传数据的,那么只需要在初始化要跳转的Fragment 后调用他的setArguments方法传入数据
如果是两个Fragment需要即时传数据,而非跳转的话,就需要先在Activity获得f1传过来的数据, 再传到f2了,就是以Activity为媒介
示例代码如下:

FragmentManager fManager = getSupportFragmentManager( );
FragmentTransaction fTransaction = fManager.beginTransaction();
Fragmentthree t1 = new Fragmentthree();
Fragmenttwo t2 = new Fragmenttwo();
Bundle bundle = new Bundle();
bundle.putString("key",id);
t2.setArguments(bundle);
fTransaction.add(R.id.fragmentRoot, t2, "123");
fTransaction.addToBackStack(t1);
fTransaction.commit();



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值