Android Activity中使用Fragment

至于,Activity和Fragment的生命周期再次不描述了,google、百度一大把。

1、首先创建FragmentActivity

2、创建Activity的布局,在此为了描述Fragment的加载方式,布局如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test.MainActivity" >
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取数据" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/button" />
//一、静态方式加载Fragment
    <fragment
        android:id="@+id/id_left_menu"
        android:name="com.example.test.LeftManMenuFragment"//这里也可以换成class="com.example.test.LeftManMenuFragment"
        android:layout_width="100dp"
        android:layout_height="200dp"
        android:layout_below="@id/textView"
        android:layout_gravity="left"
        android:tag="LEFT" />

//二、使用代码动态加载Fragment
    <FrameLayout 
        android:id="@+id/fg_left_menu"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@id/id_left_menu">
    </FrameLayout>
</RelativeLayout>

3、创建Fragment,代码如下:

动态加载方式的Fragment代码:

public class OtherFragment extends BaseFragment{
    private TextView leftCusName;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.other, container, false);
        leftCusName = (TextView)mContentView.findViewById(R.id.tv_left_user_name);

        mContentView.findViewById(R.id.fragment_other_button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "通过FrameLayout加入Fragment的形式Other Fragment", Toast.LENGTH_SHORT).show();

}
});
        return mContentView;
    }
    @Override
    public void onResume() {
        leftCusName.setText("nihao"+"");
        super.onResume();
    }
}

静态方式加载Fragment代码如下:

public class LeftManMenuFragment extends BaseFragment{
    private TextView leftCusName;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_left_menus, container, false);
        leftCusName = (TextView)mContentView.findViewById(R.id.tv_left_user_name);
        return mContentView;
    }
    @Override
    public void onResume() {
        leftCusName.setText("nihao"+"");
        super.onResume();
    }
}

4、看Activity的操作方式

        getSupportFragmentManager().beginTransaction().add(R.id.fg_left_menu,new OtherFragment(), "Other").commit();       
        /*findViewById(R.id.fragment_other_button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "通过FrameLayout加入Fragment的形式Other Fragment", Toast.LENGTH_SHORT).show();
}
});*/
        
        findViewById(R.id.fragment_button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "作为一个控件形式fragment", Toast.LENGTH_SHORT).show();
}
});

总结:

1、静态方式加载Fragment

    <fragment
        android:id="@+id/id_left_menu"
        class="com.example.test.LeftManMenuFragment"
        android:layout_width="100dp"
        android:layout_height="200dp"
        android:layout_below="@id/textView"
        android:layout_gravity="left"
        android:tag="LEFT" />
    标签fragment中使用android:name或者class属性,在Activity中可以直接获取Fragment中的view,直接通过findViewbyID获取到。

2、动态加载Fragment

getSupportFragmentManager().beginTransaction().add(R.id.fg_left_menu,new OtherFragment(), "Other").commit();       
    <FrameLayout 
        android:id="@+id/fg_left_menu"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@id/id_left_menu">

动态情况加载的Fragment,Activity不可以直接操作Fragment中的View,即Activity不可以通过findViewById直接获取View。

怎么在Activity获取Fragment中的控件呢?

当Activity有接口,需要通知Fragment中控件状态改变时候,需要Activity获取Fragment中控件:

1、静态模式加载

2、动态情况,将控件通过getView暴露出来

3、接口回调的方式

4、Eventbus

5、RXjava

6、Handler

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值