RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
关于Fragment的简单使用
1、Fragment是为了避免Activity使用的时候,因为不断频繁创建和销毁时消耗时间的这种情况
2、其特点:Fragment是轻量级的Activity,Fragment的使用时不需要再清单文件中注册
(碎片在平板和大屏幕上使用的比较广泛)

Fragment的使用:
一:静态使用Fragment
    静态使用就是把Fragment当成普通的控件,直接写在Activity的布局文件中。
    
    使用步骤:
    1、创建类,并继承Fragment
    
    2、重写Fragment的onCreateView()生命周期方法,并返回一个View
    
    3、在布局文件中使用
    
    注意:这个fragment必须要有唯一的标记,如tag或者id
**创建Fragment类的代码:
    
    public class NewFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_new, container, false);
    }

}
**在布局文件中的代码:
    
    


结果展示:

创新互联主营扎鲁特旗网站建设的网络公司,主营网站建设方案,app软件定制开发,扎鲁特旗h5小程序开发搭建,扎鲁特旗网站营销推广欢迎扎鲁特旗等地区企业咨询

关于Fragment的简单使用

二、动态使用Fragment

首先:需要在要显示的页面中,的布局文件中占位

然后是:

在Activity中:1, 得到Fragment的管理器对象

            2, 开启Fragment的事务处理

            3, 实例化要显示的Fragment 里面进行对数据的操作

            4, 动态显示Fragment

            5, 提交事务

在xml文件中的代码:

使用布局占位

在Activity中的代码:

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

NewFragment myfrg = new NewFragment();
fragmentTransaction.replace(R.id.newfragment_id,myfrg);

fragmentTransaction.commit();


分享标题:关于Fragment的简单使用
网址分享:http://scyingshan.cn/article/igpepe.html