Fragment的定义
为了解决手机和平板等各种移动终端的界面显示问题引入了碎片(Fragment)概念,它是一种可以嵌入到活动(Activity)当中的UI片段,能让程序更加合理的利用屏幕空间
Fragment在程序运行时有两种加载方式:静态加载,动态加载。
Fragment静态加载的使用
详细步骤
1.创建空白的Fragment会出现两个文件(Java文件和XML文件)
效果如图:
Java文件
fragment_blank.xml文件
提示:开始创建Fragment时一般去掉如下图中红圈的两个选项
2.
修改fragment_blank.xml文件里的内容(按照你的需求)
简单样例:这里我只写了一个TextView
<?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"
tools:context=".staticLoadingFragment"
android:orientation="horizontal"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="静态加载"
android:gravity="center"
android:background="#ee21f9f9"/>
</LinearLayout>
提示:Fragment的Java文件不需要更改
3.在Activity_main.xml文件中添加Fragment
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SloadingActivity">
<fragment
//id一定要写
android:id="@+id/static_loading_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
//通过name添加进Fragment文件
android:name="com.example.fpl.myfirstfragment.staticLoadingFragment"/>
</android.support.constraint.ConstraintLayout>
同样的,MainActivity.java文件也不需要做更改
那么这个Fragment到此就完成了静态加载
效果如下图:
Fragment 动态加载的使用
ViewPager+Fragment实现页卡滑动效果
少废话直接上案例:
创建三个Fragment文件
Num1Fragment,Num2Fragment,Num3Fragment(我只做了背景色修改,这里就不赘述)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="信息"
android:textSize="20dp"
android:gravity="center"/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="联系人"
android:textSize="20dp"
android:gravity="center"/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="朋友圈"
android:textSize="20dp"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="3dp">
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/message_view"
android:background="#eec7c6c6"/>
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/contract_view"
android:background="#eec7c6c6"/>
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/trends_view"
android:background="#eec7c6c6"/>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/myFirstViewPage"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>
MainActivity.java
package com.example.fpl.killerapplication;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private Num1Fragment num1Fragment;
private Num2Fragment num2Fragment;
private Num3Fragment num3Fragment;
private View messageview;
private View contractview;
private View trendsview;
private ArrayList<Fragment> fragmentArrayList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=findViewById(R.id.myFirstViewPage);
messageview=findViewById(R.id.message_view);
contractview=findViewById(R.id.contract_view);
trendsview=findViewById(R.id.trends_view);
num1Fragment=new Num1Fragment();
num2Fragment=new Num2Fragment();
num3Fragment=new Num3Fragment();
fragmentArrayList.add(num1Fragment);
fragmentArrayList.add(num2Fragment);
fragmentArrayList.add(num3Fragment);
ViewPageAdapter viewPageAdapter=new ViewPageAdapter(getSupportFragmentManager(),fragmentArrayList);
viewPager.setAdapter(viewPageAdapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
messageview.setBackgroundColor(Color.GRAY);
contractview.setBackgroundColor(Color.GRAY);
trendsview.setBackgroundColor(Color.GRAY);
switch (position){
case 0:
messageview.setBackgroundColor(Color.YELLOW);
break;
case 1:
contractview.setBackgroundColor(Color.YELLOW);
break;
case 2:
trendsview.setBackgroundColor(Color.YELLOW);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
效果如图(左右滑动可切换不同Fragment):
Fragment 生命周期
Activity-Fragment传值问题
提示Fragment文件中的绑定布局文件控件问题
原本的(是没有findViewById()方法的):
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_num2, container, false);
}
通过View更改后的
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_num1, container, false);
textView=view.findViewById(R.id.tv_num1);}
1.Fragment—–Activity
首先在Activity.java中写方法(传出)
public void modifyTitle( String x){
r_tv.setText(x);
}
然后在Fragment.java文件中调用该方法:(接收)
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity mainActivity= (MainActivity) getActivity();
mainActivity.modifyTitle("FUN");
}
});
2.Activity—-Fragment
Activity向Fragment传值时需要用到Bundle
简单案例:
Activity.java(传出)
Bundle bundle=new Bundle();
//传的内容
bundle.putString("name","张山");
//你需要传过去的Fragment
num1Fragment.setArguments(bundle);
Fragment.java(接收)
Bundle bundle=getArguments();
String name=bundle.getString("name");
textView.setText(name);