您的位置:首页 > 其它

关于fragment的学习总结(1)

2016-01-20 09:23 363 查看

一:什么是fragment:

fragment :Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视。针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板神马超级大屏的。难道无法做到一个App可以同时适应手机和平板么,当然了,必须有啊。Fragment的出现就是为了解决这样的问题。你可以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,更帅气的是Fragment拥有自己的生命周期和接收、处理用户的事件,这样就不必在Activity写一堆控件的事件处理的代码了。更为重要的是,你可以动态的添加、替换和移除某个Fragment。(以上来自鸿洋前辈的介绍,这篇文章也参考了很多鸿洋前辈的内容,相关的博客请移步 点击打开链接 /article/1336252.html
,还是蛮详细的)


二: fragment的生命周期

下面一幅图是来自官网的图:



onAttach(Activity)

当Fragment与Activity发生关联时调用。

onCreateView(LayoutInflater, ViewGroup,Bundle)

创建该Fragment的视图

onActivityCreated(Bundle)

当Activity的onCreate方法返回时调用

onDestoryView()

与onCreateView想对应,当该Fragment的视图被移除时调用

onDetach()

与onAttach相对应,当Fragment与Activity关联被取消时调用

用的最多的是onCreateView()方法,该方法返回的View将有fragment显示出来

从这幅图也是可以看出的:fragment和activity的生命周期是有联系的,Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期

三:使用fragment的例子详解

下面先上效果图:(定义了两个fragment,一个是用来显示title即左边的按钮区域,右边是内容区域,点击按钮会触发事件)



使用fragment也是很简单,把Fragment当成普通的控件,直接写在Activity的布局文件中
下面是步骤:(来自网络)
1:在主配置文件中Activity中声明此Fragment,就当和普通的View一样,自定name
2:继承Fragment,重写onCreateView决定Fragemnt的布局

贴上测试的代码:
首先是titlt部分的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:background="#ff00"
tools:context="com.example.administrator.fragmenttext2.titleFragment">

<!-- TODO: Update blank fragment layout -->

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:id="@+id/login"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:id="@+id/regic"
android:layout_below="@+id/login"
android:layout_alignEnd="@+id/login"
android:layout_marginTop="102dp" />
</RelativeLayout>


titileFragment
package com.example.administrator.fragmenttext2;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

/**
* A simple {@link Fragment} subclass.
*/
public class titleFragment extends Fragment implements View.OnClickListener {
View view;
public titleFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//关联相关的布局
view= inflater.inflate(R.layout.fragment_title, container, false);
view.findViewById(R.id.login).setOnClickListener(this);
view.findViewById(R.id.regic).setOnClickListener(this);
return view;
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.regic :
Toast.makeText(getActivity(),
"这是注册的fragment的测试 ! ",
Toast.LENGTH_SHORT).show();
case R.id.login :
Toast.makeText(getActivity(),
"这是登录的fragment的测试 ! ",
Toast.LENGTH_SHORT).show();
}
}
}


还有ContentFragment的其布局文件
<FrameLayout 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:background="#3225c7"
tools:context="com.example.administrator.fragmenttext2.ContentFragment">

<!-- TODO: Update blank fragment layout -->
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center"
android:text="@string/hello_blank_fragment" />

</FrameLayout>


package com.example.administrator.fragmenttext2;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* A simple {@link Fragment} subclass.
*/
public class ContentFragment extends Fragment {

public ContentFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_content, container, false);
}

}


MainActivity和其布局文件,fragment中要自定name

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.administrator.fragmenttext2.titleFragment"
android:id="@+id/titleFragment">
</fragment>
<fragment
android:layout_width="0dp"
android:layout_weight="3"
android:name="com.example.administrator.fragmenttext2.ContentFragment"
android:id="@+id/contentFragment"
android:layout_height="match_parent">
</fragment>
</LinearLayout>


package com.example.administrator.fragmenttext2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}


好了,就是这么的简单,这也是最基本的用法,再来看效果图



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: