Fragment学习笔记
什么是Fragment
1.我对Fragment的认知
我们将Fragment称之为“碎片”,在日常代码编写中,Fragment的使用也很频繁,当前的一些主流APP基本也是使用Fragment做布局的。今天我们学的也主要是Fragment用于不同选项间做切换,我觉得我们可以把Fragment看做是一个Activity,Activity在程序运行时被创建,而Fragment在Activity运行时被创建,可以说Fragment是活在Activity中的一个Activity。
2.Fragment的由来
(1)基于Android系统的设备越来越多,Google提出Fragment的概念也是希望Fragment解决屏幕碎片化问题。
(2)Fragment翻译为碎片,自Android3.0开始引入Fragment的概念,用Fragment替换TabHost。
用Fragment能解决什么问题
(1)使用Fragment可以在一个Activity中实现不同界面的灵活切换。
(2)Fragment解决了Activity间的切换不流畅,布局切换时更轻量。
(3)Fragment可以封装成不同的重用组件,并可以单独其他生命周期和UI布局。
(4)Fragment无需再AndroidManifest中注册,可以在布局文件中直接引用。
Fragment静态加载方法
步骤
1.新建类继承Fragment
2.重写onCreateView方法
3.使用LayoutInflate对象中的inflate方法绑定布局和控件
4.在Activity对应的布局文件中通过标签引用
<?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"
android:orientation="vertical"
tools:context="com.example.administrator.myapplication.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="Hello World!"
android:textSize="20sp"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:id="@+id/f_one"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.administrator.myapplication.Fragment.FragmentOne">
</fragment>
<fragment
android:id="@+id/f_two"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.administrator.myapplication.Fragment.FragmentTwo">
</fragment>
</LinearLayout>
</LinearLayout>
Fragment动态加载方法
步骤
1.新建类继承Fragment
2.重写onCreateView方法
3.使用LayoutInflate对象中的inflate方法绑定布局和控件
4.使用FragmentManager和FragmentTransaction对象进行动态加载
<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"
android:orientation="horizontal"
tools:context="com.example.administrator.myapplication.TianmaoActivity">
<LinearLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/nvzhuang_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="女装" />
<Button
android:id="@+id/nanzhuang_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="男装" />
</LinearLayout>
<FrameLayout
android:id="@+id/shop_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
package com.example.administrator.myapplication;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.administrator.myapplication.Fragment.NanzhuangFragment;
import com.example.administrator.myapplication.Fragment.NvzhuangFragment;
public class TianmaoActivity extends AppCompatActivity implements View.OnClickListener{
private Button nvBtn;
private Button nanBtn;
private NvzhuangFragment nvzhuangFragment;
private NanzhuangFragment nanzhuangFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tianmao);
bindID();
}
private void bindID() {
nvBtn = findViewById(R.id.nvzhuang_btn);
nanBtn = findViewById(R.id.nanzhuang_btn);
nvBtn.setOnClickListener(this);
nanBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
FragmentManager manager = getFragmentManager();//创建FragmentManager对象
FragmentTransaction transaction = manager.beginTransaction();//创建FragmentTransaction对象
switch (view.getId()){
//显示女装的Fragment
case R.id.nvzhuang_btn:
if(nvzhuangFragment==null){
nvzhuangFragment = new NvzhuangFragment();
}
transaction.replace(R.id.shop_content,nvzhuangFragment);
break;
//显示男装的Fragment
case R.id.nanzhuang_btn:
if(nanzhuangFragment==null){
nanzhuangFragment = new NanzhuangFragment();
}
transaction.replace(R.id.shop_content,nanzhuangFragment);
break;
default:
break;
}
transaction.commit();//最后一步提交
}
}
ViewPager+Fragment实现页面滑动切换
package com.example.administrator.myapplication;
import android.media.MediaPlayer;
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 android.widget.Button;
import com.example.administrator.myapplication.Adapter.MyPaperAdapter;
import com.example.administrator.myapplication.Fragment.ContactFragment;
import com.example.administrator.myapplication.Fragment.FriendFragment;
import com.example.administrator.myapplication.Fragment.NewsFragment;
import java.util.ArrayList;
import java.util.List;
public class WxActivity extends AppCompatActivity implements View.OnClickListener{
private Button contactBtn;
private Button friendBtn;
private Button newsBtn;
private ViewPager viewPager;
private ContactFragment contactFragment;
private FriendFragment friendFragment;
private NewsFragment newsFragment;
private List<Fragment> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wx);
bindID();
contactFragment = new ContactFragment();
friendFragment = new FriendFragment();
newsFragment = new NewsFragment();
list.add(contactFragment);
list.add(friendFragment);
list.add(newsFragment);
//创建适配器
MyPaperAdapter adapter = new MyPaperAdapter(getSupportFragmentManager(),list);
//绑定适配器
viewPager.setAdapter(adapter);
}
private void bindID() {
contactBtn = findViewById(R.id.contact_btn);
friendBtn = findViewById(R.id.friend_btn);
newsBtn = findViewById(R.id.news_btn);
viewPager = findViewById(R.id.wx_vp);
contactBtn.setOnClickListener(this);
friendBtn.setOnClickListener(this);
newsBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.contact_btn:
viewPager.setCurrentItem(0);
break;
case R.id.friend_btn:
viewPager.setCurrentItem(1);
break;
case R.id.news_btn:
viewPager.setCurrentItem(2);
break;
}
}
}
package com.example.administrator.myapplication.Adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by Administrator on 2018/3/6.
*/
public class MyPaperAdapter extends FragmentPagerAdapter{
private List<Fragment> mFragmentList;
public MyPaperAdapter(FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.mFragmentList = fragmentList;
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
}