您的位置:首页 > 移动开发 > Android开发

Android快速上手之Fragment的使用

2017-04-07 09:49 162 查看

(本文只针对新手小白,大牛请看完文章再来评价)
Fragment,官方翻译碎片化,可以将多个界面放在同一个Activity中,实现布局之间的解耦,你可以将Fragment理解为一个碎块的Activity,但是,Fragment不能单独存在,他必须要依赖于Activity,另外,Fragment与Activity的关系是同生共死的,所以可以将Activity中的一些操作放在Fragment中完成,增强代码的可读性与复用性,同时也方便维护。Fragment最早是用来给平板做适配用,因为上述的高效性,所以现在手机APP也都使用Fragment。
注释很重要!注释很重要!注释很重要!

接下来讲Fragment的使用。上面说到你可以将Fragment理解为一个碎片化的UI界面,在Activity中加载Fragment有两种方式:

1.在Activity的xml布局文件中添加Fragment,这样Activity在加载布局的时候就会加载上Fragment,但是Fragment也会有自己xml布局文件,所以这种方法的缺点就显而易见了,一是xml的嵌套,二是Fragment和Activity绑在了一起,Activity不能够删除和更换新的Fragment。上代码:

Activity的布局文件

<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Here is the Fragment"
/>
<!--把Fragment当做普通的view加入,需注意name属性是fragment的完整类名-->
<fragment
android:id="@+id/test_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.daily.TestFragment"/>
</LinearLayout>

Fragment的代码,TestFragment.java

package com.daily;

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

public class TestFragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//注意是在onCreateView方法中,用inflate填充Fragment的布局,并返回
return inflater.inflate(R.layout.test_fragment_layout,null);

}

Fragment的布局文件,test_fragment_layout.xml

<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hi,i'm Fragment"
/>
</LinearLayout>

效果图见图



2.Activity代码中动态添加Fragment核心步骤:
2.1,创建FragmentTransaction的实例
2.2,add()/replace方法添加/替换Fragment
2.3,commit()方法提交

先上效果图



上代码:MainActivity.java

public class MainActivity extends AppCompatActivity {

private Button btn_add;
private Button btn_replce;
private android.support.v4.app.FragmentManager fragmentManager;
private android.support.v4.app.FragmentTransaction fragmentTransaction;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_add = (Button) findViewById(R.id.btn_add);
btn_replce = (Button) findViewById(R.id.btn_replace);
//通过 FragmentManager创建FragmentTransaction实例。需注意:
//app包下FragmentManager用getFragmentManager()获取;
// v-4包的FragmentManager用getSupportFragmentManager()获取;
// 当Activity为FragmentActivity或者AppCompatActivity时用getSupportFragmentManager()获取。
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
init();
}

private void init() {
//用add方法添加fragment
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TestFragment testFragment = new TestFragment();
//将fragmentTransaction清空后再添加
if (fragmentTransaction.isEmpty()){
fragmentTransaction.add(R.id.fragment,testFragment).commit();
}else{
fragmentTransaction.remove(testFragment);
}
}
});
//用replace方法添加fragment
btn_replce.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TestFragment testFragment = new TestFragment();
if (fragmentTransaction.isEmpty()){
fragmentTransaction.replace(R.id.fragment,testFragment).commit();
}else{
fragmentTransaction.remove(testFragment);
}
}
});
}
}

Fragment中,TestFragment.java

public class TestFragment extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//inflate()里面三个参数,第一个是Fragment的布局,第二个是容器,第三个是否天假到根布局,需特别注意:
// 第三个参数为true则可能不是view,可写null,当为fale的时候返回就是View,必须写出container,否则布局文件的一些属性显示不出来
return inflater.inflate(R.layout.layout_fragment,container,false);
}
}

Activity布局文件,activity_main.xml

<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
tools:context="cn.daily.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:ori
4000
entation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Click to add "
/>

<Button
android:id="@+id/btn_replace"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn_add"
android:text="Click to replace "
/>
</LinearLayout>

<!--LinearLayout充当Fragment的容器-->
<LinearLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btn_add"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELLO"
android:textSize="50sp"/>
<!--从效果图中可以看出fragment总是添在容器的最下方-->
</LinearLayout>

</LinearLayout>

Fragment布局文件,layout_fragment.xml

<?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:background="#ff0000">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hi,New Fragment"
android:textSize="40sp"
android:textColor="#00f"/>
</LinearLayout>

通过代码添加Fragment可以对Fragment进行一系列操作,功能性远远大于第一种方法,所以我们大都使用第二种方法添加Fragment。
不过,有利就有弊,Fragment并不只是有好处,他的弊端也很难去解决,另外,开发中我们大都将Fragment写成单例模式,单例也会引起其他一些问题,这些问题我们以后解答。

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