Android 高级应用开发第一章--Fragment
一.什么是Fragment
Fragment,我们称之为“碎片”。大家可以把它理解为一张张的便利贴。当我们在写一个Activity时,经常会需要对一个或多个界面进行变化。所以,当我们需要写一个新界面时,只要编写一个Fragment就可以搞定,就好像在每一张空白的纸上,贴上一张写好的便利贴。你可以随便撕掉,随便修改。这样可以方便程序员的工作。
二.用Fragment能解决什么问题?
a.使用Fragment可以使得在一个Activity中实现不同页面的灵活切换。
b.当一款App同时推出手机版和pad版本时,程序猿不需要再分别编写多套布局方案,通过Fragment设计的布局可以灵活的解决不同屏幕分辨率的适配问题。
三.Fragment静态加载方法(附代码)
步骤 :
a.新建类继承Fragment。
b.重写onCreatview方法。
c.使用LayoutInflater对象中的inflate方法绑定布局和控件。
d.在Activity对应的布局文件中通过fragment标签引用。
具体详细过程如下:
1.首先创建一个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.lenovo.myapplication.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="Hello World!"
android:textSize="30sp"
android:gravity="center" />
<fragment
android:id="@+id/fragmentA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.lenovo.myapplication.FragmentA"
/>
</LinearLayout>
2.创建一个Fragment,在其布局文件中写
<fragment 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="#fff111"
tools:context="com.example.lenovo.myapplication.FragmentA">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:text="hello man"
android:textSize="30sp"/>
</fragment>
3.在所写的fragment.java中写
package com.example.lenovo.myapplication;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class FragmentA extends Fragment {
public FragmentA() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_a, container, true);
return view;
}
}
四.Fragment动态加载过程(附代码)
1.创建一个新的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"
tools:context="com.example.lenovo.myapplication.TaobaoActivity">
<LinearLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/nvzhuang"
android:layout_width="80dp"
android:layout_height="50dp"
android:text="女装" />
<Button
android:id="@+id/nanzhuang"
android:layout_width="80dp"
android:layout_height="50dp"
android:text="男装" />
</LinearLayout>
<FrameLayout
android:id="@+id/shopcenter"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
2.创建一个nanzhuang Fragmment,在其布局文件中写:
<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="@color/colorPrimary"
tools:context="com.example.lenovo.myapplication.NanzhuangFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="丑男就是你"
android:textSize="40sp"
android:gravity="center"/>
</FrameLayout>
3.创建一个nvzhuang Fragment在其布局文件中写:
<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="@color/colorAccent"
tools:context="com.example.lenovo.myapplication.NvzhuangFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="美女一枚"
android:textSize="40sp"
android:gravity="center"/>
</FrameLayout>
4.最后在nanzhuang和nvzhuang fragment.java这样写:
package com.example.lenovo.myapplication;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class NanzhuangFragment extends Fragment {
public NanzhuangFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_nanzhuang, container, false);
return view;
}
}
五.Viewpager+Fragment 实现页卡滑动切换(附代码)
1.首先创建一个类似于微信界面的Activity
2..创建一个消息Fragment
package com.example.lenovo.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class NewFragment extends Fragment {
public NewFragment() {
// 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_new, container, false);
}
}
3.创建一个朋友圈Fragment
package com.example.lenovo.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class FriendFragment extends Fragment {
public FriendFragment() {
// 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_friend, container, false);
}
}
4.创建一个联系人Fragment
package com.example.lenovo.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class ContactFragment extends Fragment {
public ContactFragment() {
// 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_contact, container, false);
}
}
5.在微信Activity布局文件中写:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.lenovo.myapplication.WxActivity">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/chat"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="50dp"
android:text="聊天"/>
<Button
android:id="@+id/friend"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="50dp"
android:text="好友"/>
<Button
android:id="@+id/news"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="50dp"
android:text="通讯录"/>
</LinearLayout>
</RelativeLayout>
6.在朋友圈,联系人以及消息Fragment的布局文件这样写:
<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"
tools:context="com.example.lenovo.myapplication.NewFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
6.**最后运用viewpager和adapter实现界面的滑动切换**
package com.example.lenovo.myapplication;
import android.support.v4.app.FragmentManager;
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.lenovo.myapplication.adapter.Myadapter;
import java.util.ArrayList;
import java.util.List;
public class WxActivity extends AppCompatActivity implements View.OnClickListener {
private Button contactbtn;
private Button friendbtn;
private Button newbtn;
private ViewPager viewPager;
private ContactFragment contactFragment;
private FriendFragment friendFragment;
private NewFragment newFragment;
private List<FriendFragment> fragmentList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wx);
bindID();
contactFragment = new ContactFragment();
friendFragment = new FriendFragment();
newFragment = new NewFragment();
fragmentList.add(friendFragment);
fragmentList.add(newFragment);
fragmentList.add(contactFragment);
Myadapter adapter=new Myadapter(getSupportFragmentManager(),fragmentList);
viewPager.setAdapter(adapter);
}
private void bindID() {
contactbtn = findViewById(R.id.chat);
friendbtn = findViewById(R.id.friend);
newbtn = findViewById(R.id.news);
contactbtn.setOnClickListener(this);
friendbtn.setOnClickListener(this);
newbtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.chat:
break;
case R.id.friend:
break;
case R.id.news:
break;
}
}
}
ps:本来还想放几张效果图的,但是图片太大了,上传不了。本人还是一名学生,如果有写的不好的地方,多多包涵。如果有错误,麻烦联系我指出错误,本人一定虚心聆听。